public async Task BindModelAsync_LogsFormatterRejectionAndSelection() { // Arrange var sink = new TestSink(); var loggerFactory = new TestLoggerFactory(sink, enabled: true); var inputFormatters = new List <IInputFormatter>() { new TestInputFormatter(canRead: false), new TestInputFormatter(canRead: true), }; var provider = new TestModelMetadataProvider(); provider.ForType <Person>().BindingDetails(d => d.BindingSource = BindingSource.Body); var bindingContext = GetBindingContext(typeof(Person), metadataProvider: provider); bindingContext.HttpContext.Request.ContentType = "application/json"; var binder = new BodyModelBinder(inputFormatters, new TestHttpRequestStreamReaderFactory(), loggerFactory); // Act await binder.BindModelAsync(bindingContext); // Assert Assert.Equal($"Rejected input formatter '{typeof(TestInputFormatter)}' for content type 'application/json'.", sink.Writes[0].State.ToString()); Assert.Equal($"Selected input formatter '{typeof(TestInputFormatter)}' for content type 'application/json'.", sink.Writes[1].State.ToString()); }
public async Task BindModelAsync_LogsNoFormatterSelectedAndRemoveFromBodyAttribute() { // Arrange var sink = new TestSink(); var loggerFactory = new TestLoggerFactory(sink, enabled: true); var inputFormatters = new List <IInputFormatter>() { new TestInputFormatter(canRead: false), new TestInputFormatter(canRead: false), }; var provider = new TestModelMetadataProvider(); provider.ForType <Person>().BindingDetails(d => d.BindingSource = BindingSource.Body); var bindingContext = GetBindingContext(typeof(Person), metadataProvider: provider); bindingContext.HttpContext.Request.ContentType = "multipart/form-data"; bindingContext.BinderModelName = bindingContext.ModelName; var binder = new BodyModelBinder(inputFormatters, new TestHttpRequestStreamReaderFactory(), loggerFactory); // Act await binder.BindModelAsync(bindingContext); // Assert Assert.Collection( sink.Writes, write => Assert.Equal( $"Rejected input formatter '{typeof(TestInputFormatter)}' for content type 'multipart/form-data'.", write.State.ToString()), write => Assert.Equal( $"Rejected input formatter '{typeof(TestInputFormatter)}' for content type 'multipart/form-data'.", write.State.ToString()), write => Assert.Equal( "No input formatter was found to support the content type 'multipart/form-data' for use with the [FromBody] attribute.", write.State.ToString()), write => Assert.Equal( $"To use model binding, remove the [FromBody] attribute from the property or parameter named '{bindingContext.ModelName}' with model type '{bindingContext.ModelType}'.", write.State.ToString())); }
public async Task BindModelAsync_DoesNotThrowNullReferenceException() { // Arrange var httpContext = new DefaultHttpContext(); var provider = new TestModelMetadataProvider(); provider.ForType <Person>().BindingDetails(d => d.BindingSource = BindingSource.Body); var bindingContext = GetBindingContext( typeof(Person), httpContext: httpContext, metadataProvider: provider); var binder = new BodyModelBinder(new List <IInputFormatter>(), new TestHttpRequestStreamReaderFactory()); // Act & Assert (does not throw) await binder.BindModelAsync(bindingContext); }
/// <summary> /// Creates a new <see cref="XmlBodyModelBinder"/>. /// </summary> /// <param name="options">The configuration for the MVC framework.</param> /// <param name="readerFactory"> /// The <see cref="IHttpRequestStreamReaderFactory"/>, used to create <see cref="System.IO.TextReader"/> /// instances for reading the request body. /// </param> public XmlBodyModelBinder(IOptions <MvcOptions> options, IHttpRequestStreamReaderFactory readerFactory) { if (options == null) { throw new ArgumentNullException(nameof(options)); } if (readerFactory == null) { throw new ArgumentNullException(nameof(readerFactory)); } IList <IInputFormatter> formatters = options.Value.InputFormatters; var list = new List <IInputFormatter>() { new XmlSerializerInputFormatter(options.Value) }; list.AddRange(formatters); BodyModelBinder = new BodyModelBinder(list, readerFactory); }