Skip to content

Commit

Permalink
fix: add target pathname currectly (#6)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <[email protected]>
  • Loading branch information
jonasolesen and pi0 committed Aug 26, 2023
1 parent 1ed43da commit e045521
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ export function setupOutgoing(outgoing, options, req, forward?) {
// the final path is target path + relative path requested by user:
const target = options[forward || "target"];
const targetPath =
target && options.prependPath !== false ? target.path || "" : "";
target && options.prependPath !== false
? target.pathname || target.path || ""
: "";

const parsed = new URL(req.url, "http://localhost");
let outgoingPath = options.toProxy
Expand Down
8 changes: 5 additions & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("httpxy", () => {
lastResolved = false;
lastRejected = undefined;
try {
await proxy.web(req, res, { target: mainListener.url });
await proxy.web(req, res, { target: mainListener.url + "base" });
lastResolved = true;
} catch (error) {
lastRejected = error;
Expand All @@ -39,8 +39,8 @@ describe("httpxy", () => {
});

it("works", async () => {
const mainResponse = await $fetch(mainListener.url + "?foo");
const proxyResponse = await $fetch(proxyListener.url + "?foo");
const mainResponse = await $fetch(mainListener.url + "base/test?foo");
const proxyResponse = await $fetch(proxyListener.url + "test?foo");

const maskResponse = (obj: any) => ({
...obj,
Expand All @@ -51,6 +51,8 @@ describe("httpxy", () => {
maskResponse(proxyResponse),
);

expect(proxyResponse.path).toBe("/base/test?foo");

expect(lastResolved).toBe(true);
expect(lastRejected).toBe(undefined);
});
Expand Down

0 comments on commit e045521

Please sign in to comment.