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

fix: implement cookie persistence using tough-cookie #2206

Merged
merged 10 commits into from
Jul 23, 2024
Merged

Conversation

kettanaito
Copy link
Member

@kettanaito kettanaito commented Jul 16, 2024

This is a rework of the request/response cookie handling. Since v2, I believe the previous logic has become obsolete. We are forwarding the mocked response cookies to the document.cookie but we won't be able to read them back if those cookies are not exposed to JavaScript (like HttpOnly cookies). Neither would we be able to read them back from the response.headers.get('set-cookie') because that's a forbidden header field.

In this rework, I'm removing @mswjs/cookies and introducing tough-cookie and its CookieJar class to get cookie filtering and, eventually, persistence (has to be implemented separately through a custom Store).

Roadmap

}
const cookiesFromStore = Object.fromEntries(
cookieStore
.getCookiesSync(request.url)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tough-cookie package implements specification-compliant way of returning matching cookies for the given URL. It's maintained and well-used. I see no reason for us to rely on @mswjs/cookies anymore.

// in the HeadersInit. This is later used to store these cookies
// in cookie jar and return the right cookies in the "cookies"
// response resolver argument.
Object.defineProperty(response, kSetCookie, {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love to store the cookies here directly but tough-cookie needs a URL to associate the cookies with. response.url is not always defined (this may be easier to solve). I don't like introducing a symbol just to pass the data around.

@kettanaito kettanaito changed the title fix: store response "set-cookie" header fix: implement cookie persistence Jul 19, 2024
@kettanaito kettanaito changed the title fix: implement cookie persistence fix: implement cookie persistence using CookieJar Jul 19, 2024
@kettanaito kettanaito changed the title fix: implement cookie persistence using CookieJar fix: implement cookie persistence using tough-cookie Jul 23, 2024
@kettanaito kettanaito merged commit c30613c into main Jul 23, 2024
12 checks passed
@kettanaito kettanaito deleted the fix/cookies branch July 23, 2024 13:54
@kettanaito
Copy link
Member Author

Released: v2.3.3 🎉

This has been released in v2.3.3!

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


Predictable release automation by @ossjs/release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cookie with path other than root not sent to handlers
1 participant