-
-
Notifications
You must be signed in to change notification settings - Fork 404
🔧 fix: merge set.headers without duplicating Response #1591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
🔧 fix: merge set.headers without duplicating Response #1591
Conversation
WalkthroughThis PR fixes header merging behavior in Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
🧪 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. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
test/response/custom-response.test.ts (1)
40-63: LGTM! Test correctly validates the header merge behavior.The test clearly verifies that Response headers take precedence over
set.headersfor conflicting keys (Content-Type), while non-conflicting headers fromset.headers(X-Framework) are properly merged. This effectively covers the fix for issue #1590.Optional: Consider adding edge case tests.
While the current test is solid, you might consider adding tests for:
- Verifying behavior when
set.headersis a Headers instance (not just a plain object)- Multiple
set-cookieheaders to ensure they're all appended correctly- Case-insensitive header name handling (e.g.,
content-typevsContent-Type)These would strengthen test coverage but are not critical given the main scenario is well-tested.
src/adapter/utils.ts (1)
341-353: Correct implementation of header precedence for Headers instance.The logic properly ensures Response headers take precedence by checking
!response.headers.has(key)before merging headers fromset.headers. The use ofset()instead ofappend()prevents duplicate header values, which was the root cause of issue #1590.Cookie handling is correct: using
getSetCookie()andappend()allows multiple set-cookie headers as intended.Minor: Optional chaining is unnecessary.
At line 352,
set.headers?.get(key)uses optional chaining, butset.headersis guaranteed to be a Headers instance per the condition at line 342. While harmless, you could simplify toset.headers.get(key) ?? ''.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/adapter/utils.ts(2 hunks)test/response/custom-response.test.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
test/response/custom-response.test.ts (1)
test/utils.ts (1)
req(1-2)
src/adapter/utils.ts (1)
src/cookies.ts (1)
set(310-320)
🔇 Additional comments (2)
src/adapter/utils.ts (2)
354-365: Correct implementation of header precedence for plain object.This branch mirrors the Headers instance logic: set-cookie headers are always appended (lines 356-360), while other headers are only added if not already present on the Response (lines 361-365). The use of
set()with the!response.headers.has(key)guard prevents the duplicate header issue.The case-insensitive nature of the Headers API ensures proper matching even when plain object keys have different casing than Response headers.
341-365: Implementation successfully fixes the header duplication issue.The changes correctly implement the merge strategy where Response headers take precedence and
set.headersonly fills in non-conflicting headers. This resolves issue #1590 where headers like Content-Type were being combined (e.g., "text/plain, application/json").Key improvements:
- Added
!response.headers.has(key)checks before merging headers- Changed from
append()toset()for non-cookie headers- Preserved
append()for set-cookie to allow multiple cookie values- Consistent behavior across both branches (Headers instance and plain object)
The implementation maintains backward compatibility with existing code while fixing the reported bug.
Fixes #1590
Problem
When a handler returns a
Responsewith custom headers,set.headersvalues were appended usingheaders.append(), causing duplicate header values:Solution
headers.set()instead ofheaders.append()for regular headersset.headersif Response doesn't already have it (Response takes precedence)append()forset-cookie(multiple cookies are valid)Changes
src/adapter/utils.ts: UpdatedcreateResponseHandler()to checkresponse.headers.has(key)before settingtest/response/custom-response.test.ts: Added test for header merge behaviorSummary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.