Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repro / mock request with incoming request.cf properties #4

Open
wants to merge 1 commit into
base: minimal-clean-slate
Choose a base branch
from
Open
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
49 changes: 49 additions & 0 deletions test/request-with-IncomingRequestCfProperties.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Miniflare } from "miniflare";

// PASSES
test("set `cf` in RequestInit", () => {
const request = new Request("http://localhost:8787", {
cf: { regionCode: "CA" } as IncomingRequestCfProperties,
});
expect(request.cf?.regionCode).toBe("CA");
});

// FAILS
test("set `cf` via Miniflare#metaProvider", async () => {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, metaProvider is only used by the HTTP server (mf.startServer/mf.createServer) at the moment 😦
@mrbbot https://discord.com/channels/595317990191398933/891052295410835476/1004750072812154970

async function handleRequest(request: Request) {
// undefined
expect(request.cf?.regionCode).toBe("CA");
return new Response("OK");
}

const mf = new Miniflare({
script: `
export default {
fetch: handleRequest,
}
`,
globals: {
handleRequest,
},
modules: true,
/**
* > Added a metaProvider option that allows you fetch metadata for an incoming Request
* > https://github.com/cloudflare/miniflare/releases/tag/v2.0.0
*
* https://github.com/cloudflare/miniflare/issues/61#issuecomment-952817275
* https://github.com/cloudflare/miniflare/commit/b7ddbb36f0a1998519a4ac179e1a609cad7e75be#diff-a732a2a999ca2078de283e7dab7fe68fef00ec14cda5e89c1fb8d3a26518fc2eR31
*/
async metaProvider(_req: Request) {
return {
// forwardedProto: req.headers.get("X-Forwarded-Proto"),
// realIp: req.headers.get("X-Forwarded-For"),
cf: {
regionCode: "CA",
},
};
},
});

await mf.dispatchFetch("http://localhost:8787/");
expect.assertions(1);
});