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

Axios fails to follow redirects from MSW mocked responses #2177

Closed
4 tasks done
a11rew opened this issue Jun 12, 2024 · 4 comments · Fixed by mswjs/interceptors#581 or #2268
Closed
4 tasks done

Axios fails to follow redirects from MSW mocked responses #2177

a11rew opened this issue Jun 12, 2024 · 4 comments · Fixed by mswjs/interceptors#581 or #2268
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node

Comments

@a11rew
Copy link

a11rew commented Jun 12, 2024

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 18 or higher

Node.js version

v20.12.2

Other versions

msw - 2.3.1
axios - 1.7.2

Reproduction repository

https://codesandbox.io/p/devbox/axios-follow-redirects-issue-9qn7z9

Reproduction steps

  1. Setup a http.get response handler that responds with a redirect (HttpResponse.redirect or new Response with 3xx code and Location header)
  2. Make a GET request to the URL specified in the handler

Current behavior

The request fails with this error message originating from the follow-redirects library Axios uses

Error [ERR_FR_REDIRECTION_FAILURE]: Redirected request failed: Cannot read properties of undefined (reading 'getHeader')

The stack trace points to this line in the follow-redirects lib
https://github.com/follow-redirects/follow-redirects/blob/35a517c5861d79dc8bff7db8626013d20b711b06/index.js#L422

response.req.getHeader is undefined in the IncomingMessage/response follow-redirects receives when the response is mocked using MSW. This does not happen when the requests returning redirects are not intercepted with MSW.

Expected behavior

Axios should be able to follow redirects normally like it does when responses are not mocked with MSW.

@a11rew a11rew added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node labels Jun 12, 2024
@kettanaito
Copy link
Member

Hi, @a11rew. Thanks for reporting this.

We have a test for follow-redirects here but, apparently, it misses something. One difference I can spot is that in our tests the original server responds with a redirect, while in your use case the mock does that. Perhaps that's the culprit.

Would you have some time to add a new test case to the test I've linked above? Something like

interceptor.on('request', ({ request }) => {
  request.respondWith(new Response(null, {
    status: 300,
    headers: { Location: '/new/url' }
  }))
})

@a11rew
Copy link
Author

a11rew commented Jun 14, 2024

Added the failing test case in this PR mswjs/interceptors#581 @kettanaito. I'd be open to contributing a fix for this, any pointers on where I should be looking?

@kettanaito
Copy link
Member

Could be potentially fixed by mswjs/interceptors#626. Afaik, Axios uses XHR by default, so we need to look into whether XHR follows redirects to distinguish if that's a request client behavior or Axios' behavior.

@kettanaito
Copy link
Member

Released: v2.4.4 🎉

This has been released in v2.4.4!

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.

@github-actions github-actions bot locked and limited conversation to collaborators Oct 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node
Projects
None yet
2 participants