public async Task BindModel_DerivedJsonInputFormatter_AddsErrorToModelState( InputFormatterExceptionModelStatePolicy inputFormatterExceptionModelStatePolicy) { // Arrange var httpContext = new DefaultHttpContext(); httpContext.Request.Body = new MemoryStream(Encoding.UTF8.GetBytes("Bad data!")); httpContext.Request.ContentType = "application/json"; var metadataProvider = new TestModelMetadataProvider(); metadataProvider.ForType <Person>().BindingDetails(d => d.BindingSource = BindingSource.Body); var bindingContext = GetBindingContext(typeof(Person), httpContext, metadataProvider); var binder = CreateBinder( new[] { new DerivedJsonInputFormatter(throwNonInputFormatterException: false) }, new MvcOptions() { InputFormatterExceptionModelStatePolicy = inputFormatterExceptionModelStatePolicy }); // Act await binder.BindModelAsync(bindingContext); // Assert Assert.False(bindingContext.Result.IsModelSet); Assert.Null(bindingContext.Result.Model); // Key is the empty string because this was a top-level binding. var entry = Assert.Single(bindingContext.ModelState); Assert.Equal(string.Empty, entry.Key); Assert.IsType <JsonReaderException>(entry.Value.Errors[0].Exception); }
public async Task BindModel_BuiltInInputFormatters_ThrowingNonInputFormatterException_Throws( IInputFormatter formatter, string contentType, InputFormatterExceptionModelStatePolicy inputFormatterExceptionModelStatePolicy) { // Arrange var httpContext = new DefaultHttpContext(); httpContext.Request.Body = new MemoryStream(Encoding.UTF8.GetBytes("valid data!")); httpContext.Request.ContentType = contentType; var metadataProvider = new TestModelMetadataProvider(); metadataProvider.ForType <Person>().BindingDetails(d => d.BindingSource = BindingSource.Body); var bindingContext = GetBindingContext(typeof(Person), httpContext, metadataProvider); var binder = CreateBinder(new[] { formatter }, new MvcOptions() { InputFormatterExceptionModelStatePolicy = inputFormatterExceptionModelStatePolicy }); // Act & Assert var exception = await Assert.ThrowsAsync <IOException>(() => binder.BindModelAsync(bindingContext)); Assert.Equal("Unable to read input stream!!", exception.Message); }
public async Task BindModel_CustomFormatter_ThrowingInputFormatterException_AddsErrorToModelState( InputFormatterExceptionModelStatePolicy inputFormatterExceptionModelStatePolicy) { // Arrange var httpContext = new DefaultHttpContext(); httpContext.Request.Body = new MemoryStream(Encoding.UTF8.GetBytes("Bad data!")); httpContext.Request.ContentType = "text/xyz"; var metadataProvider = new TestModelMetadataProvider(); metadataProvider.ForType <Person>().BindingDetails(d => d.BindingSource = BindingSource.Body); var expectedFormatException = new FormatException("bad format!"); var bindingContext = GetBindingContext(typeof(Person), httpContext, metadataProvider); var formatter = new XyzFormatter((inputFormatterContext, encoding) => { throw new InputFormatterException("Bad input!!", expectedFormatException); }); var binder = CreateBinder( new[] { formatter }, new MvcOptions() { InputFormatterExceptionModelStatePolicy = inputFormatterExceptionModelStatePolicy }); // Act await binder.BindModelAsync(bindingContext); // Assert Assert.False(bindingContext.Result.IsModelSet); Assert.Null(bindingContext.Result.Model); // Key is the empty string because this was a top-level binding. var entry = Assert.Single(bindingContext.ModelState); Assert.Equal(string.Empty, entry.Key); var errorMessage = Assert.Single(entry.Value.Errors).Exception.Message; Assert.Equal("Bad input!!", errorMessage); var formatException = Assert.IsType <FormatException>(entry.Value.Errors[0].Exception.InnerException); Assert.Same(expectedFormatException, formatException); }
public async Task BindModel_DerivedXmlInputFormatters_ThrowingNonInputFormatingException_AddsErrorToModelState( IInputFormatter formatter, string contentType, InputFormatterExceptionModelStatePolicy inputFormatterExceptionModelStatePolicy) { // Arrange var httpContext = new DefaultHttpContext(); httpContext.Request.Body = new MemoryStream(Encoding.UTF8.GetBytes("valid data!")); httpContext.Request.ContentType = contentType; var metadataProvider = new TestModelMetadataProvider(); metadataProvider.ForType <Person>().BindingDetails(d => d.BindingSource = BindingSource.Body); var bindingContext = GetBindingContext(typeof(Person), httpContext, metadataProvider); var binder = CreateBinder(new[] { formatter }, new MvcOptions() { InputFormatterExceptionModelStatePolicy = inputFormatterExceptionModelStatePolicy }); // Act await binder.BindModelAsync(bindingContext); // Assert Assert.False(bindingContext.Result.IsModelSet); Assert.Null(bindingContext.Result.Model); // Key is the empty string because this was a top-level binding. var entry = Assert.Single(bindingContext.ModelState); Assert.Equal(string.Empty, entry.Key); var errorMessage = Assert.Single(entry.Value.Errors).Exception.Message; Assert.Equal("Unable to read input stream!!", errorMessage); Assert.IsType <IOException>(entry.Value.Errors[0].Exception); }