Skip to content

Commit

Permalink
Fix headers in wfp dev
Browse files Browse the repository at this point in the history
  • Loading branch information
GregBrimble committed Oct 6, 2024
1 parent 858214b commit 452873a
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default {
fetch(request, env) {
return fetch("https://example.com");
return fetch(request);
},
};
2 changes: 1 addition & 1 deletion fixtures/external-dispatch-namespace-app/outbound/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default {
fetch(request, env) {
return new Response("intercepted", {
return new Response(JSON.stringify(Object.fromEntries([...request.headers.entries()])), {
headers: { parameter1: env.parameter1, parameter2: env.parameter2 },
});
},
Expand Down
22 changes: 15 additions & 7 deletions fixtures/external-dispatch-namespace-app/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import { fetch } from "undici";
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
import { unstable_dev } from "wrangler";
import type { ChildProcess } from "child_process";
import type { Response } from "undici";
import type { Response, RequestInfo, RequestInit } from "undici";
import type { UnstableDevWorker } from "wrangler";

const waitUntilReady = async (url: string): Promise<Response> => {
const waitUntilReady = async (input: RequestInfo, init?: RequestInit): Promise<Response> => {
let response: Response | undefined = undefined;

while (response === undefined) {
await setTimeout(500);

try {
response = await fetch(url);
response = await fetch(input, init);
} catch (e) {}
}

Expand Down Expand Up @@ -74,7 +74,11 @@ describe("external-dispatch-namespace-app", () => {

it("dispatches from a Pages project", async () => {
const pagesDispatcherResponse = await waitUntilReady(
`http://127.0.0.1:${pagesDispatcher.port}/`
`http://127.0.0.1:${pagesDispatcher.port}/`, {
headers: {
"x-foo": "bar"
}
}
);
expect(
pagesDispatcherResponse.headers.get("parameter1")
Expand All @@ -83,13 +87,17 @@ describe("external-dispatch-namespace-app", () => {
pagesDispatcherResponse.headers.get("parameter2")
).toMatchInlineSnapshot(`"p2"`);
expect(await pagesDispatcherResponse.text()).toMatchInlineSnapshot(
`"intercepted"`
`"{\"x-foo\":\"bar\"}"`
);
});

it("dispatches from a Worker", async () => {
const dispatcherResponse = await waitUntilReady(
`http://127.0.0.1:${dispatcher.port}/`
`http://127.0.0.1:${dispatcher.port}/`, {
headers: {
"x-foo": "bar"
}
}
);
expect(dispatcherResponse.headers.get("parameter1")).toMatchInlineSnapshot(
`"p1"`
Expand All @@ -98,7 +106,7 @@ describe("external-dispatch-namespace-app", () => {
`"p2"`
);
expect(await dispatcherResponse.text()).toMatchInlineSnapshot(
`"intercepted"`
`"{\"x-foo\":\"bar\"}"`
);
});
});
Expand Down
4 changes: 2 additions & 2 deletions packages/wrangler/src/dispatch-namespaces/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export default function (env) {
return {
get(name, args, options) {
return {
fetch(request) {
request = new Request(request);
fetch(input, init) {
const request = new Request(input, init);
request.headers.set(HEADER_SCRIPT_NAME, name);
request.headers.set(HEADER_URL, request.url);
request.headers.set(HEADER_PARAMETERS, JSON.stringify(Object.fromEntries(Object.entries(options?.outbound ?? {}).filter(([key]) => ALLOWED_PARAMETERS.includes(key)))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const dispatchee: Middleware = async (request, env, _ctx, middlewareCtx) => {
const outboundProxyUrl = request.headers.get(HEADER_OUTBOUND_PROXY_URL);
const parameters = request.headers.get(HEADER_PARAMETERS);

request = new Request(url, { ...request, cf });
request = new Request(url, { ...request, cf, method: request.method, headers: request.headers });

request.headers.delete(HEADER_URL);
request.headers.delete(HEADER_OUTBOUND_PROXY_URL);
Expand All @@ -42,7 +42,6 @@ const dispatchee: Middleware = async (request, env, _ctx, middlewareCtx) => {
if (parameters) {
outboundRequest.headers.set(HEADER_PARAMETERS, parameters);
}
console.log(outboundProxyUrl);
return originalFetch(outboundProxyUrl, outboundRequest);
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const outbound: Middleware = async (request, env, _ctx, middlewareCtx) => {
env[key] = value;
}

request = new Request(url, { ...request, cf });
request = new Request(url, { ...request, cf, method: request.method, headers: request.headers });

request.headers.delete(HEADER_OUTBOUND_URL);
request.headers.delete(HEADER_CF_BLOB);
Expand Down

0 comments on commit 452873a

Please sign in to comment.