Skip to content
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

Setting header with cookie value no longer overwrites a cookie value, it appends #631

Closed
noahbald opened this issue Sep 11, 2024 · 4 comments · Fixed by #639
Closed

Setting header with cookie value no longer overwrites a cookie value, it appends #631

noahbald opened this issue Sep 11, 2024 · 4 comments · Fixed by #639
Assignees

Comments

@noahbald
Copy link

noahbald commented Sep 11, 2024

Hey there, I've updated msw from ^2.4.2 to ^2.4.5 and thus @mswjs/interceptors from 0.29.1 to 0.35.1. This seems to have a changed behaviour when setting a cookie header.
I'm not too sure of the technical details, but this is the change I'm seeing.

// msw@^2.4.2
console.log(request.headers.get("cookie")); // foo=bar
request.headers.set("cookie", "foo=baz");
console.log(request.headers.get("cookie")); // foo=baz

// msw@^2.4.5
console.log(request.headers.get("cookie")); // foo=bar
request.headers.set("cookie", "foo=baz");
console.log(request.headers.get("cookie")); // foo=bar; foo=baz

Workaround

import { parse, serialize } from "cookie";

const cookies = cookies.parse(request.headers.get("cookie")!);
cookies.foo = "baz"
request.headers.set(serialize(cookies));
@kettanaito
Copy link
Member

Root cause

The issue is caused by the interceptors pushing recorded headers into the internal array:

So, whenever you set a header on a response with that header name already present, the internal array will contain two values. The Headers class will then treat that as multi-value header, merging its values.

response.headers.set('foo', 'bar')
// internally: [ ['foo', 'bar'] ]

response.headers.get('foo', 'xyz')
// internally: [ ['foo', 'bar'], ['foo', 'xyz'] ]

@kettanaito
Copy link
Member

Released: v0.35.6 🎉

This has been released in v0.35.6!

Make sure to always update to the latest version (npm i @mswjs/interceptors@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

@noahbald
Copy link
Author

Awesome work, I can confirm the fix is working on my end 🙌

@kettanaito
Copy link
Member

Yoo-hoo!

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 a pull request may close this issue.

2 participants