示例#1
0
        public async Task ReadAsync_UsesTryAddModelValidationErrorsToModelState_WhenCaptureErrorsIsSet()
        {
            // Arrange
            var content   = "{name: 'Person Name', Age: 'not-an-age'}";
            var formatter = new JsonInputFormatter {
                CaptureDeserilizationErrors = true
            };
            var contentBytes = Encoding.UTF8.GetBytes(content);

            var actionContext = GetActionContext(contentBytes);
            var metadata      = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(User));
            var context       = new InputFormatterContext(actionContext, metadata.ModelType);

            actionContext.ModelState.MaxAllowedErrors = 3;
            actionContext.ModelState.AddModelError("key1", "error1");
            actionContext.ModelState.AddModelError("key2", "error2");

            // Act
            var model = await formatter.ReadAsync(context);

            // Assert
            Assert.False(actionContext.ModelState.ContainsKey("age"));
            var error = Assert.Single(actionContext.ModelState[""].Errors);

            Assert.IsType <TooManyModelErrorsException>(error.Exception);
        }
示例#2
0
        public async Task CustomSerializerSettingsObject_TakesEffect()
        {
            // Arrange
            // missing password property here
            var contentBytes = Encoding.UTF8.GetBytes("{ \"UserName\" : \"John\"}");

            var jsonFormatter = new JsonInputFormatter()
            {
                CaptureDeserilizationErrors = true
            };

            // by default we ignore missing members, so here explicitly changing it
            jsonFormatter.SerializerSettings = new JsonSerializerSettings()
            {
                MissingMemberHandling = MissingMemberHandling.Error
            };

            var actionContext = GetActionContext(contentBytes, "application/json;charset=utf-8");
            var metadata      = new EmptyModelMetadataProvider().GetMetadataForType(modelAccessor: null,
                                                                                    modelType: typeof(UserLogin));
            var inputFormatterContext = new InputFormatterContext(actionContext, metadata.ModelType);

            // Act
            var obj = await jsonFormatter.ReadAsync(inputFormatterContext);

            // Assert
            Assert.False(actionContext.ModelState.IsValid);

            var modelErrorMessage = actionContext.ModelState.Values.First().Errors[0].Exception.Message;

            Assert.Contains("Required property 'Password' not found in JSON", modelErrorMessage);
        }
示例#3
0
        public async Task ReadAsync_UsesTryAddModelValidationErrorsToModelState()
        {
            // Arrange
            var content      = "{name: 'Person Name', Age: 'not-an-age'}";
            var formatter    = new JsonInputFormatter();
            var contentBytes = Encoding.UTF8.GetBytes(content);

            var modelState  = new ModelStateDictionary();
            var httpContext = GetHttpContext(contentBytes);

            var context = new InputFormatterContext(httpContext, modelState, typeof(User));

            modelState.MaxAllowedErrors = 3;
            modelState.AddModelError("key1", "error1");
            modelState.AddModelError("key2", "error2");

            // Act
            var model = await formatter.ReadAsync(context);

            // Assert
            Assert.False(modelState.ContainsKey("age"));
            var error = Assert.Single(modelState[""].Errors);

            Assert.IsType <TooManyModelErrorsException>(error.Exception);
        }
示例#4
0
        public async Task CustomSerializerSettingsObject_TakesEffect()
        {
            // Arrange
            // missing password property here
            var contentBytes = Encoding.UTF8.GetBytes("{ \"UserName\" : \"John\"}");

            var jsonFormatter = new JsonInputFormatter();

            // by default we ignore missing members, so here explicitly changing it
            jsonFormatter.SerializerSettings = new JsonSerializerSettings()
            {
                MissingMemberHandling = MissingMemberHandling.Error
            };

            var modelState  = new ModelStateDictionary();
            var httpContext = GetHttpContext(contentBytes, "application/json;charset=utf-8");

            var inputFormatterContext = new InputFormatterContext(httpContext, modelState, typeof(UserLogin));

            // Act
            var obj = await jsonFormatter.ReadAsync(inputFormatterContext);

            // Assert
            Assert.False(modelState.IsValid);

            var modelErrorMessage = modelState.Values.First().Errors[0].Exception.Message;

            Assert.Contains("Required property 'Password' not found in JSON", modelErrorMessage);
        }
