public async Task JsonPatchInputFormatter_ReadsOneOperation_Successfully() { // Arrange var formatter = new JsonPatchInputFormatter(); var content = "[{\"op\":\"add\",\"path\":\"Customer/Name\",\"value\":\"John\"}]"; var contentBytes = Encoding.UTF8.GetBytes(content); var actionContext = GetActionContext(contentBytes); var context = new InputFormatterContext(actionContext, typeof(JsonPatchDocument<Customer>)); // Act var model = await formatter.ReadAsync(context); // Assert var patchDoc = Assert.IsType<JsonPatchDocument<Customer>>(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 JsonPatchInputFormatter_ReturnsModelStateErrors_InvalidModelType() { // Arrange var exceptionMessage = "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type " + "'Microsoft.AspNet.Mvc.JsonPatchInputFormatterTest+Customer' 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 actionContext = GetActionContext(contentBytes, contentType: "application/json-patch+json"); var context = new InputFormatterContext(actionContext, typeof(Customer)); // Act var model = await formatter.ReadAsync(context); // Assert Assert.Contains(exceptionMessage, actionContext.ModelState[""].Errors[0].Exception.Message); }
public async Task JsonPatchInputFormatter_ReadsOneOperation_Successfully() { // Arrange var formatter = new JsonPatchInputFormatter(); var content = "[{\"op\":\"add\",\"path\":\"Customer/Name\",\"value\":\"John\"}]"; var contentBytes = Encoding.UTF8.GetBytes(content); var actionContext = GetActionContext(contentBytes); var context = new InputFormatterContext(actionContext, typeof(JsonPatchDocument <Customer>)); // Act var model = await formatter.ReadAsync(context); // Assert var patchDoc = Assert.IsType <JsonPatchDocument <Customer> >(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 JsonPatchInputFormatter_ReturnsModelStateErrors_InvalidModelType() { // Arrange var exceptionMessage = "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type " + "'Microsoft.AspNet.Mvc.JsonPatchInputFormatterTest+Customer' 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 context = new InputFormatterContext(httpContext, modelState, typeof(Customer)); // Act var model = await formatter.ReadAsync(context); // Assert Assert.Contains(exceptionMessage, modelState[""].Errors[0].Exception.Message); }