|
1 | 1 | using System.Collections;
|
2 | 2 | using System.Net;
|
3 | 3 | using System.Net.Http;
|
| 4 | +using System.Net.Http.Headers; |
4 | 5 | using System.Threading.Tasks;
|
5 | 6 | using JsonApiDotNetCore.Models;
|
6 | 7 | using JsonApiDotNetCoreExample;
|
@@ -54,6 +55,119 @@ public async Task Total_Record_Count_Included()
|
54 | 55 | Assert.Equal((long)expectedCount, (long)documents.Meta["total-records"]);
|
55 | 56 | }
|
56 | 57 |
|
| 58 | + [Fact] |
| 59 | + public async Task Total_Record_Count_Not_Included_When_None() |
| 60 | + { |
| 61 | + // arrange |
| 62 | + _context.TodoItems.RemoveRange(_context.TodoItems); |
| 63 | + _context.SaveChanges(); |
| 64 | + var builder = new WebHostBuilder() |
| 65 | + .UseStartup<MetaStartup>(); |
| 66 | + |
| 67 | + var httpMethod = new HttpMethod("GET"); |
| 68 | + var route = $"/api/v1/todo-items"; |
| 69 | + |
| 70 | + var server = new TestServer(builder); |
| 71 | + var client = server.CreateClient(); |
| 72 | + var request = new HttpRequestMessage(httpMethod, route); |
| 73 | + |
| 74 | + // act |
| 75 | + var response = await client.SendAsync(request); |
| 76 | + var responseBody = await response.Content.ReadAsStringAsync(); |
| 77 | + var documents = JsonConvert.DeserializeObject<Documents>(responseBody); |
| 78 | + |
| 79 | + // assert |
| 80 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 81 | + Assert.Null(documents.Meta); |
| 82 | + } |
| 83 | + |
| 84 | + [Fact] |
| 85 | + public async Task Total_Record_Count_Included_POST() |
| 86 | + { |
| 87 | + // arrange |
| 88 | + _context.TodoItems.RemoveRange(_context.TodoItems); |
| 89 | + _context.SaveChanges(); |
| 90 | + var expectedCount = 1; |
| 91 | + var builder = new WebHostBuilder() |
| 92 | + .UseStartup<MetaStartup>(); |
| 93 | + |
| 94 | + var httpMethod = new HttpMethod("POST"); |
| 95 | + var route = $"/api/v1/todo-items"; |
| 96 | + |
| 97 | + var server = new TestServer(builder); |
| 98 | + var client = server.CreateClient(); |
| 99 | + var request = new HttpRequestMessage(httpMethod, route); |
| 100 | + var content = new |
| 101 | + { |
| 102 | + data = new |
| 103 | + { |
| 104 | + type = "todo-items", |
| 105 | + attributes = new |
| 106 | + { |
| 107 | + description = "New Description", |
| 108 | + } |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + request.Content = new StringContent(JsonConvert.SerializeObject(content)); |
| 113 | + request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json"); |
| 114 | + |
| 115 | + // act |
| 116 | + var response = await client.SendAsync(request); |
| 117 | + var responseBody = await response.Content.ReadAsStringAsync(); |
| 118 | + var documents = JsonConvert.DeserializeObject<Document>(responseBody); |
| 119 | + |
| 120 | + // assert |
| 121 | + Assert.Equal(HttpStatusCode.Created, response.StatusCode); |
| 122 | + Assert.NotNull(documents.Meta); |
| 123 | + Assert.Equal((long)expectedCount, (long)documents.Meta["total-records"]); |
| 124 | + } |
| 125 | + |
| 126 | + [Fact] |
| 127 | + public async Task Total_Record_Count_Included_PATCH() |
| 128 | + { |
| 129 | + // arrange |
| 130 | + _context.TodoItems.RemoveRange(_context.TodoItems); |
| 131 | + TodoItem todoItem = new TodoItem(); |
| 132 | + _context.TodoItems.Add(todoItem); |
| 133 | + _context.SaveChanges(); |
| 134 | + var expectedCount = 1; |
| 135 | + var builder = new WebHostBuilder() |
| 136 | + .UseStartup<MetaStartup>(); |
| 137 | + |
| 138 | + var httpMethod = new HttpMethod("PATCH"); |
| 139 | + var route = $"/api/v1/todo-items/{todoItem.Id}"; |
| 140 | + |
| 141 | + var server = new TestServer(builder); |
| 142 | + var client = server.CreateClient(); |
| 143 | + var request = new HttpRequestMessage(httpMethod, route); |
| 144 | + var content = new |
| 145 | + { |
| 146 | + data = new |
| 147 | + { |
| 148 | + type = "todo-items", |
| 149 | + id = todoItem.Id, |
| 150 | + attributes = new |
| 151 | + { |
| 152 | + description = "New Description", |
| 153 | + } |
| 154 | + } |
| 155 | + }; |
| 156 | + |
| 157 | + request.Content = new StringContent(JsonConvert.SerializeObject(content)); |
| 158 | + request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json"); |
| 159 | + |
| 160 | + // act |
| 161 | + var response = await client.SendAsync(request); |
| 162 | + var responseBody = await response.Content.ReadAsStringAsync(); |
| 163 | + var documents = JsonConvert.DeserializeObject<Document>(responseBody); |
| 164 | + |
| 165 | + // assert |
| 166 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 167 | + Assert.NotNull(documents.Meta); |
| 168 | + Assert.Equal((long)expectedCount, (long)documents.Meta["total-records"]); |
| 169 | + } |
| 170 | + |
57 | 171 | [Fact]
|
58 | 172 | public async Task EntityThatImplements_IHasMeta_Contains_MetaData()
|
59 | 173 | {
|
|
0 commit comments