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 formatter = new JsonPatchInputFormatter(); 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); // 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 formatter = new JsonPatchInputFormatter(); 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); // 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); 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 void CanRead_ReturnsTrueOnlyForJsonPatchContentType(string requestContentType, bool expectedCanRead) { // Arrange var logger = GetLogger(); var formatter = new JsonPatchInputFormatter(logger); 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_ReadsMultipleOperations_Successfully() { // Arrange var formatter = new JsonPatchInputFormatter(); 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); // 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_ReturnsTrueOnlyForJsonPatchContentType(string requestContentType, bool expectedCanRead) { // Arrange var formatter = new JsonPatchInputFormatter(); 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 formatterContext = new InputFormatterContext( httpContext, modelName: string.Empty, modelState: modelState, modelType: typeof(JsonPatchDocument <Customer>)); // Act var result = formatter.CanRead(formatterContext); // Assert Assert.Equal(expectedCanRead, result); }
public void CanRead_ReturnsFalse_NonJsonPatchContentType(Type modelType) { // Arrange var formatter = new JsonPatchInputFormatter(); 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 formatterContext = new InputFormatterContext( httpContext, modelName: string.Empty, modelState: modelState, modelType: modelType); // Act var result = formatter.CanRead(formatterContext); // Assert Assert.False(result); }
public void CanRead_ReturnsTrueOnlyForJsonPatchContentType(string requestContentType, bool expectedCanRead) { // Arrange var formatter = new JsonPatchInputFormatter(); 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 formatterContext = new InputFormatterContext( httpContext, modelName: string.Empty, modelState: modelState, modelType: typeof(JsonPatchDocument<Customer>)); // Act var result = formatter.CanRead(formatterContext); // Assert Assert.Equal(expectedCanRead, result); }
public void CanRead_ReturnsFalse_NonJsonPatchContentType(Type modelType) { // Arrange var formatter = new JsonPatchInputFormatter(); 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); // Act var result = formatter.CanRead(formatterContext); // Assert Assert.False(result); }
public void CanRead_ReturnsTrueOnlyForJsonPatchContentType(string requestContentType, bool expectedCanRead) { // Arrange var logger = GetLogger(); var formatter = new JsonPatchInputFormatter(logger); 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); }