Skip to content

Commit 44536d7

Browse files
authored
Merge pull request #21 from KrammyGod/fix-missing-search-params
fix: target search params are missing when target is a URL
2 parents 664e960 + 004fd9b commit 44536d7

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lib/http-proxy/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export function setupOutgoing(
112112

113113
// target if defined is a URL object so has attribute "pathname", not "path".
114114
const targetPath =
115-
target && options.prependPath !== false && 'pathname' in target ? getPath(target.pathname) : "/";
115+
target && options.prependPath !== false && 'pathname' in target ? getPath(`${target.pathname}${target.search ?? ""}`) : "/";
116116

117117
let outgoingPath = options.toProxy ? req.url : getPath(req.url);
118118

lib/test/lib/http-proxy-common.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,33 @@ describe("#setupOutgoing", () => {
292292
expect(outgoing.path).toEqual("/forward/src?f=1&s=1");
293293
});
294294

295+
it("target path is URL and has query string", () => {
296+
const outgoing: any = {};
297+
setupOutgoing(
298+
outgoing,
299+
{
300+
target: new URL("http://dummy.org/some-path?a=1&b=2"),
301+
},
302+
{ url: "/src?s=1" },
303+
);
304+
305+
expect(outgoing.path).toEqual("/some-path/src?a=1&b=2&s=1");
306+
});
307+
308+
it("target path is URL and has query string with ignorePath", () => {
309+
const outgoing: any = {};
310+
setupOutgoing(
311+
outgoing,
312+
{
313+
target: new URL("http://dummy.org/some-path?a=1&b=2"),
314+
ignorePath: true,
315+
},
316+
{ url: "/src?s=1" },
317+
);
318+
319+
expect(outgoing.path).toEqual("/some-path?a=1&b=2");
320+
});
321+
295322
//
296323
// This is the proper failing test case for the common.join problem
297324
//

0 commit comments

Comments
 (0)