Skip to content

Commit f42939d

Browse files
authored
test: cover HTTPS/HTTP2 host port resolution in createRequestHandler
1 parent 1ebc845 commit f42939d

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

packages/vite-plugin-cloudflare/src/__tests__/utils.spec.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,11 @@ describe("createRequestHandler", () => {
106106
let httpServer: http.Server;
107107
let port: number;
108108
let capturedUrls: string[];
109+
let capturedForwardedHosts: (string | null)[];
109110

110111
beforeEach(async () => {
111112
capturedUrls = [];
113+
capturedForwardedHosts = [];
112114
});
113115

114116
afterEach(async () => {
@@ -117,13 +119,15 @@ describe("createRequestHandler", () => {
117119
);
118120
});
119121

120-
function startServer() {
122+
function startServer(mutateReq?: (req: http.IncomingMessage) => void) {
121123
const handler = createRequestHandler(async (request) => {
122124
capturedUrls.push(request.url);
125+
capturedForwardedHosts.push(request.headers.get("X-Forwarded-Host"));
123126
return new MiniflareResponse("OK");
124127
});
125128

126129
httpServer = http.createServer((req, res) => {
130+
mutateReq?.(req);
127131
void handler(
128132
req as unknown as Parameters<typeof handler>[0],
129133
res,
@@ -188,4 +192,27 @@ describe("createRequestHandler", () => {
188192
});
189193
expect(capturedUrls[0]).toBe(`http://127.0.0.1:${port}/path`);
190194
});
195+
196+
test("preserves non-default port from `:authority` when `Host` is missing", async ({
197+
expect,
198+
}) => {
199+
await startServer((req) => {
200+
delete req.headers.host;
201+
req.headers[":authority"] = "localhost:5173";
202+
});
203+
await fetch(`http://127.0.0.1:${port}/path`);
204+
expect(capturedUrls[0]).toBe("http://localhost:5173/path");
205+
expect(capturedForwardedHosts[0]).toBe("localhost:5173");
206+
});
207+
208+
test("preserves non-default port from the `Host` header", async ({
209+
expect,
210+
}) => {
211+
await startServer((req) => {
212+
req.headers.host = "localhost:5173";
213+
});
214+
await fetch(`http://127.0.0.1:${port}/path`);
215+
expect(capturedUrls[0]).toBe("http://localhost:5173/path");
216+
expect(capturedForwardedHosts[0]).toBe("localhost:5173");
217+
});
191218
});

0 commit comments

Comments
 (0)