Skip to content
Closed
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
2 changes: 1 addition & 1 deletion src/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export function joinURL(base: string | undefined, path: string | undefined): str
if (!base || base === "/") {
return path || "/";
}
if (!path) {
if (!path || path === "/") {
return base;
}
// eslint-disable-next-line unicorn/prefer-at
Expand Down
31 changes: 28 additions & 3 deletions test/_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,19 @@ describe("lib/http-proxy/common.js", () => {
expect(outgoing.path).to.eql("/?project=example&path=");
});

it("should not add trailing slash when target has base path and request path is root", () => {
const outgoing = createOutgoing();
common.setupOutgoing(
outgoing,
{
target: URL.parse("http://localhost/api")!,
},
stubIncomingMessage({ url: "/" }),
);

expect(outgoing.path).to.eql("/api");
});

it("should not modify the query string", () => {
const outgoing = createOutgoing();
common.setupOutgoing(
Expand Down Expand Up @@ -694,13 +707,25 @@ describe("lib/http-proxy/common.js", () => {
expect(common.joinURL("/base/", "path")).to.eql("/base/path");
});

it("should preserve trailing slash when path is exactly /", () => {
expect(common.joinURL("/maildev", "/")).to.eql("/maildev/");
it("should return base when path is exactly /", () => {
expect(common.joinURL("/maildev", "/")).to.eql("/maildev");
});

it("should keep a single trailing slash when both base ends and path is /", () => {
it("should return base when base ends with slash and path is /", () => {
expect(common.joinURL("/maildev/", "/")).to.eql("/maildev/");
});

it("should return base when path is empty", () => {
expect(common.joinURL("/api", "")).to.eql("/api");
});

it("should join base and nested path", () => {
expect(common.joinURL("/api", "/users")).to.eql("/api/users");
});

it("should join base with trailing slash and nested path", () => {
expect(common.joinURL("/api/", "/users")).to.eql("/api/users");
});
});

describe("#rewriteCookieProperty", () => {
Expand Down