Example #1
0
        public async Task BindModel_DerivedJsonInputFormatter_AddsErrorToModelState(
            InputFormatterExceptionPolicy inputFormatterExceptionPolicy)
        {
            // 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()
            {
                InputFormatterExceptionPolicy = inputFormatterExceptionPolicy
            });

            // 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.NotEmpty(entry.Value.Errors[0].ErrorMessage);
            Assert.Null(entry.Value.Errors[0].Exception);
        }
Example #2
0
        public async Task BindModel_BuiltInInputFormatters_ThrowingNonInputFormatterException_Throws(
            IInputFormatter formatter,
            string contentType,
            InputFormatterExceptionPolicy inputFormatterExceptionPolicy)
        {
            // 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()
            {
                InputFormatterExceptionPolicy = inputFormatterExceptionPolicy
            });

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

            Assert.Equal("Unable to read input stream!!", exception.Message);
        }
Example #3
0
        public async Task BindModel_CustomFormatter_ThrowingInputFormatterException_AddsErrorToModelState(
            InputFormatterExceptionPolicy inputFormatterExceptionPolicy)
        {
            // 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()
            {
                InputFormatterExceptionPolicy = inputFormatterExceptionPolicy
            });

            // 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).ErrorMessage;

            Assert.Equal("Bad input!!", errorMessage);
            Assert.Null(entry.Value.Errors[0].Exception);
        }
Example #4
0
        public async Task BindModel_DerivedXmlInputFormatters_ThrowingNonInputFormatingException_AddsErrorToModelState(
            IInputFormatter formatter,
            string contentType,
            InputFormatterExceptionPolicy inputFormatterExceptionPolicy)
        {
            // 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()
            {
                InputFormatterExceptionPolicy = inputFormatterExceptionPolicy
            });

            // 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);
        }