public void CanRead_ReturnsTrueOnlyForJsonPatchContentType(string requestContentType, bool expectedCanRead) { // Arrange var logger = GetLogger(); var formatter = new JsonPatchInputFormatter(logger, _serializerSettings, ArrayPool <char> .Shared, _objectPoolProvider); var content = "[{\"op\": \"add\", \"path\" : \"Customer/Name\", \"value\":\"John\"}]"; var contentBytes = Encoding.UTF8.GetBytes(content); var modelState = new ModelStateDictionary(); var httpContext = GetHttpContext(contentBytes, contentType: requestContentType); var provider = new EmptyModelMetadataProvider(); var metadata = provider.GetMetadataForType(typeof(JsonPatchDocument <Customer>)); var formatterContext = new InputFormatterContext( httpContext, modelName: string.Empty, modelState: modelState, metadata: metadata, readerFactory: new TestHttpRequestStreamReaderFactory().CreateReader); // Act var result = formatter.CanRead(formatterContext); // Assert Assert.Equal(expectedCanRead, result); }
public async Task JsonPatchInputFormatter_ReturnsModelStateErrors_InvalidModelType() { // Arrange var exceptionMessage = "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type " + $"'{typeof(Customer).FullName}' because the type requires a JSON object "; var logger = GetLogger(); var formatter = new JsonPatchInputFormatter(logger, _serializerSettings, ArrayPool <char> .Shared, _objectPoolProvider); var content = "[{\"op\": \"add\", \"path\" : \"Customer/Name\", \"value\":\"John\"}]"; var contentBytes = Encoding.UTF8.GetBytes(content); var modelState = new ModelStateDictionary(); var httpContext = GetHttpContext(contentBytes, contentType: "application/json-patch+json"); var provider = new EmptyModelMetadataProvider(); var metadata = provider.GetMetadataForType(typeof(Customer)); var context = new InputFormatterContext( httpContext, modelName: string.Empty, modelState: modelState, metadata: metadata, readerFactory: new TestHttpRequestStreamReaderFactory().CreateReader); // Act var result = await formatter.ReadAsync(context); // Assert Assert.True(result.HasError); Assert.Contains(exceptionMessage, modelState[""].Errors[0].Exception.Message); }
public async Task JsonPatchInputFormatter_ReadsMultipleOperations_Successfully() { // Arrange var logger = GetLogger(); var formatter = new JsonPatchInputFormatter(logger, _serializerSettings, ArrayPool <char> .Shared, _objectPoolProvider); var content = "[{\"op\": \"add\", \"path\" : \"Customer/Name\", \"value\":\"John\"}," + "{\"op\": \"remove\", \"path\" : \"Customer/Name\"}]"; var contentBytes = Encoding.UTF8.GetBytes(content); var modelState = new ModelStateDictionary(); var httpContext = GetHttpContext(contentBytes); var provider = new EmptyModelMetadataProvider(); var metadata = provider.GetMetadataForType(typeof(JsonPatchDocument <Customer>)); var context = new InputFormatterContext( httpContext, modelName: string.Empty, modelState: modelState, metadata: metadata, readerFactory: new TestHttpRequestStreamReaderFactory().CreateReader); // Act var result = await formatter.ReadAsync(context); // Assert Assert.False(result.HasError); var patchDoc = Assert.IsType <JsonPatchDocument <Customer> >(result.Model); Assert.Equal("add", patchDoc.Operations[0].op); Assert.Equal("Customer/Name", patchDoc.Operations[0].path); Assert.Equal("John", patchDoc.Operations[0].value); Assert.Equal("remove", patchDoc.Operations[1].op); Assert.Equal("Customer/Name", patchDoc.Operations[1].path); }
public void CanRead_ReturnsFalse_NonJsonPatchContentType(Type modelType) { // Arrange var logger = GetLogger(); var formatter = new JsonPatchInputFormatter(logger, _serializerSettings, ArrayPool <char> .Shared, _objectPoolProvider); var content = "[{\"op\": \"add\", \"path\" : \"Customer/Name\", \"value\":\"John\"}]"; var contentBytes = Encoding.UTF8.GetBytes(content); var modelState = new ModelStateDictionary(); var httpContext = GetHttpContext(contentBytes, contentType: "application/json-patch+json"); var provider = new EmptyModelMetadataProvider(); var metadata = provider.GetMetadataForType(modelType); var formatterContext = new InputFormatterContext( httpContext, modelName: string.Empty, modelState: modelState, metadata: metadata, readerFactory: new TestHttpRequestStreamReaderFactory().CreateReader); // Act var result = formatter.CanRead(formatterContext); // Assert Assert.False(result); }
public async Task SuppressInputFormatterBufferingSetToTrue_UsingMutatedOptions_DoesNotBufferRequestBody() { // Arrange var logger = GetLogger(); var mvcOptions = new MvcOptions(); mvcOptions.SuppressInputFormatterBuffering = false; var formatter = new JsonPatchInputFormatter( logger, _serializerSettings, ArrayPool <char> .Shared, _objectPoolProvider, mvcOptions); var content = "[{\"op\":\"add\",\"path\":\"Customer/Name\",\"value\":\"John\"}]"; var contentBytes = Encoding.UTF8.GetBytes(content); var modelState = new ModelStateDictionary(); var httpContext = new DefaultHttpContext(); httpContext.Features.Set <IHttpResponseFeature>(new TestResponseFeature()); httpContext.Request.Body = new NonSeekableReadStream(contentBytes); httpContext.Request.ContentType = "application/json"; var provider = new EmptyModelMetadataProvider(); var metadata = provider.GetMetadataForType(typeof(JsonPatchDocument <Customer>)); var context = new InputFormatterContext( httpContext, modelName: string.Empty, modelState: modelState, metadata: metadata, readerFactory: new TestHttpRequestStreamReaderFactory().CreateReader); // Act // Mutate options after passing into the constructor to make sure that the value type is not store in the constructor mvcOptions.SuppressInputFormatterBuffering = true; var result = await formatter.ReadAsync(context); // Assert Assert.False(result.HasError); var patchDoc = Assert.IsType <JsonPatchDocument <Customer> >(result.Model); Assert.Equal("add", patchDoc.Operations[0].op); Assert.Equal("Customer/Name", patchDoc.Operations[0].path); Assert.Equal("John", patchDoc.Operations[0].value); Assert.False(httpContext.Request.Body.CanSeek); result = await formatter.ReadAsync(context); // Assert Assert.False(result.HasError); Assert.Null(result.Model); }
public async Task BuffersRequestBody_ByDefault() { // Arrange var logger = GetLogger(); #pragma warning disable CS0618 var formatter = new JsonPatchInputFormatter(logger, _serializerSettings, ArrayPool <char> .Shared, _objectPoolProvider); #pragma warning restore CS0618 var content = "[{\"op\":\"add\",\"path\":\"Customer/Name\",\"value\":\"John\"}]"; var contentBytes = Encoding.UTF8.GetBytes(content); var modelState = new ModelStateDictionary(); var httpContext = new DefaultHttpContext(); httpContext.Features.Set <IHttpResponseFeature>(new TestResponseFeature()); httpContext.Request.Body = new NonSeekableReadStream(contentBytes); httpContext.Request.ContentType = "application/json"; var provider = new EmptyModelMetadataProvider(); var metadata = provider.GetMetadataForType(typeof(JsonPatchDocument <Customer>)); var context = new InputFormatterContext( httpContext, modelName: string.Empty, modelState: modelState, metadata: metadata, readerFactory: new TestHttpRequestStreamReaderFactory().CreateReader); // Act var result = await formatter.ReadAsync(context); // Assert Assert.False(result.HasError); var patchDoc = Assert.IsType <JsonPatchDocument <Customer> >(result.Model); Assert.Equal("add", patchDoc.Operations[0].op); Assert.Equal("Customer/Name", patchDoc.Operations[0].path); Assert.Equal("John", patchDoc.Operations[0].value); Assert.True(httpContext.Request.Body.CanSeek); httpContext.Request.Body.Seek(0L, SeekOrigin.Begin); result = await formatter.ReadAsync(context); // Assert Assert.False(result.HasError); patchDoc = Assert.IsType <JsonPatchDocument <Customer> >(result.Model); Assert.Equal("add", patchDoc.Operations[0].op); Assert.Equal("Customer/Name", patchDoc.Operations[0].path); Assert.Equal("John", patchDoc.Operations[0].value); }
public async Task Version_2_1_Constructor_SuppressInputFormatterBuffering_DoesNotBufferRequestBody() { // Arrange var mvcOptions = new MvcOptions() { SuppressInputFormatterBuffering = false, }; var formatter = new JsonPatchInputFormatter( GetLogger(), _serializerSettings, ArrayPool <char> .Shared, _objectPoolProvider, mvcOptions, new MvcJsonOptions()); var content = "[{\"op\":\"add\",\"path\":\"Customer/Name\",\"value\":\"John\"}]"; var contentBytes = Encoding.UTF8.GetBytes(content); var httpContext = new DefaultHttpContext(); httpContext.Features.Set <IHttpResponseFeature>(new TestResponseFeature()); httpContext.Request.Body = new NonSeekableReadStream(contentBytes); httpContext.Request.ContentType = "application/json"; var formatterContext = CreateInputFormatterContext(typeof(JsonPatchDocument <Customer>), httpContext); // Act // Mutate options after passing into the constructor to make sure that the value type is not store in the constructor mvcOptions.SuppressInputFormatterBuffering = true; var result = await formatter.ReadAsync(formatterContext); // Assert Assert.False(result.HasError); var patchDocument = Assert.IsType <JsonPatchDocument <Customer> >(result.Model); Assert.Equal("add", patchDocument.Operations[0].op); Assert.Equal("Customer/Name", patchDocument.Operations[0].path); Assert.Equal("John", patchDocument.Operations[0].value); Assert.False(httpContext.Request.Body.CanSeek); result = await formatter.ReadAsync(formatterContext); // Assert Assert.False(result.HasError); Assert.Null(result.Model); }
public async Task Version_2_1_Constructor_BuffersRequestBody_ByDefault() { // Arrange var formatter = new JsonPatchInputFormatter( GetLogger(), _serializerSettings, ArrayPool <char> .Shared, _objectPoolProvider, new MvcOptions(), new MvcJsonOptions()); var content = "[{\"op\":\"add\",\"path\":\"Customer/Name\",\"value\":\"John\"}]"; var contentBytes = Encoding.UTF8.GetBytes(content); var httpContext = new DefaultHttpContext(); httpContext.Features.Set <IHttpResponseFeature>(new TestResponseFeature()); httpContext.Request.Body = new NonSeekableReadStream(contentBytes); httpContext.Request.ContentType = "application/json"; var formatterContext = CreateInputFormatterContext(typeof(JsonPatchDocument <Customer>), httpContext); // Act var result = await formatter.ReadAsync(formatterContext); // Assert Assert.False(result.HasError); var patchDocument = Assert.IsType <JsonPatchDocument <Customer> >(result.Model); Assert.Equal("add", patchDocument.Operations[0].op); Assert.Equal("Customer/Name", patchDocument.Operations[0].path); Assert.Equal("John", patchDocument.Operations[0].value); Assert.True(httpContext.Request.Body.CanSeek); httpContext.Request.Body.Seek(0L, SeekOrigin.Begin); result = await formatter.ReadAsync(formatterContext); // Assert Assert.False(result.HasError); patchDocument = Assert.IsType <JsonPatchDocument <Customer> >(result.Model); Assert.Equal("add", patchDocument.Operations[0].op); Assert.Equal("Customer/Name", patchDocument.Operations[0].path); Assert.Equal("John", patchDocument.Operations[0].value); }