public async Task Constructor_BuffersRequestBody_ByDefault() { // Arrange var formatter = new NewtonsoftJsonPatchInputFormatter( GetLogger(), _serializerSettings, ArrayPool <char> .Shared, _objectPoolProvider, new MvcOptions(), new MvcNewtonsoftJsonOptions()); 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, allowSyncReads: false); 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); }
public async Task Constructor_SuppressInputFormatterBuffering_DoesNotBufferRequestBody() { // Arrange var mvcOptions = new MvcOptions() { SuppressInputFormatterBuffering = false, }; var formatter = new NewtonsoftJsonPatchInputFormatter( GetLogger(), _serializerSettings, ArrayPool <char> .Shared, _objectPoolProvider, mvcOptions, new MvcNewtonsoftJsonOptions()); 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_0_Constructor_SuppressInputFormatterBuffering_DoesNotBufferRequestBody() { // Arrange #pragma warning disable CS0618 var formatter = new NewtonsoftJsonPatchInputFormatter( GetLogger(), _serializerSettings, ArrayPool <char> .Shared, _objectPoolProvider, suppressInputFormatterBuffering: true); #pragma warning restore CS0618 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 context = CreateInputFormatterContext(typeof(JsonPatchDocument <Customer>), httpContext); // Act var result = await formatter.ReadAsync(context); // 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(context); // Assert Assert.False(result.HasError); Assert.Null(result.Model); }