diff --git a/lib/http-proxy/common.ts b/lib/http-proxy/common.ts index 3acdd08..7c8c318 100644 --- a/lib/http-proxy/common.ts +++ b/lib/http-proxy/common.ts @@ -268,6 +268,9 @@ function hasPort(host: string): boolean { } function getPath(url?: string): string { + if (url === '' || url?.startsWith('?')) { + return url + } const u = toURL(url); return `${u.pathname ?? ""}${u.search ?? ""}`; } diff --git a/lib/test/http/empty-req-url.test.ts b/lib/test/http/empty-req-url.test.ts new file mode 100644 index 0000000..1593885 --- /dev/null +++ b/lib/test/http/empty-req-url.test.ts @@ -0,0 +1,52 @@ +import express from "express"; +import getPort from "../get-port"; +import ProxyServer, { createServer } from "../.."; +import http from "node:http"; + +describe("test empty req.url", () => { + let port: number, server: http.Server; + + it("create a simple http server", async () => { + port = await getPort(); + const app = express(); + + app.get("/test", (req, res, next) => { + if (req.path !== "/test") return next(); + res.send("Test Page!: " + JSON.stringify(req.query)); + }); + + server = app.listen(port); + }); + + let proxy: ProxyServer, httpServer: http.Server; + let proxyPort: number; + it("create a proxy server", async () => { + proxy = createServer(); + proxy.on("error", (err, _req, res) => { + console.error("Proxy error:", err); + res.end("Something went wrong."); + }); + httpServer = http.createServer((req, res) => { + req.url = '' + new URL(`http://example.com${req.url}`).search; + proxy.web(req, res, { target: `http://localhost:${port}/test` }); + }); + + proxyPort = await getPort(); + httpServer.listen(proxyPort); + }); + + const getProxy = async (url: string) => { + return await (await fetch(`http://localhost:${proxyPort}${url}`)).text(); + }; + + it("get using the proxy", async () => { + expect(await getProxy("")).toBe("Test Page!: {}"); + expect(await getProxy("?foo")).toBe("Test Page!: {\"foo\":\"\"}"); + expect(await getProxy("?foo=bar")).toBe("Test Page!: {\"foo\":\"bar\"}"); + }); + + it("clean up", () => { + server.close(); + httpServer.close(); + }); +}); diff --git a/lib/test/lib/http-proxy-common.test.ts b/lib/test/lib/http-proxy-common.test.ts index 5037ec2..80d766c 100644 --- a/lib/test/lib/http-proxy-common.test.ts +++ b/lib/test/lib/http-proxy-common.test.ts @@ -487,7 +487,7 @@ describe("#setupOutgoing", () => { const outgoing: any = {}; setupOutgoing(outgoing, { target: { pathname: "" } }, { url: "" }); - expect(outgoing.path).toEqual("/"); + expect(outgoing.path).toEqual(""); }); });