Skip to content

Commit 2b92a65

Browse files
authored
fix(vitest-pool-workers): responses from fetchMock should have immutable headers (#8254)
1 parent 3c695d8 commit 2b92a65

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

.changeset/vast-trains-joke.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/vitest-pool-workers": patch
3+
---
4+
5+
fix: responses from `fetchMock` should have immutable headers

fixtures/vitest-pool-workers-examples/misc/test/fetch-mock.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ it("intercepts URLs with query parameters with repeated keys", async () => {
6161
expect(await response3.text()).toBe("baz");
6262
});
6363

64+
it("throws if you try to mutate the headers", async () => {
65+
fetchMock
66+
.get("https://example.com")
67+
.intercept({ path: "/" })
68+
.reply(200, "body");
69+
70+
let response = await fetch("https://example.com");
71+
72+
expect(() => response.headers.set("foo", "bar")).toThrowError();
73+
expect(() => response.headers.append("foo", "baz")).toThrowError();
74+
expect(() => response.headers.delete("foo")).toThrowError();
75+
});
76+
6477
describe("AbortSignal", () => {
6578
let abortSignalTimeoutMock: MockInstance;
6679

packages/vitest-pool-workers/src/worker/fetch-mock.ts

+8
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,15 @@ globalThis.fetch = async (input, init) => {
170170
statusText: responseStatusText,
171171
headers: responseHeaders,
172172
});
173+
const throwImmutableHeadersError = () => {
174+
throw new TypeError("Can't modify immutable headers");
175+
};
173176
Object.defineProperty(response, "url", { value: url.href });
177+
Object.defineProperties(response.headers, {
178+
set: { value: throwImmutableHeadersError },
179+
append: { value: throwImmutableHeadersError },
180+
delete: { value: throwImmutableHeadersError },
181+
});
174182
responseResolve(response);
175183
} else {
176184
responseResolve(maybeResponse);

0 commit comments

Comments
 (0)