Example #1
0
        public async Task BindModel_CustomFormatter_ThrowingNonInputFormatterException_Throws()
        {
            // Arrange
            var httpContext = new DefaultHttpContext();

            httpContext.Request.Body        = new MemoryStream(Encoding.UTF8.GetBytes("valid data"));
            httpContext.Request.ContentType = "text/xyz";

            var metadataProvider = new TestModelMetadataProvider();

            metadataProvider.ForType <Person>().BindingDetails(d => d.BindingSource = BindingSource.Body);

            var bindingContext = GetBindingContext(typeof(Person), httpContext, metadataProvider);
            var formatter      = new XyzFormatter((inputFormatterContext, encoding) =>
            {
                throw new IOException("Unable to read input stream!!");
            });
            var binder = CreateBinder(
                new[] { formatter },
                new MvcOptions()
            {
                InputFormatterExceptionModelStatePolicy = InputFormatterExceptionModelStatePolicy.MalformedInputExceptions
            });

            // Act
            var exception = await Assert.ThrowsAsync <IOException>(
                () => binder.BindModelAsync(bindingContext));

            Assert.Equal("Unable to read input stream!!", exception.Message);
        }
Example #2
0
        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);
        }