示例#5
0
        public async Task Validates_RequiredAttributeOnStructTypes()
        {
            // Arrange
            var contentBytes          = Encoding.UTF8.GetBytes("{\"Longitude\":{}}");
            var jsonFormatter         = new JsonInputFormatter();
            var actionContext         = GetActionContext(contentBytes, "application/json;charset=utf-8");
            var metadata              = new EmptyModelMetadataProvider().GetMetadataForType(typeof(GpsCoordinate));
            var inputFormatterContext = new InputFormatterContext(actionContext, metadata.ModelType);

            // Act
            var obj = await jsonFormatter.ReadAsync(inputFormatterContext);

            // Assert
            Assert.False(actionContext.ModelState.IsValid);
            Assert.Equal(2, actionContext.ModelState.Count);
            var errorMessages = GetModelStateErrorMessages(actionContext.ModelState);

            Assert.Equal(3, errorMessages.Count());
            Assert.Contains(
                errorMessages,
                (errorMessage) => errorMessage.Contains("Required property 'Latitude' not found in JSON"));
            Assert.Contains(
                errorMessages,
                (errorMessage) => errorMessage.Contains("Required property 'X' not found in JSON"));
            Assert.Contains(
                errorMessages,
                (errorMessage) => errorMessage.Contains("Required property 'Y' not found in JSON"));
        }
示例#6
0
        public async Task ThrowsException_OnSupplyingNull_ForRequiredValueType()
        {
            // Arrange
            var contentBytes          = Encoding.UTF8.GetBytes("{\"Id\":\"null\",\"Name\":\"Programming C#\"}");
            var jsonFormatter         = new JsonInputFormatter();
            var actionContext         = GetActionContext(contentBytes, "application/json;charset=utf-8");
            var metadata              = new EmptyModelMetadataProvider().GetMetadataForType(typeof(Book));
            var inputFormatterContext = new InputFormatterContext(actionContext, metadata.ModelType);

            // Act
            var obj = await jsonFormatter.ReadAsync(inputFormatterContext);

            // Assert
            var book = obj as Book;

            Assert.NotNull(book);
            Assert.Equal(0, book.Id);
            Assert.Equal("Programming C#", book.Name);
            Assert.False(actionContext.ModelState.IsValid);

            Assert.Equal(1, actionContext.ModelState.Values.First().Errors.Count);
            var modelErrorMessage = actionContext.ModelState.Values.First().Errors[0].Exception.Message;

            Assert.Contains("Could not convert string to integer: null. Path 'Id'", modelErrorMessage);
        }
示例#7
0
        public async Task ReadAsync_ThrowsOnDeserializationErrors()
        {
            // Arrange
            var content      = "{name: 'Person Name', Age: 'not-an-age'}";
            var formatter    = new JsonInputFormatter();
            var contentBytes = Encoding.UTF8.GetBytes(content);

            var httpContext = GetActionContext(contentBytes);
            var metadata    = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(User));
            var context     = new InputFormatterContext(httpContext, metadata.ModelType);

            // Act and Assert
            await Assert.ThrowsAsync <JsonReaderException>(() => formatter.ReadAsync(context));
        }
示例#8
0
        public async Task JsonFormatterReadsSimpleTypes(string content, Type type, object expected)
        {
            // Arrange
            var formatter    = new JsonInputFormatter();
            var contentBytes = Encoding.UTF8.GetBytes(content);

            var actionContext = GetActionContext(contentBytes);
            var context       = new InputFormatterContext(actionContext, type);

            // Act
            var model = await formatter.ReadAsync(context);

            // Assert
            Assert.Equal(expected, model);
        }
示例#9
0
        public async Task ReadAsync_AddsModelValidationErrorsToModelState()
        {
            // Arrange
            var content      = "{name: 'Person Name', Age: 'not-an-age'}";
            var formatter    = new JsonInputFormatter();
            var contentBytes = Encoding.UTF8.GetBytes(content);

            var actionContext = GetActionContext(contentBytes);
            var metadata      = new EmptyModelMetadataProvider().GetMetadataForType(typeof(User));
            var context       = new InputFormatterContext(actionContext, metadata.ModelType);

            // Act
            var model = await formatter.ReadAsync(context);

            // Assert
            Assert.Equal("Could not convert string to decimal: not-an-age. Path 'Age', line 1, position 39.",
                         actionContext.ModelState["Age"].Errors[0].Exception.Message);
        }
示例#10
0
        public async Task JsonFormatterReadsComplexTypes()
        {
            // Arrange
            var content      = "{name: 'Person Name', Age: '30'}";
            var formatter    = new JsonInputFormatter();
            var contentBytes = Encoding.UTF8.GetBytes(content);

            var httpContext = GetHttpContext(contentBytes);
            var context     = new InputFormatterContext(httpContext, new ModelStateDictionary(), typeof(User));

            // Act
            var model = await formatter.ReadAsync(context);

            // Assert
            var userModel = Assert.IsType <User>(model);

            Assert.Equal("Person Name", userModel.Name);
            Assert.Equal(30, userModel.Age);
        }
