Skip to content

Commit 94f650e

Browse files
authoredJan 6, 2025··
fix(vitest-pool-workers): fix support for query params with repeated keys (#7668)
1 parent f5b3fb5 commit 94f650e

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed
 

‎.changeset/moody-bikes-cross.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@cloudflare/vitest-pool-workers": patch
3+
---
4+
5+
fix: Add support interception of URLs with repeated key/name in its query params.
6+
7+
e.g., `https://example.com/foo/bar?a=1&a=2`

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

+11
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ it("falls through to global fetch() if unmatched", async () => {
3232
expect(await response.text()).toBe("fallthrough:GET https://example.com/bad");
3333
});
3434

35+
it("intercepts URLs with query parameters with repeated keys", async () => {
36+
fetchMock
37+
.get("https://example.com")
38+
.intercept({ path: "/foo/bar?a=1&a=2" })
39+
.reply(200, "body");
40+
41+
let response = await fetch("https://example.com/foo/bar?a=1&a=2");
42+
expect(response.url).toEqual("https://example.com/foo/bar?a=1&a=2");
43+
expect(await response.text()).toBe("body");
44+
});
45+
3546
describe("AbortSignal", () => {
3647
let abortSignalTimeoutMock: MockInstance;
3748

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,10 @@ globalThis.fetch = async (input, init) => {
9393
const bodyText = bodyArray === null ? "" : DECODER.decode(bodyArray);
9494
const dispatchOptions: Dispatcher.DispatchOptions = {
9595
origin: url.origin,
96-
path: url.pathname,
96+
path: url.pathname + url.search,
9797
method: request.method as Dispatcher.HttpMethod,
9898
body: bodyText,
9999
headers: requestHeaders,
100-
query: Object.fromEntries(url.searchParams),
101100
};
102101
requests.set(dispatchOptions, { request, body: bodyArray });
103102

0 commit comments

Comments
 (0)
Please sign in to comment.