|
| 1 | +using System.Net; |
| 2 | +using System.Text.Json; |
| 3 | +using JsonApiToolkit.Extensions; |
| 4 | +using JsonApiToolkit.Models.Documents; |
| 5 | +using JsonApiToolkit.Models.Errors; |
| 6 | +using JsonApiToolkit.Models.Resources; |
| 7 | +using Microsoft.AspNetCore.Builder; |
| 8 | +using Microsoft.AspNetCore.Hosting; |
| 9 | +using Microsoft.AspNetCore.TestHost; |
| 10 | +using Microsoft.EntityFrameworkCore; |
| 11 | +using Microsoft.Extensions.DependencyInjection; |
| 12 | +using Microsoft.Extensions.Hosting; |
| 13 | + |
| 14 | +namespace JsonApiToolkit.Tests.Integration; |
| 15 | + |
| 16 | +/// <summary> |
| 17 | +/// Integration tests for runtime strict-pagination behavior (page > totalPages → 404). |
| 18 | +/// Re-uses the QueryTestArticle/QueryTestDbContext fixtures from JsonApiQueryAsyncTests. |
| 19 | +/// </summary> |
| 20 | +public class StrictPaginationIntegrationTests : IDisposable |
| 21 | +{ |
| 22 | + private readonly IHost _host; |
| 23 | + private readonly HttpClient _client; |
| 24 | + private readonly JsonSerializerOptions _jsonOptions = new() |
| 25 | + { |
| 26 | + PropertyNameCaseInsensitive = true, |
| 27 | + }; |
| 28 | + |
| 29 | + public StrictPaginationIntegrationTests() |
| 30 | + { |
| 31 | + var databaseName = $"StrictPaginationTestDb_{Guid.NewGuid()}"; |
| 32 | + |
| 33 | + _host = new HostBuilder() |
| 34 | + .ConfigureWebHost(webBuilder => |
| 35 | + { |
| 36 | + webBuilder |
| 37 | + .UseTestServer() |
| 38 | + .ConfigureServices(services => |
| 39 | + { |
| 40 | + services.AddDbContext<QueryTestDbContext>(options => |
| 41 | + options.UseInMemoryDatabase(databaseName) |
| 42 | + ); |
| 43 | + services.AddControllers(); |
| 44 | + services.AddJsonApiToolkit(options => |
| 45 | + { |
| 46 | + options.StrictPagination = true; |
| 47 | + }); |
| 48 | + }) |
| 49 | + .Configure(app => |
| 50 | + { |
| 51 | + app.UseRouting(); |
| 52 | + app.UseEndpoints(endpoints => endpoints.MapControllers()); |
| 53 | + |
| 54 | + using var scope = app.ApplicationServices.CreateScope(); |
| 55 | + var context = |
| 56 | + scope.ServiceProvider.GetRequiredService<QueryTestDbContext>(); |
| 57 | + SeedFiveArticles(context); |
| 58 | + }); |
| 59 | + }) |
| 60 | + .Build(); |
| 61 | + |
| 62 | + _host.Start(); |
| 63 | + _client = _host.GetTestClient(); |
| 64 | + } |
| 65 | + |
| 66 | + private static void SeedFiveArticles(QueryTestDbContext context) |
| 67 | + { |
| 68 | + for (int i = 1; i <= 5; i++) |
| 69 | + { |
| 70 | + context.Articles.Add( |
| 71 | + new QueryTestArticle |
| 72 | + { |
| 73 | + Id = i, |
| 74 | + Title = $"Article {i}", |
| 75 | + Content = $"Content {i}", |
| 76 | + CreatedAt = new DateTime(2024, 1, i), |
| 77 | + IsPublished = true, |
| 78 | + ViewCount = i * 10, |
| 79 | + } |
| 80 | + ); |
| 81 | + } |
| 82 | + context.SaveChanges(); |
| 83 | + } |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public async Task PageBeyondTotal_Returns404Async() |
| 87 | + { |
| 88 | + // 5 articles, page size 2 → 3 total pages. Page 100 must 404. |
| 89 | + var response = await _client.GetAsync("/api/articles?page[number]=100&page[size]=2"); |
| 90 | + |
| 91 | + Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public async Task PageBeyondTotal_ErrorBodyHasMetaAsync() |
| 96 | + { |
| 97 | + var response = await _client.GetAsync("/api/articles?page[number]=10&page[size]=2"); |
| 98 | + |
| 99 | + Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); |
| 100 | + |
| 101 | + var content = await response.Content.ReadAsStringAsync(); |
| 102 | + var doc = JsonSerializer.Deserialize<JsonApiErrorResponse>(content, _jsonOptions); |
| 103 | + |
| 104 | + Assert.NotNull(doc?.Errors); |
| 105 | + var error = Assert.Single(doc.Errors); |
| 106 | + Assert.Equal("404", error.Status); |
| 107 | + Assert.Equal(JsonApiErrorCodes.InvalidPageNumber, error.Code); |
| 108 | + Assert.Equal("page[number]", error.Source?.Parameter); |
| 109 | + Assert.NotNull(error.Meta); |
| 110 | + Assert.Equal(10, GetIntFromMeta(error.Meta, "value")); |
| 111 | + Assert.Equal(3, GetIntFromMeta(error.Meta, "totalPages")); |
| 112 | + Assert.Equal(5, GetIntFromMeta(error.Meta, "totalResources")); |
| 113 | + } |
| 114 | + |
| 115 | + [Fact] |
| 116 | + public async Task LastPage_Returns200Async() |
| 117 | + { |
| 118 | + // Exactly the last page must succeed (boundary check: > not >=). |
| 119 | + var response = await _client.GetAsync("/api/articles?page[number]=3&page[size]=2&sort=id"); |
| 120 | + |
| 121 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 122 | + |
| 123 | + var content = await response.Content.ReadAsStringAsync(); |
| 124 | + var doc = JsonSerializer.Deserialize<JsonApiCollectionDocument<ResourceObject>>( |
| 125 | + content, |
| 126 | + _jsonOptions |
| 127 | + ); |
| 128 | + |
| 129 | + Assert.NotNull(doc?.Data); |
| 130 | + Assert.Single(doc.Data); |
| 131 | + Assert.Equal("5", doc.Data.First().Id); |
| 132 | + } |
| 133 | + |
| 134 | + [Fact] |
| 135 | + public async Task EmptyResultWithPaging_DoesNotReturn404Async() |
| 136 | + { |
| 137 | + // Filter that matches no rows. With totalCount=0, strict mode must not 404 page=2 — |
| 138 | + // there are no pages to be wrong about. |
| 139 | + var response = await _client.GetAsync( |
| 140 | + "/api/articles?filter[title]=NoSuchArticle&page[number]=2&page[size]=10" |
| 141 | + ); |
| 142 | + |
| 143 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 144 | + |
| 145 | + var content = await response.Content.ReadAsStringAsync(); |
| 146 | + var doc = JsonSerializer.Deserialize<JsonApiCollectionDocument<ResourceObject>>( |
| 147 | + content, |
| 148 | + _jsonOptions |
| 149 | + ); |
| 150 | + |
| 151 | + Assert.NotNull(doc?.Data); |
| 152 | + Assert.Empty(doc.Data); |
| 153 | + } |
| 154 | + |
| 155 | + private static int GetIntFromMeta(Dictionary<string, object> meta, string key) |
| 156 | + { |
| 157 | + Assert.True(meta.TryGetValue(key, out var raw), $"Missing meta key '{key}'"); |
| 158 | + return raw switch |
| 159 | + { |
| 160 | + JsonElement e => e.GetInt32(), |
| 161 | + int i => i, |
| 162 | + long l => (int)l, |
| 163 | + _ => Convert.ToInt32(raw), |
| 164 | + }; |
| 165 | + } |
| 166 | + |
| 167 | + public void Dispose() |
| 168 | + { |
| 169 | + _client.Dispose(); |
| 170 | + _host.Dispose(); |
| 171 | + GC.SuppressFinalize(this); |
| 172 | + } |
| 173 | +} |
0 commit comments