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

feat(fetch): support following redirect responses #627

Merged
merged 3 commits into from
Sep 8, 2024

Conversation

kettanaito
Copy link
Member

@kettanaito kettanaito commented Sep 7, 2024

Warning

Marking this as a breaking (feat) release because mocked redirect responses will now be followed by default (previously, returned as-is). The previous behavior was faulty and not spec-compliant, but I don't want to break consumers once again.

The redirect response handling is taken from Undici almost as-is because it doesn't export its web/fetch/utils as a typed module (it's untyped CJS). Luckily, Undici follows the spec in those implementations so they are unlikely to change. I'm also omitting things that are exclusive to Undici, such as accessing request.body.source.

Relevant resources

@kettanaito kettanaito force-pushed the fix/fetch-follow-redirect branch 2 times, most recently from 8955263 to e9ef8c7 Compare September 7, 2024 18:55
@kettanaito kettanaito marked this pull request as ready for review September 7, 2024 18:55
@kettanaito kettanaito force-pushed the fix/fetch-follow-redirect branch from e9ef8c7 to 9fba26d Compare September 7, 2024 18:58
@kettanaito kettanaito changed the title fix(fetch): support following redirect responses feat(fetch): support following redirect responses Sep 7, 2024
@kettanaito kettanaito requested a review from mikicho September 7, 2024 19:01
await httpServer.close()
})

it('follows a bypassed redirect response', async () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Added the tests for all scenarios we can reproduce from Undici.

/**
* @see https://github.com/nodejs/undici/blob/a6dac3149c505b58d2e6d068b97f4dc993da55f0/lib/web/fetch/index.js#L1210
*/
export async function followFetchRedirect(
Copy link
Member Author

Choose a reason for hiding this comment

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

Implementation of following redirects.

* This way, the client can manually follow the redirect it receives.
* @see https://github.com/nodejs/undici/blob/a6dac3149c505b58d2e6d068b97f4dc993da55f0/lib/web/fetch/index.js#L1173
*/
if (RESPONSE_STATUS_CODES_WITH_REDIRECT.has(response.status)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Fix: handle redirect responses in a special way.

@kettanaito
Copy link
Member Author

kettanaito commented Sep 7, 2024

I'd also look into following redirects in http so we can ship the two breaking fixes in a single release. I think we can even reuse the same test cases...

@kettanaito kettanaito merged commit 7410d45 into main Sep 8, 2024
2 checks passed
@kettanaito kettanaito deleted the fix/fetch-follow-redirect branch September 8, 2024 11:40
@kettanaito
Copy link
Member Author

Released: v0.35.0 🎉

This has been released in v0.35.0!

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.

@mikicho
Copy link
Contributor

mikicho commented Sep 8, 2024

Amazing PR!

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