Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

fix: preserve repeated headers when merging defaults - #666

Open
lee3Q wants to merge 2 commits into
unjs:mainfrom
lee3Q:fix-655-repeated-headers
Open

lee3Q wants to merge 2 commits into
unjs:mainfrom
lee3Q:fix-655-repeated-headers

Conversation

@lee3Q

@lee3Q lee3Q commented Nov 27, 2026 •

Copy link
Copy Markdown

Summary

Resolves #655.

Normalize request headers before overlaying defaults so repeated tuple values follow the native Headers behavior. Remove overridden default names first, then append request entries. This preserves separate Set-Cookie entries as well as combined Accept values, while keeping request-over-default precedence.

Adds regression tests for repeated tuples with unrelated and same-name defaults, separate Set-Cookie entries, and input/default immutability.

The earlier attempt in #656 is closed and unmerged. It used the same normalization and delete/append approach; this PR includes an additional Set-Cookie regression.

Verification

Local Node 22.23.5, pnpm 10.20.0:

  • Same final tests against unchanged base code: 28 passed, 2 failed on the repeated-header regressions.
  • pnpm test: lint and coverage run passed, 30 tests passed.
  • pnpm exec tsc --noEmit: passed.
  • pnpm build: passed.
  • Additional direct header probes covered mixed-case names, same-name overrides, tuples and Headers with separate Set-Cookie entries, empty input, and no input/default mutation.

Not run locally: Node 20/24 CI matrix, browsers, Deno, or alternate Headers implementations. The getSetCookie() regression uses the native Node implementation.

Compatibility note: malformed tuples with other than two elements now throw through the Headers constructor even when defaults exist, matching the existing no-defaults path.

AI assistance was used for implementation and static review. The local execution results above are separate from those reviews.

Summary by CodeRabbit

  • Bug Fixes
    • Repeated header values are now preserved when headers are combined, including Accept and Set-Cookie values. Existing defaults and supplied header entries remain unchanged.

Added tests to verify header merging behavior in fetch requests.
Refactor header handling to delete existing keys before appending new ones.
@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

mergeHeaders now preserves repeated header entries from request input. Tests cover repeated Accept and Set-Cookie values with and without defaults, and verify that input values remain unchanged.

Changes: Header merging

Layer / File(s) Summary
Merge repeated headers and verify request behavior
src/utils.ts, test/index.test.ts
mergeHeaders removes defaults for names supplied by the request, then appends all input entries. Tests cover repeated header values, default-header behavior, and unchanged inputs.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Possibly related PRs

  • unjs/ofetch#656: This earlier PR targets the same repeated-header issue and changes mergeHeaders using a similar approach.

Suggested reviewers: pi0

Merge Risk: Minimal · up to 7fc08

This change fixes repeated request header values being dropped when default headers are configured. The supplied evidence shows no remaining merge-blocking risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix: preserve repeated headers when merging defaults" matches the main change. mergeHeaders now appends request header entries instead of using set, so repeated values such as Accept and Se…
Linked Issues check Passed Issue #655 requires that repeated values in a request's HeadersInit tuples survive when defaults are configured, and that a request header overrides a default with the same name. At head, src/utils.ts…
Out of Scope Changes check Passed The whole-PR changes, per the supplied summary, touch two files: src/utils.ts (mergeHeaders only, +6/-4) and test/index.test.ts (new header-merge regression tests, +60/-1). The src/utils.ts change i…
Full details: Title check

Explanation

The title "fix: preserve repeated headers when merging defaults" matches the main change. mergeHeaders now appends request header entries instead of using set, so repeated values such as Accept and Set-Cookie survive when defaults exist. The title is short, specific, and uses a conventional fix: prefix. It does not mention the precedence rule, but that is a secondary detail.

Full details: Linked Issues check

Explanation

Issue #655 requires that repeated values in a request's HeadersInit tuples survive when defaults are configured, and that a request header overrides a default with the same name. At head, src/utils.ts mergeHeaders builds inputHeaders = new Headers(input), deletes each input name from the defaults Headers, then appends every entry from inputHeaders. Because the input is normalized through Headers, repeated Accept values are combined as the native constructor does (application/json, text/plain). Unrelated defaults such as x-client no longer remove input entries. Same-name defaults are deleted before the append, so the request value wins. Separate Set-Cookie entries are appended individually. The PR summary and test summary state regression tests cover repeated tuples with unrelated and same-name defaults, separate Set-Cookie entries, and input/default immutability. I did not read the test file directly; the test coverage claim rests on the supplied summary. The code path matches the issue's suggested fix. No non-coding requirements apply.

Full details: Out of Scope Changes check

Explanation

The whole-PR changes, per the supplied summary, touch two files: src/utils.ts (mergeHeaders only, +6/-4) and test/index.test.ts (new header-merge regression tests, +60/-1). The src/utils.ts change is the fix described in #655. The test changes support that fix by covering the issue's reproduction and the same-name override requirement. The PR description notes a compatibility effect: malformed tuples with other than two elements now throw through the Headers constructor when defaults exist. This follows from normalizing input through Headers and matches the no-defaults path. It is a direct consequence of the fix, not unrelated work. No unrelated changes were identified. The diff could not be read through the tool because changed blobs were unavailable, so this rests on the head file contents and the supplied summary.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c84037cf-d51d-488a-ad4c-7c37f1269fab
Commits

Reviewing files that changed from the base of the PR and between 1dbc37f and 7fc088e.

Files selected for processing: 2

  • src/utils.ts
  • test/index.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

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.

Default headers cause repeated values in HeadersInit tuples to be dropped

1 participant