Exemplo n.º 1
0
        public void DefaultMediaType_ReturnsApplicationJson()
        {
            // Arrange 
            var formatter = new JsonInputFormatter();

            // Act
            var mediaType = formatter.SupportedMediaTypes[0];

            // Assert
            Assert.Equal("application/json", mediaType);
        }
Exemplo n.º 2
0
        public void DefaultMediaType_ReturnsApplicationJson()
        {
            // Arrange
            var formatter = new JsonInputFormatter();

            // Act
            var mediaType = formatter.SupportedMediaTypes[0];

            // Assert
            Assert.Equal("application/json", mediaType);
        }
Exemplo n.º 3
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 = GetHttpContext(contentBytes);
            var modelState  = new ModelStateDictionary();
            var metadata    = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(User));
            var context     = new InputFormatterContext(httpContext, metadata, modelState);

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

            var httpContext = GetHttpContext(contentBytes);
            var modelState  = new ModelStateDictionary();
            var metadata    = new EmptyModelMetadataProvider().GetMetadataForType(null, type);
            var context     = new InputFormatterContext(httpContext, metadata, modelState);

            // Act
            await formatter.ReadAsync(context);

            // Assert
            Assert.Equal(expected, context.Model);
        }
Exemplo n.º 5
0
        public async Task JsonFormatterReadsSimpleTypes(string content, Type type, object expected)
        {
            // Arrange
            var formatter = new JsonInputFormatter();
            var contentBytes = Encoding.UTF8.GetBytes(content);

            var httpContext = GetHttpContext(contentBytes);
            var modelState = new ModelStateDictionary();
            var metadata = new EmptyModelMetadataProvider().GetMetadataForType(null, type);
            var context = new InputFormatterContext(httpContext, metadata, modelState);

            // Act
            await formatter.ReadAsync(context);

            // Assert
            Assert.Equal(expected, context.Model);
        }
Exemplo n.º 6
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 modelState = new ModelStateDictionary();
            var metadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(User));
            var context = new InputFormatterContext(httpContext, metadata, modelState);

            // Act
            await formatter.ReadAsync(context);

            // Assert
            var model = Assert.IsType<User>(context.Model);
            Assert.Equal("Person Name", model.Name);
            Assert.Equal(30, model.Age);
        }
Exemplo n.º 7
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 modelState  = new ModelStateDictionary();
            var metadata    = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(User));
            var context     = new InputFormatterContext(httpContext, metadata, modelState);

            // Act
            await formatter.ReadAsync(context);

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

            Assert.Equal("Person Name", model.Name);
            Assert.Equal(30, model.Age);
        }
Exemplo n.º 8
0
        public async Task ReadAsync_AddsModelValidationErrorsToModelState_WhenCaptureErrorsIsSet()
        {
            // Arrange
            var content   = "{name: 'Person Name', Age: 'not-an-age'}";
            var formatter = new JsonInputFormatter {
                CaptureDeserilizationErrors = true
            };
            var contentBytes = Encoding.UTF8.GetBytes(content);

            var httpContext = GetHttpContext(contentBytes);
            var modelState  = new ModelStateDictionary();
            var metadata    = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(User));
            var context     = new InputFormatterContext(httpContext, metadata, modelState);

            // Act
            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);
        }
Exemplo n.º 9
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 = GetHttpContext(contentBytes);
            var modelState = new ModelStateDictionary();
            var metadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(User));
            var context = new InputFormatterContext(httpContext, metadata, modelState);

            // Act and Assert
            await Assert.ThrowsAsync<JsonReaderException>(() => formatter.ReadAsync(context));
        }
Exemplo n.º 10
0
        public async Task ReadAsync_AddsModelValidationErrorsToModelState_WhenCaptureErrorsIsSet()
        {
            // Arrange
            var content = "{name: 'Person Name', Age: 'not-an-age'}";
            var formatter = new JsonInputFormatter { CaptureDeserilizationErrors = true };
            var contentBytes = Encoding.UTF8.GetBytes(content);

            var httpContext = GetHttpContext(contentBytes);
            var modelState = new ModelStateDictionary();
            var metadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(User));
            var context = new InputFormatterContext(httpContext, metadata, modelState);

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