Skip to content

Commit c47b9be

Browse files
test: address PR feedback on startup order and redirects
1 parent 2f9c831 commit c47b9be

3 files changed

Lines changed: 13 additions & 5 deletions

File tree

EssentialCSharp.Web.Tests/FunctionalTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class FunctionalTests : IntegrationTestBase
1313
[Arguments("/alive")]
1414
public async Task WhenTheApplicationStarts_ItCanLoadLoadPages(string relativeUrl)
1515
{
16-
using HttpResponseMessage response = await GetWithRedirectsAsync(relativeUrl);
16+
using HttpResponseMessage response = await GetFollowingRedirectsAsync(relativeUrl);
1717

1818
await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK);
1919
}
@@ -28,7 +28,7 @@ public async Task WhenTheApplicationStarts_ItCanLoadLoadPages(string relativeUrl
2828
[Arguments("/about?someOtherParam=value")]
2929
public async Task WhenPagesAreAccessed_TheyReturnHtml(string relativeUrl)
3030
{
31-
using HttpResponseMessage response = await GetWithRedirectsAsync(relativeUrl);
31+
using HttpResponseMessage response = await GetFollowingRedirectsAsync(relativeUrl);
3232

3333
await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK);
3434

@@ -43,7 +43,7 @@ public async Task WhenPagesAreAccessed_TheyReturnHtml(string relativeUrl)
4343
[Test]
4444
public async Task WhenTheApplicationStarts_NonExistingPage_GivesCorrectStatusCode()
4545
{
46-
using HttpResponseMessage response = await GetWithRedirectsAsync("/non-existing-page1234");
46+
using HttpResponseMessage response = await GetFollowingRedirectsAsync("/non-existing-page1234");
4747

4848
await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.NotFound);
4949
}

EssentialCSharp.Web.Tests/IntegrationTestBase.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ public abstract class IntegrationTestBase : WebApplicationTest<WebApplicationFac
99
/// <summary>
1010
/// Executes a GET request and follows redirect responses while preserving
1111
/// TUnit trace correlation by using <see cref="TracedWebApplicationFactory{T}.CreateClient()"/>.
12+
/// This helper intentionally follows redirects using GET requests only.
1213
/// </summary>
13-
protected async Task<HttpResponseMessage> GetWithRedirectsAsync(string relativeUrl, int maxRedirects = 10)
14+
protected async Task<HttpResponseMessage> GetFollowingRedirectsAsync(string relativeUrl, int maxRedirects = 10)
1415
{
1516
HttpClient client = Factory.CreateClient();
1617
HttpResponseMessage response = await client.GetAsync(relativeUrl);
@@ -30,6 +31,12 @@ protected async Task<HttpResponseMessage> GetWithRedirectsAsync(string relativeU
3031
response = await client.GetAsync(location);
3132
}
3233

34+
if (IsRedirectStatusCode(response.StatusCode))
35+
{
36+
throw new InvalidOperationException(
37+
$"Exceeded redirect limit ({maxRedirects}) for '{relativeUrl}'. Last status: {(int)response.StatusCode} {response.StatusCode}.");
38+
}
39+
3340
return response;
3441
}
3542

EssentialCSharp.Web.Tests/WebApplicationFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
5656
options.UseSqlite(dbConnection);
5757
});
5858

59-
services.AddHostedService<EnsureCreatedHostedService>();
59+
// Ensure schema exists before any other hosted service that reads from the database.
60+
services.Insert(0, ServiceDescriptor.Singleton<IHostedService, EnsureCreatedHostedService>());
6061

6162
// Replace IListingSourceCodeService with one backed by TestData
6263
services.RemoveAll<IListingSourceCodeService>();

0 commit comments

Comments
 (0)