Skip to content

Commit df0ca2f

Browse files
committed
Address request context review feedback
1 parent 8b2c333 commit df0ca2f

4 files changed

Lines changed: 24 additions & 24 deletions

File tree

src/PostHog.AspNetCore/Tracing/PostHogRequestContextMiddlewareExtensions.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ public static IApplicationBuilder UsePostHogRequestContext(
1717
this IApplicationBuilder app,
1818
Action<PostHogRequestContextOptions>? configure = null)
1919
{
20-
if (app is null)
21-
{
22-
return app!;
23-
}
20+
ArgumentNullException.ThrowIfNull(app);
2421

2522
var options = new PostHogRequestContextOptions();
2623
configure?.Invoke(options);
2724
return app.UseMiddleware<PostHogRequestContextMiddleware>(options);
2825
}
26+
2927
}

src/PostHog.AspNetCore/Tracing/PostHogTracingHeaders.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ public static PostHogRequestContextData Extract(HttpContext? httpContext, PostHo
7474
return null;
7575
}
7676

77-
var value = values.Count > 0 ? values[0] : values.ToString();
78-
return SanitizeValue(value);
77+
return SanitizeValue(values[0]);
7978
}
8079

8180
internal static string? SanitizeValue(string? value)
@@ -117,7 +116,8 @@ static void AddIfPresent(Dictionary<string, object> properties, string key, stri
117116
"://",
118117
request.Host.ToUriComponent(),
119118
request.PathBase.ToUriComponent(),
120-
request.Path.ToUriComponent());
119+
request.Path.ToUriComponent(),
120+
request.QueryString.ToUriComponent());
121121
}
122122

123123
static string? GetRequestPath(HttpRequest request)

tests/UnitTests.AspNetCore/PostHogRequestContextMiddlewareTests.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ public async Task NullHttpContextDoesNotThrow()
2020
await middleware.InvokeAsync(null);
2121
}
2222

23+
[Fact]
24+
public void NullApplicationBuilderThrowsArgumentNullException()
25+
{
26+
Assert.Throws<ArgumentNullException>(
27+
() => PostHogRequestContextMiddlewareExtensions.UsePostHogRequestContext(null!));
28+
}
29+
2330
[Fact]
2431
public void NullTracingExtractionInputsReturnEmptyContext()
2532
{
@@ -60,7 +67,7 @@ public async Task AppliesRequestContextHeadersAndRequestMetadataToDownstreamCapt
6067
Assert.Equal("frontend-user", batchItem.GetProperty("distinct_id").GetString());
6168
var properties = batchItem.GetProperty("properties");
6269
Assert.Equal("frontend-session", properties.GetProperty("$session_id").GetString());
63-
Assert.Equal("https://example.com/api/test", properties.GetProperty("$current_url").GetString());
70+
Assert.Equal("https://example.com/api/test?filter=1", properties.GetProperty("$current_url").GetString());
6471
Assert.Equal("POST", properties.GetProperty("$request_method").GetString());
6572
Assert.Equal("/api/test", properties.GetProperty("$request_path").GetString());
6673
Assert.Equal("TestAgent/1.0", properties.GetProperty("$user_agent").GetString());
@@ -255,7 +262,7 @@ public async Task CapturesUnhandledExceptionsWithRequestContextAndRethrows()
255262
Assert.Equal("exception-user", batchItem.GetProperty("distinct_id").GetString());
256263
var properties = batchItem.GetProperty("properties");
257264
Assert.Equal("exception-session", properties.GetProperty("$session_id").GetString());
258-
Assert.Equal("https://example.com/api/test", properties.GetProperty("$current_url").GetString());
265+
Assert.Equal("https://example.com/api/test?filter=1", properties.GetProperty("$current_url").GetString());
259266
Assert.Equal("ExceptionAgent/1.0", properties.GetProperty("$user_agent").GetString());
260267
Assert.Equal("10.0.0.2", properties.GetProperty("$ip").GetString());
261268
Assert.Equal(StatusCodes.Status503ServiceUnavailable, properties.GetProperty("$response_status_code").GetInt32());

tests/UnitTests/PostHogContextTests.cs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,26 +100,21 @@ public async Task ExplicitCaptureValuesOverrideContext()
100100
Assert.Equal("context-only-value", properties.GetProperty("context-only").GetString());
101101
}
102102

103-
[Fact]
104-
public void MissingDistinctIdCreatesPersonlessContext()
103+
[Theory]
104+
[InlineData(null, false)]
105+
[InlineData(true, true)]
106+
public void PersonlessContextSetsProcessPersonProfile(bool? explicitOverride, bool expectedValue)
105107
{
106-
var context = PostHogContextHelper.ResolveCaptureContext(distinctId: null, properties: null);
108+
var properties = explicitOverride.HasValue
109+
? new Dictionary<string, object> { ["$process_person_profile"] = explicitOverride.Value }
110+
: null;
111+
112+
var context = PostHogContextHelper.ResolveCaptureContext(distinctId: null, properties: properties);
107113

108114
Assert.True(Guid.TryParse(context.DistinctId, out _));
109115
Assert.True(context.IsPersonless);
110116
Assert.NotNull(context.Properties);
111-
Assert.False((bool)context.Properties["$process_person_profile"]);
112-
}
113-
114-
[Fact]
115-
public void ExplicitProcessPersonProfileOverridesPersonlessDefault()
116-
{
117-
var context = PostHogContextHelper.ResolveCaptureContext(
118-
distinctId: null,
119-
properties: new Dictionary<string, object> { ["$process_person_profile"] = true });
120-
121-
Assert.NotNull(context.Properties);
122-
Assert.True((bool)context.Properties["$process_person_profile"]);
117+
Assert.Equal(expectedValue, (bool)context.Properties["$process_person_profile"]);
123118
}
124119

125120
[Fact]

0 commit comments

Comments
 (0)