Skip to content

Axios fails to follow redirects from MSW mocked responses #2177

@a11rew

Description

@a11rew

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds:triageIssues that have not been investigated yet.scope:nodeRelated to MSW running in Node

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions