msw icon indicating copy to clipboard operation
msw copied to clipboard

Headers created before `server.listen()` are not being considered after changed with `headers.set()` or `headers.append()`

Open diego-aquino opened this issue 4 months ago • 3 comments

Prerequisites

Environment check

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

Node.js version

v20.17.0

Reproduction repository

https://github.com/diego-aquino/msw-headers-init-repro-with-vitest

Reproduction steps

  1. Clone the reproduction repository
  2. npm install
  3. npm run test

Current behavior

The README.md of the reproduction repository contains a more detailed explanation. Here's a summary:

I created two identical test suites, except for the imported MSW version:

Before starting the MSW server, each suite creates a Headers instance with initial values, appends a new header and checks if the initial values are preserved and the instance contains the correct values.

After starting the server with server.listen(), the suites append yet another header to the instance and re-run the checks. With [email protected], the existing values are preserved and the new header is appended. However, the current headers are lost with [email protected] when used in a Request or fetch call. The only kept header is the one appended after starting the server, as if the instance was empty before.

The problem does not appear to occur when using the headers directly, because only the last expect reading the headers from a Request fails.

Expected behavior

For headers created before server.listen(), Request and fetch should consider their previous values after changed with set or append. This was the behavior with [email protected] and appears to have changed in [email protected] and later.

Let me know if there is anything unclear I could clarify!

diego-aquino avatar Sep 29 '24 16:09 diego-aquino