Skip to content

Commit 93269b3

Browse files
test: address remaining PR review comments
1 parent c47b9be commit 93269b3

5 files changed

Lines changed: 37 additions & 9 deletions

File tree

EssentialCSharp.Web.Tests/ContentRateLimitingTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class ContentRateLimitingTests : IntegrationTestBase
1212
[Test]
1313
public async Task ContentEndpoint_ExceedingPerMinuteLimit_Returns429()
1414
{
15-
HttpClient client = Factory.CreateClient();
15+
using HttpClient client = CreateClientWithoutRedirectFollowing();
1616

1717
// Anonymous limit is 10/min. First 10 requests should not be rate-limited.
1818
for (int i = 0; i < 10; i++)

EssentialCSharp.Web.Tests/FunctionalTests.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public class FunctionalTests : IntegrationTestBase
1313
[Arguments("/alive")]
1414
public async Task WhenTheApplicationStarts_ItCanLoadLoadPages(string relativeUrl)
1515
{
16-
using HttpResponseMessage response = await GetFollowingRedirectsAsync(relativeUrl);
16+
using HttpClient client = Factory.CreateClient();
17+
using HttpResponseMessage response = await GetFollowingGetRedirectsAsync(client, relativeUrl);
1718

1819
await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK);
1920
}
@@ -28,7 +29,8 @@ public async Task WhenTheApplicationStarts_ItCanLoadLoadPages(string relativeUrl
2829
[Arguments("/about?someOtherParam=value")]
2930
public async Task WhenPagesAreAccessed_TheyReturnHtml(string relativeUrl)
3031
{
31-
using HttpResponseMessage response = await GetFollowingRedirectsAsync(relativeUrl);
32+
using HttpClient client = Factory.CreateClient();
33+
using HttpResponseMessage response = await GetFollowingGetRedirectsAsync(client, relativeUrl);
3234

3335
await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK);
3436

@@ -43,7 +45,8 @@ public async Task WhenPagesAreAccessed_TheyReturnHtml(string relativeUrl)
4345
[Test]
4446
public async Task WhenTheApplicationStarts_NonExistingPage_GivesCorrectStatusCode()
4547
{
46-
using HttpResponseMessage response = await GetFollowingRedirectsAsync("/non-existing-page1234");
48+
using HttpClient client = Factory.CreateClient();
49+
using HttpResponseMessage response = await GetFollowingGetRedirectsAsync(client, "/non-existing-page1234");
4750

4851
await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.NotFound);
4952
}

EssentialCSharp.Web.Tests/IntegrationTestBase.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Net;
2+
using Microsoft.AspNetCore.Mvc.Testing;
23
using Microsoft.Extensions.DependencyInjection;
34
using TUnit.AspNetCore;
45

@@ -11,9 +12,14 @@ public abstract class IntegrationTestBase : WebApplicationTest<WebApplicationFac
1112
/// TUnit trace correlation by using <see cref="TracedWebApplicationFactory{T}.CreateClient()"/>.
1213
/// This helper intentionally follows redirects using GET requests only.
1314
/// </summary>
14-
protected async Task<HttpResponseMessage> GetFollowingRedirectsAsync(string relativeUrl, int maxRedirects = 10)
15+
protected HttpClient CreateClientWithoutRedirectFollowing() =>
16+
Factory.Inner.CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = false });
17+
18+
protected static async Task<HttpResponseMessage> GetFollowingGetRedirectsAsync(
19+
HttpClient client,
20+
string relativeUrl,
21+
int maxRedirects = 10)
1522
{
16-
HttpClient client = Factory.CreateClient();
1723
HttpResponseMessage response = await client.GetAsync(relativeUrl);
1824

1925
for (int redirectCount = 0;

EssentialCSharp.Web.Tests/McpTestHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.Authentication;
77
using Microsoft.AspNetCore.Authentication.Cookies;
88
using Microsoft.AspNetCore.Identity;
9+
using Microsoft.AspNetCore.Mvc.Testing;
910
using Microsoft.Extensions.DependencyInjection;
1011
using Microsoft.Extensions.Options;
1112
using TUnit.AspNetCore;
@@ -15,7 +16,7 @@ namespace EssentialCSharp.Web.Tests;
1516
internal static class McpTestHelper
1617
{
1718
public static HttpClient CreateClient(TracedWebApplicationFactory<Program> factory) =>
18-
factory.CreateClient();
19+
factory.Inner.CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = false });
1920

2021
public static HttpRequestMessage CreateInitializeRequest(string path = "/mcp")
2122
{

EssentialCSharp.Web.Tests/WebApplicationFactory.cs

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

59-
// Ensure schema exists before any other hosted service that reads from the database.
60-
services.Insert(0, ServiceDescriptor.Singleton<IHostedService, EnsureCreatedHostedService>());
59+
// Ensure schema exists before any hosted service that reads from the database.
60+
ServiceDescriptor ensureCreatedDescriptor =
61+
ServiceDescriptor.Singleton<IHostedService, EnsureCreatedHostedService>();
62+
int firstHostedServiceIndex = -1;
63+
for (int i = 0; i < services.Count; i++)
64+
{
65+
if (services[i].ServiceType == typeof(IHostedService))
66+
{
67+
firstHostedServiceIndex = i;
68+
break;
69+
}
70+
}
71+
if (firstHostedServiceIndex >= 0)
72+
{
73+
services.Insert(firstHostedServiceIndex, ensureCreatedDescriptor);
74+
}
75+
else
76+
{
77+
services.Add(ensureCreatedDescriptor);
78+
}
6179

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

0 commit comments

Comments
 (0)