Skip to content
Open
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
52 changes: 52 additions & 0 deletions packages/fetch/src/fetch.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,58 @@ describe("fetchwithRequestOptions E2E tests", () => {
});
});

describe("Proxy bypass", () => {
test.each([
["request options", true],
["environment", true],
["different port", false],
["hostname only", true],
])("routes %s entries correctly", async (source, direct) => {
await startHttpServer();
const proxy = http.createServer((_req, res) => {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ proxied: true }));
});
await new Promise<void>((resolve) =>
proxy.listen(0, "127.0.0.1", resolve),
);
serversToCleanup.push(proxy);
const proxyPort = (proxy.address() as { port: number }).port;
const oldNoProxy = process.env.NO_PROXY;
const oldLowerNoProxy = process.env.no_proxy;
delete process.env.NO_PROXY;
delete process.env.no_proxy;
process.env.NO_PROXY =
source === "environment" ? `localhost:${HTTP_PORT}` : "";

try {
const pattern =
source === "hostname only"
? "localhost"
: `localhost:${source === "different port" ? HTTP_PORT + 1 : HTTP_PORT}`;
const response = await fetchwithRequestOptions(
`http://localhost:${HTTP_PORT}/json`,
undefined,
{
proxy: `http://127.0.0.1:${proxyPort}`,
noProxy: source === "environment" ? [] : [pattern],
},
);

expect(await response.json()).toEqual(
direct
? { message: "Hello from HTTP server", method: "GET" }
: { proxied: true },
);
} finally {
if (oldNoProxy === undefined) delete process.env.NO_PROXY;
else process.env.NO_PROXY = oldNoProxy;
if (oldLowerNoProxy === undefined) delete process.env.no_proxy;
else process.env.no_proxy = oldLowerNoProxy;
}
});
});

describe("Request options integration", () => {
test("should merge extra body properties", async () => {
await startHttpServer();
Expand Down
42 changes: 42 additions & 0 deletions packages/fetch/src/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { HttpProxyAgent } from "http-proxy-agent";
import { HttpsProxyAgent } from "https-proxy-agent";
import { Response } from "node-fetch";
import { afterEach, describe, expect, test, vi } from "vitest";
import { fetchwithRequestOptions } from "./fetch.js";
import patchedFetch from "./node-fetch-patch.js";

vi.mock("./node-fetch-patch.js", () => ({ default: vi.fn() }));

afterEach(() => {
vi.unstubAllEnvs();
vi.resetAllMocks();
});

describe("proxy bypass ports", () => {
test.each([
["http:", "80", false],
["https:", "443", false],
["http:", "443", true],
["https:", "80", true],
])(
"uses the effective port for %s and noProxy :%s",
async (protocol, port, proxied) => {
vi.stubEnv("NO_PROXY", "");
vi.stubEnv("no_proxy", "");
vi.mocked(patchedFetch).mockResolvedValue(new Response("ok"));
const url = new URL(`${protocol}//example.com/`);
const originalUrl = url.href;

await fetchwithRequestOptions(url, undefined, {
proxy: "http://127.0.0.1:8080",
noProxy: [`example.com:${port}`],
});

const agent = vi.mocked(patchedFetch).mock.calls[0][1]?.agent;
expect(
agent instanceof HttpProxyAgent || agent instanceof HttpsProxyAgent,
).toBe(proxied);
expect(url.href).toBe(originalUrl);
},
);
});
6 changes: 5 additions & 1 deletion packages/fetch/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ export async function fetchwithRequestOptions(
const proxy = getProxy(url.protocol, requestOptions);

// Check if should bypass proxy based on requestOptions or NO_PROXY env var
const shouldBypass = shouldBypassProxy(url.hostname, requestOptions);
const port = url.port || (url.protocol === "https:" ? "443" : "80");
const shouldBypass = shouldBypassProxy(
`${url.hostname}:${port}`,
requestOptions,
);

// Create agent
const protocol = url.protocol === "https:" ? https : http;
Expand Down
Loading