Added functional tests for JsonPatch
This commit is contained in:
parent
da5c304f5d
commit
68866f8716
|
|
@ -0,0 +1,150 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using FormatterWebSite;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
|
{
|
||||||
|
public class JsonPatchSampleTest : IClassFixture<MvcTestFixture<Startup>>
|
||||||
|
{
|
||||||
|
public JsonPatchSampleTest(MvcTestFixture<Startup> fixture)
|
||||||
|
{
|
||||||
|
Client = fixture.Client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpClient Client { get; }
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task AddOperation_Works()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var input = "[{ 'op': 'add', 'path': 'Reviews/-', 'value': { 'Rating': 3.5 }}]".Replace("'", "\"");
|
||||||
|
var request = GetPatchRequest(input);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await Client.SendAsync(request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
var body = await response.Content.ReadAsStringAsync();
|
||||||
|
var product = JsonConvert.DeserializeObject<Product>(body);
|
||||||
|
Assert.NotNull(product);
|
||||||
|
Assert.NotNull(product.Reviews);
|
||||||
|
Assert.Equal(3, product.Reviews.Count);
|
||||||
|
Assert.Equal(3.5, product.Reviews[2].Rating);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ReplaceOperation_Works()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var input = "[{ 'op': 'replace', 'path': 'Reviews/0/Rating', 'value': 5 }]".Replace("'", "\"");
|
||||||
|
var request = GetPatchRequest(input);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await Client.SendAsync(request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
var body = await response.Content.ReadAsStringAsync();
|
||||||
|
var product = JsonConvert.DeserializeObject<Product>(body);
|
||||||
|
Assert.NotNull(product);
|
||||||
|
Assert.NotNull(product.Reviews);
|
||||||
|
Assert.Equal(2, product.Reviews.Count);
|
||||||
|
Assert.Equal(5, product.Reviews[0].Rating);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CopyOperation_Works()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var input = "[{ 'op': 'copy', 'path': 'Reviews/1/Rating', 'from': 'Reviews/0/Rating'}]".Replace("'", "\"");
|
||||||
|
var request = GetPatchRequest(input);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await Client.SendAsync(request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
var body = await response.Content.ReadAsStringAsync();
|
||||||
|
var product = JsonConvert.DeserializeObject<Product>(body);
|
||||||
|
Assert.NotNull(product);
|
||||||
|
Assert.NotNull(product.Reviews);
|
||||||
|
Assert.Equal(2, product.Reviews.Count);
|
||||||
|
Assert.Equal(4, product.Reviews[1].Rating);
|
||||||
|
Assert.Equal(4, product.Reviews[0].Rating);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task MoveOperation_Works()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var input = "[{ 'op': 'move', 'path': 'Reviews/1/Rating', 'from': 'Reviews/0/Rating'}]".Replace("'", "\"");
|
||||||
|
var request = GetPatchRequest(input);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await Client.SendAsync(request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
var body = await response.Content.ReadAsStringAsync();
|
||||||
|
var product = JsonConvert.DeserializeObject<Product>(body);
|
||||||
|
Assert.NotNull(product);
|
||||||
|
Assert.NotNull(product.Reviews);
|
||||||
|
Assert.Equal(2, product.Reviews.Count);
|
||||||
|
Assert.Equal(4, product.Reviews[1].Rating);
|
||||||
|
Assert.Equal(0, product.Reviews[0].Rating);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task RemoveOperation_Works()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var input = "[{ 'op': 'remove', 'path': 'Reviews/0/Rating'}]".Replace("'", "\"");
|
||||||
|
var request = GetPatchRequest(input);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await Client.SendAsync(request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
var body = await response.Content.ReadAsStringAsync();
|
||||||
|
var product = JsonConvert.DeserializeObject<Product>(body);
|
||||||
|
Assert.NotNull(product);
|
||||||
|
Assert.NotNull(product.Reviews);
|
||||||
|
Assert.Equal(2, product.Reviews.Count);
|
||||||
|
Assert.Equal(0, product.Reviews[0].Rating);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task AddOperation_InvalidValueForProperty_AddsErrorToModelState()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var input = "[{ 'op': 'add', 'path': 'Reviews/-', 'value': { 'Rating': 'not-a-double' }}]".Replace("'", "\"");
|
||||||
|
var request = GetPatchRequest(input);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await Client.SendAsync(request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
private HttpRequestMessage GetPatchRequest(string body)
|
||||||
|
{
|
||||||
|
return new HttpRequestMessage
|
||||||
|
{
|
||||||
|
Content = new StringContent(body, Encoding.UTF8, "application/json-patch+json"),
|
||||||
|
Method = new HttpMethod("PATCH"),
|
||||||
|
RequestUri = new Uri("http://localhost/jsonpatch/PatchProduct")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,51 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Microsoft.AspNetCore.JsonPatch;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace FormatterWebSite.Controllers
|
||||||
|
{
|
||||||
|
[Route("jsonpatch/[action]")]
|
||||||
|
public class JsonPatchController : Controller
|
||||||
|
{
|
||||||
|
[HttpPatch]
|
||||||
|
public IActionResult PatchProduct([FromBody] JsonPatchDocument<Product> patchDoc)
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
return BadRequest(ModelState);
|
||||||
|
}
|
||||||
|
|
||||||
|
var product = CreateProduct();
|
||||||
|
patchDoc.ApplyTo(product, ModelState);
|
||||||
|
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
return BadRequest(ModelState);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(product);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Product CreateProduct()
|
||||||
|
{
|
||||||
|
return new Product
|
||||||
|
{
|
||||||
|
Name = "Book1",
|
||||||
|
Reviews = new List<Review>()
|
||||||
|
{
|
||||||
|
new Review
|
||||||
|
{
|
||||||
|
Rating = 4
|
||||||
|
},
|
||||||
|
new Review
|
||||||
|
{
|
||||||
|
Rating = 3
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace FormatterWebSite
|
||||||
|
{
|
||||||
|
public class Product
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public List<Review> Reviews { get; set; } = new List<Review>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
namespace FormatterWebSite
|
||||||
|
{
|
||||||
|
public class Review
|
||||||
|
{
|
||||||
|
public double Rating { get; set; }
|
||||||
|
|
||||||
|
public string Description { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue