|
| 1 | +using System.Linq; |
| 2 | +using System.Net; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using FluentAssertions; |
| 5 | +using JsonApiDotNetCore.Serialization.Objects; |
| 6 | +using TestBuildingBlocks; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace JsonApiDotNetCoreExampleTests.IntegrationTests.HostingInIIS |
| 10 | +{ |
| 11 | + public sealed class HostingTests |
| 12 | + : IClassFixture<ExampleIntegrationTestContext<HostingStartup<HostingDbContext>, HostingDbContext>> |
| 13 | + { |
| 14 | + private const string HostPrefix = "http://localhost"; |
| 15 | + |
| 16 | + private readonly ExampleIntegrationTestContext<HostingStartup<HostingDbContext>, HostingDbContext> _testContext; |
| 17 | + private readonly HostingFakers _fakers = new HostingFakers(); |
| 18 | + |
| 19 | + public HostingTests(ExampleIntegrationTestContext<HostingStartup<HostingDbContext>, HostingDbContext> testContext) |
| 20 | + { |
| 21 | + _testContext = testContext; |
| 22 | + } |
| 23 | + |
| 24 | + [Fact] |
| 25 | + public async Task Get_primary_resources_with_include_returns_links() |
| 26 | + { |
| 27 | + // Arrange |
| 28 | + var gallery = _fakers.ArtGallery.Generate(); |
| 29 | + gallery.Paintings = _fakers.Painting.Generate(1).ToHashSet(); |
| 30 | + |
| 31 | + await _testContext.RunOnDatabaseAsync(async dbContext => |
| 32 | + { |
| 33 | + await dbContext.ClearTableAsync<ArtGallery>(); |
| 34 | + dbContext.ArtGalleries.Add(gallery); |
| 35 | + await dbContext.SaveChangesAsync(); |
| 36 | + }); |
| 37 | + |
| 38 | + const string route = "/iis-application-virtual-directory/public-api/artGalleries?include=paintings"; |
| 39 | + |
| 40 | + // Act |
| 41 | + var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<Document>(route); |
| 42 | + |
| 43 | + // Assert |
| 44 | + httpResponse.Should().HaveStatusCode(HttpStatusCode.OK); |
| 45 | + |
| 46 | + responseDocument.Links.Self.Should().Be(HostPrefix + route); |
| 47 | + responseDocument.Links.Related.Should().BeNull(); |
| 48 | + responseDocument.Links.First.Should().Be(HostPrefix + route); |
| 49 | + responseDocument.Links.Last.Should().Be(HostPrefix + route); |
| 50 | + responseDocument.Links.Prev.Should().BeNull(); |
| 51 | + responseDocument.Links.Next.Should().BeNull(); |
| 52 | + |
| 53 | + string galleryLink = HostPrefix + $"/iis-application-virtual-directory/public-api/artGalleries/{gallery.StringId}"; |
| 54 | + |
| 55 | + responseDocument.ManyData.Should().HaveCount(1); |
| 56 | + responseDocument.ManyData[0].Links.Self.Should().Be(galleryLink); |
| 57 | + responseDocument.ManyData[0].Relationships["paintings"].Links.Self.Should().Be(galleryLink + "/relationships/paintings"); |
| 58 | + responseDocument.ManyData[0].Relationships["paintings"].Links.Related.Should().Be(galleryLink + "/paintings"); |
| 59 | + |
| 60 | + // TODO: The next link is wrong: it should use the custom route. |
| 61 | + // https://github.com/json-api-dotnet/JsonApiDotNetCore/issues/956 |
| 62 | + string paintingLink = HostPrefix + $"/iis-application-virtual-directory/public-api/paintings/{gallery.Paintings.ElementAt(0).StringId}"; |
| 63 | + |
| 64 | + responseDocument.Included.Should().HaveCount(1); |
| 65 | + responseDocument.Included[0].Links.Self.Should().Be(paintingLink); |
| 66 | + responseDocument.Included[0].Relationships["exposedAt"].Links.Self.Should().Be(paintingLink + "/relationships/exposedAt"); |
| 67 | + responseDocument.Included[0].Relationships["exposedAt"].Links.Related.Should().Be(paintingLink + "/exposedAt"); |
| 68 | + } |
| 69 | + |
| 70 | + [Fact] |
| 71 | + public async Task Get_primary_resources_with_include_on_custom_route_returns_links() |
| 72 | + { |
| 73 | + // Arrange |
| 74 | + var painting = _fakers.Painting.Generate(); |
| 75 | + painting.ExposedAt = _fakers.ArtGallery.Generate(); |
| 76 | + |
| 77 | + await _testContext.RunOnDatabaseAsync(async dbContext => |
| 78 | + { |
| 79 | + await dbContext.ClearTableAsync<Painting>(); |
| 80 | + dbContext.Paintings.Add(painting); |
| 81 | + await dbContext.SaveChangesAsync(); |
| 82 | + }); |
| 83 | + |
| 84 | + const string route = "/iis-application-virtual-directory/custom/path/to/paintings?include=exposedAt"; |
| 85 | + |
| 86 | + // Act |
| 87 | + var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<Document>(route); |
| 88 | + |
| 89 | + // Assert |
| 90 | + httpResponse.Should().HaveStatusCode(HttpStatusCode.OK); |
| 91 | + |
| 92 | + responseDocument.Links.Self.Should().Be(HostPrefix + route); |
| 93 | + responseDocument.Links.Related.Should().BeNull(); |
| 94 | + responseDocument.Links.First.Should().Be(HostPrefix + route); |
| 95 | + responseDocument.Links.Last.Should().Be(HostPrefix + route); |
| 96 | + responseDocument.Links.Prev.Should().BeNull(); |
| 97 | + responseDocument.Links.Next.Should().BeNull(); |
| 98 | + |
| 99 | + string paintingLink = HostPrefix + $"/iis-application-virtual-directory/custom/path/to/paintings/{painting.StringId}"; |
| 100 | + |
| 101 | + responseDocument.ManyData.Should().HaveCount(1); |
| 102 | + responseDocument.ManyData[0].Links.Self.Should().Be(paintingLink); |
| 103 | + responseDocument.ManyData[0].Relationships["exposedAt"].Links.Self.Should().Be(paintingLink + "/relationships/exposedAt"); |
| 104 | + responseDocument.ManyData[0].Relationships["exposedAt"].Links.Related.Should().Be(paintingLink + "/exposedAt"); |
| 105 | + |
| 106 | + // TODO: The next link is wrong: it should not use the custom route. |
| 107 | + // https://github.com/json-api-dotnet/JsonApiDotNetCore/issues/956 |
| 108 | + string galleryLink = HostPrefix + $"/iis-application-virtual-directory/custom/path/to/artGalleries/{painting.ExposedAt.StringId}"; |
| 109 | + |
| 110 | + responseDocument.Included.Should().HaveCount(1); |
| 111 | + responseDocument.Included[0].Links.Self.Should().Be(galleryLink); |
| 112 | + responseDocument.Included[0].Relationships["paintings"].Links.Self.Should().Be(galleryLink + "/relationships/paintings"); |
| 113 | + responseDocument.Included[0].Relationships["paintings"].Links.Related.Should().Be(galleryLink + "/paintings"); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments