Skip to content

Commit 11424df

Browse files
committed
test(patch): server responds 404 if entity does not exist
1 parent ca569fb commit 11424df

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

src/JsonApiDotNetCore/Controllers/JsonApiController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ public virtual async Task<IActionResult> PatchAsync(TId id, [FromBody] T entity)
158158

159159
var updatedEntity = await _entities.UpdateAsync(id, entity);
160160

161+
if(updatedEntity == null) return NotFound();
162+
161163
return Ok(updatedEntity);
162164
}
163165

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Net;
4+
using System.Net.Http;
5+
using System.Net.Http.Headers;
6+
using System.Threading.Tasks;
7+
using Bogus;
8+
using DotNetCoreDocs;
9+
using DotNetCoreDocs.Writers;
10+
using JsonApiDotNetCore.Serialization;
11+
using JsonApiDotNetCore.Services;
12+
using JsonApiDotNetCoreExample;
13+
using JsonApiDotNetCoreExample.Data;
14+
using JsonApiDotNetCoreExample.Models;
15+
using Microsoft.AspNetCore.Hosting;
16+
using Microsoft.AspNetCore.TestHost;
17+
using Newtonsoft.Json;
18+
using Xunit;
19+
20+
namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec
21+
{
22+
[Collection("WebHostCollection")]
23+
public class UpdatingDataTests
24+
{
25+
private DocsFixture<Startup, JsonDocWriter> _fixture;
26+
private AppDbContext _context;
27+
private Faker<TodoItem> _todoItemFaker;
28+
29+
public UpdatingDataTests(DocsFixture<Startup, JsonDocWriter> fixture)
30+
{
31+
_fixture = fixture;
32+
_context = fixture.GetService<AppDbContext>();
33+
_todoItemFaker = new Faker<TodoItem>()
34+
.RuleFor(t => t.Description, f => f.Lorem.Sentence())
35+
.RuleFor(t => t.Ordinal, f => f.Random.Number());
36+
}
37+
38+
[Fact]
39+
public async Task Respond_404_If_EntityDoesNotExist()
40+
{
41+
// arrange
42+
var maxPersonId = _context.TodoItems.LastOrDefault()?.Id ?? 0;
43+
var todoItem = _todoItemFaker.Generate();
44+
var builder = new WebHostBuilder()
45+
.UseStartup<Startup>();
46+
47+
var server = new TestServer(builder);
48+
var client = server.CreateClient();
49+
50+
var content = new
51+
{
52+
data = new
53+
{
54+
type = "todo-items",
55+
attributes = new
56+
{
57+
description = todoItem.Description,
58+
ordinal = todoItem.Ordinal
59+
}
60+
}
61+
};
62+
63+
var httpMethod = new HttpMethod("PATCH");
64+
var route = $"/api/v1/todo-items/{maxPersonId + 100}";
65+
var request = new HttpRequestMessage(httpMethod, route);
66+
67+
request.Content = new StringContent(JsonConvert.SerializeObject(content));
68+
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
69+
70+
// Act
71+
var response = await client.SendAsync(request);
72+
73+
// Assert
74+
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)