Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/effective-fetch-request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hsblabs/fetch-interceptor": patch
---

Preserve `RequestInit` overrides when intercepting `fetch(request, init)` calls.
59 changes: 59 additions & 0 deletions src/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,31 @@ describe("createFetchRequest", () => {
expect(await clonedRequest.text()).toBe("hello");
});

it("applies init overrides to Request inputs without consuming the original body", async () => {
const originalRequest = new Request("https://example.com/base", {
body: "original-body",
headers: {
"x-original": "1",
},
method: "POST",
});

const request = createFetchRequest(originalRequest, {
body: "override-body",
headers: {
"x-override": "1",
},
method: "PUT",
});

expect(request.method).toBe("PUT");
expect(request.headers.get("x-original")).toBeNull();
expect(request.headers.get("x-override")).toBe("1");
expect(await request.text()).toBe("override-body");
expect(originalRequest.bodyUsed).toBe(false);
expect(await originalRequest.text()).toBe("original-body");
});

it("creates a Request from fetch arguments", async () => {
const request = createFetchRequest("https://example.com/from-args", {
body: JSON.stringify({ hello: "world" }),
Expand All @@ -35,6 +60,40 @@ describe("createFetchRequest", () => {
});

describe("createFetchInterceptorHandler", () => {
it("matches against the effective Request when fetch receives Request and init", async () => {
const onIntercept = vi.fn();
const originalFetch = vi.fn(async () => new Response("ok"));
const interceptedFetch = createFetchInterceptorHandler(originalFetch, {
matcher: (request) => request.method === "POST",
onIntercept,
});
const baseRequest = new Request("https://example.com/base", {
headers: {
"x-original": "1",
},
method: "PUT",
});

await interceptedFetch(baseRequest, {
body: "override-body",
headers: {
"x-override": "1",
},
method: "POST",
});

expect(originalFetch).toHaveBeenCalledOnce();
expect(onIntercept).toHaveBeenCalledOnce();

const [request] = onIntercept.mock.calls[0];

expect(request.method).toBe("POST");
expect(request.headers.get("x-original")).toBeNull();
expect(request.headers.get("x-override")).toBe("1");
expect(await request.text()).toBe("override-body");
expect(baseRequest.bodyUsed).toBe(false);
});

it("intercepts matching requests with a cloned response", async () => {
const onIntercept = vi.fn();
const originalFetch = vi.fn(async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function createFetchRequest(
const [input, init] = args;

if (input instanceof Request) {
return input.clone();
return new Request(input.clone(), init);
}

return new Request(input, init);
Expand Down
Loading