fix(HttpResponse): forward cookies only when response is used - #2728
Conversation
📝 WalkthroughWalkthroughRequest handling now supports optional mocked responses and forwards raw Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant RequestHandler
participant MockedResponse
participant forwardResponseCookies
participant Document
Client->>RequestHandler: send request
RequestHandler->>MockedResponse: invoke resolver -> Response | undefined
alt mocked Response present
RequestHandler->>forwardResponseCookies: forwardResponseCookies(response)
forwardResponseCookies->>MockedResponse: read raw Set-Cookie(s)
forwardResponseCookies->>Document: assign each to document.cookie
end
RequestHandler->>Client: return mocked or proxied response
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Review rate limit: 6/8 reviews remaining, refill in 10 minutes and 57 seconds.Comment |
686d87d to
5afdabf
Compare
commit: |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/core/handlers/RequestHandler.ts (1)
340-340: Prefer explicitResponsenarrowing before cookie forwarding.At Line 353, the truthy check works in practice, but
instanceof Responsemakes the contract explicit and safer for untyped consumers.Suggested tweak
- if (mockedResponse) { + if (mockedResponse instanceof Response) { forwardResponseCookies(mockedResponse) }Also applies to: 353-355
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/core/handlers/RequestHandler.ts` at line 340, Replace the loose truthy check used before cookie forwarding with an explicit Response type guard: ensure the value returned where the code currently narrows with a truthy check is first checked with "instanceof Response" (or a dedicated isResponse helper) before you call response.clone()/forwardCookies or access Response properties; update the Promise<Response | undefined> handling in RequestHandler (the code that currently treats the resolved value as truthy) to branch on instanceof Response and only forward cookies in that branch, returning undefined or handling other cases otherwise.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/core/handlers/RequestHandler.ts`:
- Line 340: Replace the loose truthy check used before cookie forwarding with an
explicit Response type guard: ensure the value returned where the code currently
narrows with a truthy check is first checked with "instanceof Response" (or a
dedicated isResponse helper) before you call response.clone()/forwardCookies or
access Response properties; update the Promise<Response | undefined> handling in
RequestHandler (the code that currently treats the resolved value as truthy) to
branch on instanceof Response and only forward cookies in that branch, returning
undefined or handling other cases otherwise.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 209eb580-c94a-4edc-89ad-7c7f48544994
📒 Files selected for processing (4)
src/core/handlers/RequestHandler.tssrc/core/utils/HttpResponse/decorators.tssrc/core/utils/request/storeResponseCookies.tstest/browser/rest-api/request/request-cookies.mocks.ts
🚧 Files skipped from review as they are similar to previous changes (3)
- test/browser/rest-api/request/request-cookies.mocks.ts
- src/core/utils/request/storeResponseCookies.ts
- src/core/utils/HttpResponse/decorators.ts
Released: v2.14.0 🎉This has been released in v2.14.0. Get these changes by running the following command: Predictable release automation by Release. |
Currently, we are forwarding the mocked response cookies onto
document.cookieas a part of theHttpResponseconstructor. That means that even constructing anHttpResponseinstance will immediately forward itsSet-Cookieontodocument.cookie, even if that response never gets used (e.g. is conditional).Changes
HttpResponseno longer forwards itsSet-Cookieontodocument.cookiein the constructor.RequestHandler.runand applies only to the mocked response getting used.