|
| 1 | +import { afterEach, beforeEach, describe, expect, test } from "bun:test" |
| 2 | +import { getProxyForUrl } from "../../src/util/proxy-env" |
| 3 | + |
| 4 | +const KEYS = ["ALL_PROXY", "all_proxy", "HTTP_PROXY", "http_proxy", "HTTPS_PROXY", "https_proxy", "NO_PROXY", "no_proxy"] as const |
| 5 | +const saved: Record<string, string | undefined> = {} |
| 6 | + |
| 7 | +function snapshotEnv() { |
| 8 | + for (const key of KEYS) saved[key] = process.env[key] |
| 9 | +} |
| 10 | + |
| 11 | +function restoreEnv() { |
| 12 | + for (const key of KEYS) { |
| 13 | + if (saved[key] === undefined) delete process.env[key] |
| 14 | + else process.env[key] = saved[key] |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +describe("getProxyForUrl loopback", () => { |
| 19 | + beforeEach(snapshotEnv) |
| 20 | + afterEach(restoreEnv) |
| 21 | + |
| 22 | + test("does not proxy loopback even when ALL_PROXY is set", () => { |
| 23 | + process.env.ALL_PROXY = "http://127.0.0.1:18764" |
| 24 | + process.env.HTTP_PROXY = "http://127.0.0.1:18764" |
| 25 | + delete process.env.NO_PROXY |
| 26 | + delete process.env.no_proxy |
| 27 | + expect(getProxyForUrl("http://127.0.0.1:8317/v1")).toBeUndefined() |
| 28 | + expect(getProxyForUrl("http://localhost:8317/v1")).toBeUndefined() |
| 29 | + expect(getProxyForUrl("http://[::1]:8317/v1")).toBeUndefined() |
| 30 | + expect(getProxyForUrl("http://0.0.0.0:8317/v1")).toBeUndefined() |
| 31 | + }) |
| 32 | + |
| 33 | + test("strips brackets so [::1] matches loopback bypass", () => { |
| 34 | + process.env.ALL_PROXY = "http://proxy.example:8080" |
| 35 | + delete process.env.NO_PROXY |
| 36 | + delete process.env.no_proxy |
| 37 | + expect(getProxyForUrl("http://[::1]/")).toBeUndefined() |
| 38 | + }) |
| 39 | +}) |
0 commit comments