|
| 1 | +using FluentAssertions; |
| 2 | +using Microsoft.Kiota.Http.HttpClientLibrary; |
| 3 | +using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options; |
| 4 | +using Microsoft.Net.Http.Headers; |
| 5 | +using OpenApiKiotaEndToEndTests.Headers.GeneratedCode; |
| 6 | +using OpenApiKiotaEndToEndTests.Headers.GeneratedCode.Models; |
| 7 | +using OpenApiTests; |
| 8 | +using OpenApiTests.Headers; |
| 9 | +using TestBuildingBlocks; |
| 10 | +using Xunit; |
| 11 | +using Xunit.Abstractions; |
| 12 | + |
| 13 | +namespace OpenApiKiotaEndToEndTests.Headers; |
| 14 | + |
| 15 | +public sealed class HeaderTests : IClassFixture<IntegrationTestContext<OpenApiStartup<HeaderDbContext>, HeaderDbContext>> |
| 16 | +{ |
| 17 | + private readonly IntegrationTestContext<OpenApiStartup<HeaderDbContext>, HeaderDbContext> _testContext; |
| 18 | + private readonly TestableHttpClientRequestAdapterFactory _requestAdapterFactory; |
| 19 | + private readonly HeaderFakers _fakers = new(); |
| 20 | + |
| 21 | + public HeaderTests(IntegrationTestContext<OpenApiStartup<HeaderDbContext>, HeaderDbContext> testContext, ITestOutputHelper testOutputHelper) |
| 22 | + { |
| 23 | + _testContext = testContext; |
| 24 | + _requestAdapterFactory = new TestableHttpClientRequestAdapterFactory(testOutputHelper); |
| 25 | + |
| 26 | + testContext.UseController<CountriesController>(); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public async Task Returns_Location_for_post_resource_request() |
| 31 | + { |
| 32 | + // Arrange |
| 33 | + Country newCountry = _fakers.Country.Generate(); |
| 34 | + |
| 35 | + using HttpClientRequestAdapter requestAdapter = _requestAdapterFactory.CreateAdapter(_testContext.Factory); |
| 36 | + var apiClient = new HeadersClient(requestAdapter); |
| 37 | + |
| 38 | + var headerInspector = new HeadersInspectionHandlerOption |
| 39 | + { |
| 40 | + InspectResponseHeaders = true |
| 41 | + }; |
| 42 | + |
| 43 | + var requestBody = new CountryPostRequestDocument |
| 44 | + { |
| 45 | + Data = new CountryDataInPostRequest |
| 46 | + { |
| 47 | + Type = CountryResourceType.Countries, |
| 48 | + Attributes = new CountryAttributesInPostRequest |
| 49 | + { |
| 50 | + Name = newCountry.Name, |
| 51 | + Population = newCountry.Population |
| 52 | + } |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + // Act |
| 57 | + CountryPrimaryResponseDocument? response = |
| 58 | + await apiClient.Countries.PostAsync(requestBody, configuration => configuration.Options.Add(headerInspector)); |
| 59 | + |
| 60 | + // Assert |
| 61 | + response.ShouldNotBeNull(); |
| 62 | + response.Data.ShouldNotBeNull(); |
| 63 | + |
| 64 | + string[] locationHeaderValues = headerInspector.ResponseHeaders.Should().ContainKey(HeaderNames.Location).WhoseValue.ToArray(); |
| 65 | + locationHeaderValues.ShouldHaveCount(1); |
| 66 | + locationHeaderValues[0].Should().Be($"/countries/{response.Data.Id}"); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public async Task Returns_ContentLength_for_head_primary_resources_request() |
| 71 | + { |
| 72 | + // Arrange |
| 73 | + Country existingCountry = _fakers.Country.Generate(); |
| 74 | + |
| 75 | + await _testContext.RunOnDatabaseAsync(async dbContext => |
| 76 | + { |
| 77 | + await dbContext.ClearTableAsync<Country>(); |
| 78 | + dbContext.Countries.Add(existingCountry); |
| 79 | + await dbContext.SaveChangesAsync(); |
| 80 | + }); |
| 81 | + |
| 82 | + using HttpClientRequestAdapter requestAdapter = _requestAdapterFactory.CreateAdapter(_testContext.Factory); |
| 83 | + var apiClient = new HeadersClient(requestAdapter); |
| 84 | + |
| 85 | + var headerInspector = new HeadersInspectionHandlerOption |
| 86 | + { |
| 87 | + InspectResponseHeaders = true |
| 88 | + }; |
| 89 | + |
| 90 | + // Act |
| 91 | + Stream? response = await apiClient.Countries.HeadAsync(configuration => configuration.Options.Add(headerInspector)); |
| 92 | + |
| 93 | + // Assert |
| 94 | + response.Should().BeNull(); |
| 95 | + |
| 96 | + string[] contentLengthHeaderValues = headerInspector.ResponseHeaders.Should().ContainKey(HeaderNames.ContentLength).WhoseValue.ToArray(); |
| 97 | + contentLengthHeaderValues.ShouldHaveCount(1); |
| 98 | + long.Parse(contentLengthHeaderValues[0]).Should().BeGreaterThan(0); |
| 99 | + } |
| 100 | + |
| 101 | + [Fact] |
| 102 | + public async Task Returns_ContentLength_for_head_primary_resource_request() |
| 103 | + { |
| 104 | + // Arrange |
| 105 | + Country existingCountry = _fakers.Country.Generate(); |
| 106 | + |
| 107 | + await _testContext.RunOnDatabaseAsync(async dbContext => |
| 108 | + { |
| 109 | + dbContext.Countries.Add(existingCountry); |
| 110 | + await dbContext.SaveChangesAsync(); |
| 111 | + }); |
| 112 | + |
| 113 | + using HttpClientRequestAdapter requestAdapter = _requestAdapterFactory.CreateAdapter(_testContext.Factory); |
| 114 | + var apiClient = new HeadersClient(requestAdapter); |
| 115 | + |
| 116 | + var headerInspector = new HeadersInspectionHandlerOption |
| 117 | + { |
| 118 | + InspectResponseHeaders = true |
| 119 | + }; |
| 120 | + |
| 121 | + // Act |
| 122 | + Stream? response = await apiClient.Countries[existingCountry.StringId].HeadAsync(configuration => configuration.Options.Add(headerInspector)); |
| 123 | + |
| 124 | + // Assert |
| 125 | + response.Should().BeNull(); |
| 126 | + |
| 127 | + string[] contentLengthHeaderValues = headerInspector.ResponseHeaders.Should().ContainKey(HeaderNames.ContentLength).WhoseValue.ToArray(); |
| 128 | + contentLengthHeaderValues.ShouldHaveCount(1); |
| 129 | + long.Parse(contentLengthHeaderValues[0]).Should().BeGreaterThan(0); |
| 130 | + } |
| 131 | + |
| 132 | + [Fact] |
| 133 | + public async Task Returns_ContentLength_for_head_secondary_resource_request() |
| 134 | + { |
| 135 | + // Arrange |
| 136 | + Country existingCountry = _fakers.Country.Generate(); |
| 137 | + existingCountry.Languages = _fakers.Language.Generate(1).ToHashSet(); |
| 138 | + |
| 139 | + await _testContext.RunOnDatabaseAsync(async dbContext => |
| 140 | + { |
| 141 | + dbContext.Countries.Add(existingCountry); |
| 142 | + await dbContext.SaveChangesAsync(); |
| 143 | + }); |
| 144 | + |
| 145 | + using HttpClientRequestAdapter requestAdapter = _requestAdapterFactory.CreateAdapter(_testContext.Factory); |
| 146 | + var apiClient = new HeadersClient(requestAdapter); |
| 147 | + |
| 148 | + var headerInspector = new HeadersInspectionHandlerOption |
| 149 | + { |
| 150 | + InspectResponseHeaders = true |
| 151 | + }; |
| 152 | + |
| 153 | + // Act |
| 154 | + Stream? response = await apiClient.Countries[existingCountry.StringId].Languages.HeadAsync(configuration => configuration.Options.Add(headerInspector)); |
| 155 | + |
| 156 | + // Assert |
| 157 | + response.Should().BeNull(); |
| 158 | + |
| 159 | + string[] contentLengthHeaderValues = headerInspector.ResponseHeaders.Should().ContainKey(HeaderNames.ContentLength).WhoseValue.ToArray(); |
| 160 | + contentLengthHeaderValues.ShouldHaveCount(1); |
| 161 | + long.Parse(contentLengthHeaderValues[0]).Should().BeGreaterThan(0); |
| 162 | + } |
| 163 | + |
| 164 | + [Fact] |
| 165 | + public async Task Returns_ContentLength_for_head_relationship_request() |
| 166 | + { |
| 167 | + // Arrange |
| 168 | + Country existingCountry = _fakers.Country.Generate(); |
| 169 | + existingCountry.Languages = _fakers.Language.Generate(1).ToHashSet(); |
| 170 | + |
| 171 | + await _testContext.RunOnDatabaseAsync(async dbContext => |
| 172 | + { |
| 173 | + dbContext.Countries.Add(existingCountry); |
| 174 | + await dbContext.SaveChangesAsync(); |
| 175 | + }); |
| 176 | + |
| 177 | + using HttpClientRequestAdapter requestAdapter = _requestAdapterFactory.CreateAdapter(_testContext.Factory); |
| 178 | + var apiClient = new HeadersClient(requestAdapter); |
| 179 | + |
| 180 | + var headerInspector = new HeadersInspectionHandlerOption |
| 181 | + { |
| 182 | + InspectResponseHeaders = true |
| 183 | + }; |
| 184 | + |
| 185 | + // Act |
| 186 | + Stream? response = await apiClient.Countries[existingCountry.StringId].Relationships.Languages |
| 187 | + .HeadAsync(configuration => configuration.Options.Add(headerInspector)); |
| 188 | + |
| 189 | + // Assert |
| 190 | + response.Should().BeNull(); |
| 191 | + |
| 192 | + string[] contentLengthHeaderValues = headerInspector.ResponseHeaders.Should().ContainKey(HeaderNames.ContentLength).WhoseValue.ToArray(); |
| 193 | + contentLengthHeaderValues.ShouldHaveCount(1); |
| 194 | + long.Parse(contentLengthHeaderValues[0]).Should().BeGreaterThan(0); |
| 195 | + } |
| 196 | +} |
0 commit comments