Skip to content

Commit

Permalink
Ensure path is relative (#699)
Browse files Browse the repository at this point in the history
* Ensure path is relative

* Add regression test
  • Loading branch information
penalosa authored and mrbbot committed Nov 1, 2023
1 parent a42a47b commit a098c7d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/miniflare/src/workers/core/entry.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function getUserRequest(
// If a custom `upstream` was specified, make sure the URL starts with it
let path = url.pathname + url.search;
// Remove leading slash, so we resolve relative to `upstream`'s path
if (path.startsWith("/")) path = path.substring(1);
if (path.startsWith("/")) path = `./${path.substring(1)}`;
url = new URL(path, upstreamUrl);
}

Expand Down
19 changes: 18 additions & 1 deletion packages/miniflare/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,24 @@ test("Miniflare: fetch mocking", async (t) => {
}
);
});

test("Miniflare: custom upstream as origin (with colons)", async (t) => {
const upstream = await useServer(t, (req, res) => {
res.end(`upstream: ${new URL(req.url ?? "", "http://upstream")}`);
});
const mf = new Miniflare({
upstream: new URL("/extra:extra/", upstream.http.toString()).toString(),
modules: true,
script: `export default {
fetch(request) {
return fetch(request);
}
}`,
});
t.teardown(() => mf.dispose());
// Check rewrites protocol, hostname, and port, but keeps pathname and query
const res = await mf.dispatchFetch("https://random:0/path:path?a=1");
t.is(await res.text(), "upstream: http://upstream/extra:extra/path:path?a=1");
});
test("Miniflare: custom upstream as origin", async (t) => {
const upstream = await useServer(t, (req, res) => {
res.end(`upstream: ${new URL(req.url ?? "", "http://upstream")}`);
Expand Down

0 comments on commit a098c7d

Please sign in to comment.