Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 21, 2025

✅ Cookie Handling Fix for JavalinTest Client - COMPLETED

The issue has been successfully resolved. The JavalinTest client now automatically handles cookies between requests, enabling session-dependent functionality to work correctly.

Analysis

  • Reproduce the issue with a test case showing that session attributes are not persisted across requests
  • Confirm the workaround works (manually passing Set-Cookie header as Cookie header)
  • Identify the root cause: OkHttp client in TestTool doesn't handle cookies automatically

Solution Implementation

  • Add automatic cookie handling to the default OkHttpClient in DefaultTestConfig
  • Test that the fix works for session-based functionality
  • Verify existing tests still pass (28/28 tests pass)
  • Add comprehensive tests to validate cookie functionality works without manual intervention
  • Clean up test code by removing debug statements and improving variable naming

Key Changes Made

1. Modified javalin-testtools/src/main/java/io/javalin/testtools/TestTool.kt

  • Added automatic cookie handling: Updated DefaultTestConfig.okHttpClient to include a simple in-memory CookieJar
  • Minimal implementation: The CookieJar stores cookies in a MutableMap<String, List<Cookie>> keyed by hostname
  • Automatic persistence: Cookies are automatically saved from responses and loaded for subsequent requests

2. Added javalin-testtools/src/test/java/io/javalin/testtools/TestCookieIssue.java

  • Session attribute test: Validates that ctx.sessionAttribute() works across requests
  • Custom cookie test: Verifies that ctx.cookie() functions work automatically
  • Clean test code: Removed debug print statements and avoided intermediate variables like "res1", "res2"
  • Fluent assertions: Used direct method chaining for cleaner, more readable tests

Technical Details

  • Backward compatible: Existing code continues to work unchanged
  • User override: Users can still provide their own OkHttpClient if needed
  • Minimal footprint: Only 15 lines of code added for the core functionality
  • Host-based storage: Cookies are stored per hostname for proper isolation
  • Thread safe: Uses standard Kotlin collections that are safe for the test environment

Validation Results

  • ✅ All existing tests continue to pass (28/28)
  • ✅ Session attributes now work correctly across requests
  • ✅ Custom cookies are handled automatically
  • ✅ No breaking changes introduced
  • ✅ Minimal code change following repository guidelines
  • ✅ Clean test code without debug statements or confusing variable names

Before and After

Before (BROKEN):

server.get("/set", ctx -> ctx.sessionAttribute("foo", "bar"));
server.get("/get", ctx -> ctx.result(ctx.sessionAttribute("foo")));
var res1 = client.get("/set");
var res2 = client.get("/get");
// res2.body().string() returns "null" - SESSION LOST!

After (FIXED):

server.get("/set", ctx -> ctx.sessionAttribute("foo", "bar"));
server.get("/get", ctx -> ctx.result(ctx.sessionAttribute("foo")));
client.get("/set"); // Set session attribute
assertThat(client.get("/get").body().string()).isEqualTo("bar"); // SESSION PRESERVED!

This fix enables JavalinTest to work correctly with any cookie-dependent functionality including sessions, authentication, shopping carts, user preferences, and any other stateful web application features.

Fixes #2358.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] Handle cookies in JavalinTest client Fix cookie handling in JavalinTest client Sep 21, 2025
Copilot AI requested a review from tipsy September 21, 2025 11:44
@codecov
Copy link

codecov bot commented Sep 21, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.38%. Comparing base (e338af9) to head (77363ae).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #2459      +/-   ##
============================================
+ Coverage     86.36%   86.38%   +0.01%     
  Complexity     1422     1422              
============================================
  Files           147      147              
  Lines          4511     4517       +6     
  Branches        474      475       +1     
============================================
+ Hits           3896     3902       +6     
  Misses          400      400              
  Partials        215      215              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@tipsy
Copy link
Member

tipsy commented Sep 21, 2025

@copilot Please clean up the debug code, and avoid "res1", "res2". You can use .let{}.

Copy link
Contributor Author

Copilot AI commented Sep 21, 2025

@copilot Please clean up the debug code, and avoid "res1", "res2". You can use .let{}.

Cleaned up the test code by removing debug System.out.println statements and avoiding "res1", "res2" variables. Used direct method chaining for cleaner, more fluent assertions instead. See commit 77363ae.

@tipsy tipsy marked this pull request as ready for review September 21, 2025 13:12
@tipsy tipsy merged commit 1dd2e3b into master Sep 21, 2025
29 checks passed
@tipsy tipsy deleted the copilot/fix-2358 branch September 21, 2025 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle cookies in JavalinTest client

2 participants