Skip to content

Commit 79c014b

Browse files
CopilotKeboo
andcommitted
Address PR review feedback: consolidate tests, improve dispose pattern, and restore error handling
Co-authored-by: Keboo <[email protected]>
1 parent 9387ce1 commit 79c014b

File tree

3 files changed

+18
-41
lines changed

3 files changed

+18
-41
lines changed

EssentialCSharp.Web.Tests/FunctionalTests.cs

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ public async Task WhenTheApplicationStarts_ItCanLoadLoadPages(string relativeUrl
2424
[InlineData("/guidelines?rid=test-referral-id")]
2525
[InlineData("/about?rid=abc123")]
2626
[InlineData("/hello-world?rid=user-referral")]
27-
public async Task WhenPagesAreAccessedWithRidParameter_TheyReturnContentSuccessfully(string relativeUrl)
27+
[InlineData("/guidelines?rid=")]
28+
[InlineData("/about?rid= ")]
29+
[InlineData("/guidelines?foo=bar")]
30+
[InlineData("/about?someOtherParam=value")]
31+
public async Task WhenPagesAreAccessed_TheyReturnHtml(string relativeUrl)
2832
{
2933
using WebApplicationFactory factory = new();
3034

@@ -41,23 +45,6 @@ public async Task WhenPagesAreAccessedWithRidParameter_TheyReturnContentSuccessf
4145
Assert.Contains("<html", content, StringComparison.OrdinalIgnoreCase);
4246
}
4347

44-
[Theory]
45-
[InlineData("/guidelines?rid=")]
46-
[InlineData("/about?rid= ")]
47-
public async Task WhenPagesAreAccessedWithEmptyRidParameter_TheyStillWork(string relativeUrl)
48-
{
49-
using WebApplicationFactory factory = new();
50-
51-
HttpClient client = factory.CreateClient();
52-
using HttpResponseMessage response = await client.GetAsync(relativeUrl);
53-
54-
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
55-
56-
string content = await response.Content.ReadAsStringAsync();
57-
Assert.NotEmpty(content);
58-
Assert.Contains("<html", content, StringComparison.OrdinalIgnoreCase);
59-
}
60-
6148
[Fact]
6249
public async Task WhenTheApplicationStarts_NonExistingPage_GivesCorrectStatusCode()
6350
{
@@ -68,21 +55,4 @@ public async Task WhenTheApplicationStarts_NonExistingPage_GivesCorrectStatusCod
6855

6956
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
7057
}
71-
72-
[Theory]
73-
[InlineData("/guidelines?foo=bar")]
74-
[InlineData("/about?someOtherParam=value")]
75-
public async Task WhenPagesAreAccessedWithNonRidParameters_TheyStillWork(string relativeUrl)
76-
{
77-
using WebApplicationFactory factory = new();
78-
79-
HttpClient client = factory.CreateClient();
80-
using HttpResponseMessage response = await client.GetAsync(relativeUrl);
81-
82-
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
83-
84-
string content = await response.Content.ReadAsStringAsync();
85-
Assert.NotEmpty(content);
86-
Assert.Contains("<html", content, StringComparison.OrdinalIgnoreCase);
87-
}
8858
}

EssentialCSharp.Web.Tests/WebApplicationFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
4545
protected override void Dispose(bool disposing)
4646
{
4747
base.Dispose(disposing);
48-
if (disposing && _Connection != null)
48+
if (disposing)
4949
{
50-
_Connection.Dispose();
50+
_Connection?.Dispose();
5151
_Connection = null;
5252
}
5353
}

EssentialCSharp.Web/Middleware/ReferralTrackingMiddleware.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,20 @@ public async Task InvokeAsync(HttpContext context, IReferralService referralServ
2727
return;
2828
}
2929

30-
if (context.User is { Identity.IsAuthenticated: true } claimsUser)
30+
try
3131
{
32-
referralService.TrackReferralAsync(referralId, claimsUser);
32+
if (context.User is { Identity.IsAuthenticated: true } claimsUser)
33+
{
34+
referralService.TrackReferralAsync(referralId, claimsUser);
35+
}
36+
else
37+
{
38+
referralService.TrackReferralAsync(referralId, null);
39+
}
3340
}
34-
else
41+
catch (Exception ex)
3542
{
36-
referralService.TrackReferralAsync(referralId, null);
43+
_Logger.LogError(ex, "Failed to track referral ID {ReferralId}", referralId);
3744
}
3845

3946
await _Next(context);

0 commit comments

Comments
 (0)