示例#11
0
        public async Task JsonFormatterReadsComplexTypes()
        {
            // Arrange
            var content      = "{name: 'Person Name', Age: '30'}";
            var formatter    = new JsonInputFormatter();
            var contentBytes = Encoding.UTF8.GetBytes(content);

            var actionContext = GetActionContext(contentBytes);
            var metadata      = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(User));
            var context       = new InputFormatterContext(actionContext, metadata.ModelType);

            // Act
            var model = await formatter.ReadAsync(context);

            // Assert
            var userModel = Assert.IsType <User>(model);

            Assert.Equal("Person Name", userModel.Name);
            Assert.Equal(30, userModel.Age);
        }
示例#12
0
        public async Task ReadAsync_AddsModelValidationErrorsToModelState()
        {
            // Arrange
            var content      = "{name: 'Person Name', Age: 'not-an-age'}";
            var formatter    = new JsonInputFormatter();
            var contentBytes = Encoding.UTF8.GetBytes(content);

            var modelState  = new ModelStateDictionary();
            var httpContext = GetHttpContext(contentBytes);

            var context = new InputFormatterContext(httpContext, modelState, typeof(User));

            // Act
            var model = await formatter.ReadAsync(context);

            // Assert
            Assert.Equal(
                "Could not convert string to decimal: not-an-age. Path 'Age', line 1, position 39.",
                modelState["Age"].Errors[0].Exception.Message);
        }
示例#13
0
        public async Task Validation_DoesNotHappen_ForNonRequired_ValueTypeProperties()
        {
            // Arrange
            var contentBytes          = Encoding.UTF8.GetBytes("{\"Name\":\"Seattle\"}");
            var jsonFormatter         = new JsonInputFormatter();
            var actionContext         = GetActionContext(contentBytes, "application/json;charset=utf-8");
            var metadata              = new EmptyModelMetadataProvider().GetMetadataForType(typeof(Location));
            var inputFormatterContext = new InputFormatterContext(actionContext, metadata.ModelType);

            // Act
            var obj = await jsonFormatter.ReadAsync(inputFormatterContext);

            // Assert
            Assert.True(actionContext.ModelState.IsValid);
            var location = obj as Location;

            Assert.NotNull(location);
            Assert.Equal(0, location.Id);
            Assert.Equal("Seattle", location.Name);
        }
示例#14
0
        public async Task Validates_RequiredAttribute_OnRegularAndInheritedProperties(Type type)
        {
            // Arrange
            var contentBytes          = Encoding.UTF8.GetBytes("{ \"Name\" : \"Programming C#\"}");
            var jsonFormatter         = new JsonInputFormatter();
            var actionContext         = GetActionContext(contentBytes, "application/json;charset=utf-8");
            var metadata              = new EmptyModelMetadataProvider().GetMetadataForType(type);
            var inputFormatterContext = new InputFormatterContext(actionContext, metadata.ModelType);

            // Act
            var obj = await jsonFormatter.ReadAsync(inputFormatterContext);

            // Assert
            Assert.False(actionContext.ModelState.IsValid);
            Assert.Equal(1, actionContext.ModelState.Count);

            var modelErrorMessage = actionContext.ModelState.Values.First().Errors[0].Exception.Message;

            Assert.Contains("Required property 'Id' not found in JSON", modelErrorMessage);
        }
示例#15
0
        public async Task Validation_DoesNotHappen_OnNullableValueTypeProperties()
        {
            // Arrange
            var contentBytes          = Encoding.UTF8.GetBytes("{}");
            var jsonFormatter         = new JsonInputFormatter();
            var actionContext         = GetActionContext(contentBytes, "application/json;charset=utf-8");
            var metadata              = new EmptyModelMetadataProvider().GetMetadataForType(typeof(Venue));
            var inputFormatterContext = new InputFormatterContext(actionContext, metadata.ModelType);

            // Act
            var obj = await jsonFormatter.ReadAsync(inputFormatterContext);

            // Assert
            Assert.True(actionContext.ModelState.IsValid);
            var venue = obj as Venue;

            Assert.NotNull(venue);
            Assert.Null(venue.Location);
            Assert.Null(venue.NearByLocations);
            Assert.Null(venue.Name);
        }