|
| 1 | +using System.Net; |
| 2 | +using System.Net.Http; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using DotNetCoreDocs; |
| 5 | +using DotNetCoreDocs.Writers; |
| 6 | +using JsonApiDotNetCoreExample; |
| 7 | +using Microsoft.AspNetCore.Hosting; |
| 8 | +using Microsoft.AspNetCore.TestHost; |
| 9 | +using Newtonsoft.Json; |
| 10 | +using Xunit; |
| 11 | +using Person = JsonApiDotNetCoreExample.Models.Person; |
| 12 | +using JsonApiDotNetCore.Models; |
| 13 | +using JsonApiDotNetCoreExample.Data; |
| 14 | +using Bogus; |
| 15 | +using JsonApiDotNetCoreExample.Models; |
| 16 | +using System.Linq; |
| 17 | +using System; |
| 18 | + |
| 19 | +namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec.DocumentTests |
| 20 | +{ |
| 21 | + [Collection("WebHostCollection")] |
| 22 | + public class PagingTests |
| 23 | + { |
| 24 | + private DocsFixture<Startup, JsonDocWriter> _fixture; |
| 25 | + private AppDbContext _context; |
| 26 | + private Faker<Person> _personFaker; |
| 27 | + private Faker<TodoItem> _todoItemFaker; |
| 28 | + private Faker<TodoItemCollection> _todoItemCollectionFaker; |
| 29 | + |
| 30 | + public PagingTests(DocsFixture<Startup, JsonDocWriter> fixture) |
| 31 | + { |
| 32 | + _fixture = fixture; |
| 33 | + _context = fixture.GetService<AppDbContext>(); |
| 34 | + _personFaker = new Faker<Person>() |
| 35 | + .RuleFor(p => p.FirstName, f => f.Name.FirstName()) |
| 36 | + .RuleFor(p => p.LastName, f => f.Name.LastName()); |
| 37 | + |
| 38 | + _todoItemFaker = new Faker<TodoItem>() |
| 39 | + .RuleFor(t => t.Description, f => f.Lorem.Sentence()) |
| 40 | + .RuleFor(t => t.Ordinal, f => f.Random.Number()); |
| 41 | + |
| 42 | + _todoItemCollectionFaker = new Faker<TodoItemCollection>() |
| 43 | + .RuleFor(t => t.Name, f => f.Company.CatchPhrase()); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public async Task Server_IncludesPagination_Links() |
| 48 | + { |
| 49 | + // arrange |
| 50 | + var pageSize = 5; |
| 51 | + var numberOfTodoItems = _context.TodoItems.Count(); |
| 52 | + var numberOfPages = (int)Math.Ceiling(decimal.Divide(numberOfTodoItems, pageSize)); |
| 53 | + var startPageNumber = 2; |
| 54 | + |
| 55 | + var builder = new WebHostBuilder() |
| 56 | + .UseStartup<Startup>(); |
| 57 | + |
| 58 | + var httpMethod = new HttpMethod("GET"); |
| 59 | + var route = $"/api/v1/todo-items?page[number]=2"; |
| 60 | + |
| 61 | + var server = new TestServer(builder); |
| 62 | + var client = server.CreateClient(); |
| 63 | + var request = new HttpRequestMessage(httpMethod, route); |
| 64 | + |
| 65 | + // act |
| 66 | + var response = await client.SendAsync(request); |
| 67 | + var documents = JsonConvert.DeserializeObject<Documents>(await response.Content.ReadAsStringAsync()); |
| 68 | + var links = documents.Links; |
| 69 | + |
| 70 | + // assert |
| 71 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 72 | + Assert.NotEmpty(links.First); |
| 73 | + Assert.NotEmpty(links.Next); |
| 74 | + Assert.NotEmpty(links.Last); |
| 75 | + |
| 76 | + Assert.Equal($"http://localhost/api/v1/todo-items?page[size]={pageSize}&page[number]={startPageNumber+1}", links.Next); |
| 77 | + Assert.Equal($"http://localhost/api/v1/todo-items?page[size]={pageSize}&page[number]={startPageNumber-1}", links.Prev); |
| 78 | + Assert.Equal($"http://localhost/api/v1/todo-items?page[size]={pageSize}&page[number]={numberOfPages}", links.Last); |
| 79 | + Assert.Equal($"http://localhost/api/v1/todo-items?page[size]={pageSize}&page[number]=1", links.First); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments