feat(server): Add request context support#522
Open
dustinbyrne wants to merge 2 commits into
Open
Conversation
ec3bde4 to
f6c2cdb
Compare
Prompt To Fix All With AIFix the following 1 code review issue. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 1
posthog-server/src/test/java/com/posthog/server/PostHogEvaluateFlagsTest.kt:235-299
**Duplicate tests should be parameterized**
`evaluateFlags uses request context distinctId when omitted` and `evaluateFlags uses request context distinctId when null` are structurally identical — the only difference is calling `postHog.evaluateFlags()` vs `postHog.evaluateFlags(null)`. Per the team's preference for parameterized tests, these two cases should be collapsed into a single `@ParameterizedTest` (e.g. using a boolean parameter or a supplier for the call-under-test).
Reviews (1): Last reviewed commit: "fix: allow nullable request context dist..." | Re-trigger Greptile |
Comment on lines
+235
to
+299
| @Test | ||
| fun `evaluateFlags uses request context distinctId when omitted`() { | ||
| val mockServer = MockWebServer() | ||
| mockServer.enqueue(jsonResponse(createFlagsResponse("a", enabled = true))) | ||
| mockServer.start() | ||
|
|
||
| var postHog: PostHogInterface? = null | ||
| try { | ||
| postHog = | ||
| PostHog.with( | ||
| PostHogConfig.builder(TEST_API_KEY) | ||
| .host(mockServer.url("/").toString()) | ||
| .build(), | ||
| ) | ||
|
|
||
| val snapshot = | ||
| PostHogRequestContext.withContext(PostHogRequestContextData(distinctId = "context-user")) { | ||
| postHog.evaluateFlags() | ||
| } | ||
|
|
||
| assertEquals("context-user", snapshot.distinctId) | ||
| assertTrue(snapshot.isEnabled("a")) | ||
|
|
||
| val request = mockServer.takeRequest(2, TimeUnit.SECONDS) | ||
| assertNotNull(request) | ||
| val body = request.body.unGzip() | ||
| assertTrue(body.contains("\"distinct_id\":\"context-user\""), "expected context distinctId in request body, got: $body") | ||
| } finally { | ||
| postHog?.close() | ||
| mockServer.shutdown() | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `evaluateFlags uses request context distinctId when null`() { | ||
| val mockServer = MockWebServer() | ||
| mockServer.enqueue(jsonResponse(createFlagsResponse("a", enabled = true))) | ||
| mockServer.start() | ||
|
|
||
| var postHog: PostHogInterface? = null | ||
| try { | ||
| postHog = | ||
| PostHog.with( | ||
| PostHogConfig.builder(TEST_API_KEY) | ||
| .host(mockServer.url("/").toString()) | ||
| .build(), | ||
| ) | ||
|
|
||
| val snapshot = | ||
| PostHogRequestContext.withContext(PostHogRequestContextData(distinctId = "context-user")) { | ||
| postHog.evaluateFlags(null) | ||
| } | ||
|
|
||
| assertEquals("context-user", snapshot.distinctId) | ||
| assertTrue(snapshot.isEnabled("a")) | ||
|
|
||
| val request = mockServer.takeRequest(2, TimeUnit.SECONDS) | ||
| assertNotNull(request) | ||
| val body = request.body.unGzip() | ||
| assertTrue(body.contains("\"distinct_id\":\"context-user\""), "expected context distinctId in request body, got: $body") | ||
| } finally { | ||
| postHog?.close() | ||
| mockServer.shutdown() | ||
| } | ||
| } |
There was a problem hiding this comment.
Duplicate tests should be parameterized
evaluateFlags uses request context distinctId when omitted and evaluateFlags uses request context distinctId when null are structurally identical — the only difference is calling postHog.evaluateFlags() vs postHog.evaluateFlags(null). Per the team's preference for parameterized tests, these two cases should be collapsed into a single @ParameterizedTest (e.g. using a boolean parameter or a supplier for the call-under-test).
Prompt To Fix With AI
This is a comment left during a code review.
Path: posthog-server/src/test/java/com/posthog/server/PostHogEvaluateFlagsTest.kt
Line: 235-299
Comment:
**Duplicate tests should be parameterized**
`evaluateFlags uses request context distinctId when omitted` and `evaluateFlags uses request context distinctId when null` are structurally identical — the only difference is calling `postHog.evaluateFlags()` vs `postHog.evaluateFlags(null)`. Per the team's preference for parameterized tests, these two cases should be collapsed into a single `@ParameterizedTest` (e.g. using a boolean parameter or a supplier for the call-under-test).
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
marandaneto
approved these changes
May 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
💡 Motivation and Context
Server-side SDK users handling frontend requests need a safe way to carry PostHog frontend tracing context into backend captures. This adds request-scoped context support for
posthog-server, so captures and exception captures can inherit frontend distinct/session metadata while still preserving explicit SDK call overrides.💚 How did you test it?
./gradlew :posthog-server:test --tests "com.posthog.server.PostHogRequestContextTest" --tests "com.posthog.server.PostHogEvaluateFlagsTest"make checkFormat./gradlew :posthog-server:testgit diff --checkmake api📝 Checklist
If releasing new changes
.changeset/request-context-server.md)