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(parseIsomorphicRequest): bypassing cookies properly #1155

Merged
merged 4 commits into from Mar 10, 2022

Conversation

hrsh7th
Copy link
Contributor

@hrsh7th hrsh7th commented Mar 9, 2022

Fix #1154

This PR aims to bypass correctly APIs that require cookies.

I've created the msw-snapshot package for testing that will response from actual server or disk-cache.
I met this problem in the test with the API that requires the login session.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 9, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit b64f988:

Sandbox Source
MSW React Configuration

@@ -15,7 +16,8 @@ export function parseIsomorphicRequest(
url: request.url,
method: request.method,
body: parseBody(request.body, request.headers),
headers: request.headers,
credentials: request.credentials,
headers: new Headers(Array.from(request.headers.entries())),
Copy link
Member

Choose a reason for hiding this comment

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

I think request.headers is already an instance of the Headers polyfill. Could you share more info as to why we need to construct it again? Are some headers being omitted? If so, have you understand why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To be honest, I don't understand perfectly the reason for this but it's needed to keep the headers field. Even if we call parseIsomorphicRequest multiple times.

I'll check how to disappear the headers field when calling parseIsomorphicRequest multiple times.

Copy link
Contributor Author

@hrsh7th hrsh7th Mar 9, 2022

Choose a reason for hiding this comment

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

Oh. I understood the reason. That the reason why is the setRequestCookies will overwrite the headers field.

No. Apparently it should be confirmed a little more firmly. I'll consider it. (google translated)

Copy link
Member

Choose a reason for hiding this comment

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

No worries!
The headers must not disappear—that's for certain. I just think that we don't need to deconstruct an existing Headers instance to construct it again 🤔 It'd be great to catch the issue you're experiencing into a unit test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've considered it.

The parseIsomorphicRequest always attaches the all cookies

This fact is commented in https://github.com/mswjs/msw/pull/1155/files#diff-738c9fc8b1eee13315e077f1aed118cc90af2e9cfb3965436290ff67a6d64284R33 .

I think it's correct so I don't modify it.

The setRequestCookie consumes the cookie headers

So we should re-create the new headers field. (because it's overwritten).

Finally, I think current PR is correct.

Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

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

Hey, @hrsh7th. Thanks for opening this pull request.

I've left a few suggestions/clarifications. Let me know about those when you have time.

Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

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

Thanks for improving the library, @hrsh7th! Welcome to contributors 🎉

@kettanaito kettanaito merged commit 755bc9d into mswjs:main Mar 10, 2022
kettanaito pushed a commit that referenced this pull request Mar 15, 2022
## [0.39.2](v0.39.1...v0.39.2) (2022-03-15)

### Bug Fixes

* **parseIsomorphicRequest:** bypassing cookies properly ([#1155](#1155)) ([755bc9d](755bc9d))
* remove console.log from "setRequestCookies" ([6f7ed98](6f7ed98))
* set "credentials" to "same-origin" for "ClientRequest" ([#1159](#1159)) ([c3cd80a](c3cd80a))
* set minimal supported Node.js version to 14 ([#1160](#1160)) ([d7ab139](d7ab139))
@kettanaito
Copy link
Member

🎉 This PR is included in version 0.39.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

kettanaito pushed a commit that referenced this pull request May 17, 2022
## [0.39.2](v0.39.1...v0.39.2) (2022-03-15)

### Bug Fixes

* **parseIsomorphicRequest:** bypassing cookies properly ([#1155](#1155)) ([755bc9d](755bc9d))
* remove console.log from "setRequestCookies" ([6f7ed98](6f7ed98))
* set "credentials" to "same-origin" for "ClientRequest" ([#1159](#1159)) ([c3cd80a](c3cd80a))
* set minimal supported Node.js version to 14 ([#1160](#1160)) ([d7ab139](d7ab139))
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.

parseIsomorphicRequest doesn't handle credentials and cookies.
2 participants