|
| 1 | +import http from "node:http"; |
| 2 | +import https from "node:https"; |
| 3 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 4 | +import { getDirectAgentForCdp, hasProxyEnv, withNoProxyForLocalhost } from "./cdp-proxy-bypass.js"; |
| 5 | + |
| 6 | +describe("cdp-proxy-bypass", () => { |
| 7 | + describe("getDirectAgentForCdp", () => { |
| 8 | + it("returns http.Agent for http://localhost URLs", () => { |
| 9 | + const agent = getDirectAgentForCdp("http://localhost:9222"); |
| 10 | + expect(agent).toBeInstanceOf(http.Agent); |
| 11 | + }); |
| 12 | + |
| 13 | + it("returns http.Agent for http://127.0.0.1 URLs", () => { |
| 14 | + const agent = getDirectAgentForCdp("http://127.0.0.1:9222/json/version"); |
| 15 | + expect(agent).toBeInstanceOf(http.Agent); |
| 16 | + }); |
| 17 | + |
| 18 | + it("returns https.Agent for wss://localhost URLs", () => { |
| 19 | + const agent = getDirectAgentForCdp("wss://localhost:9222"); |
| 20 | + expect(agent).toBeInstanceOf(https.Agent); |
| 21 | + }); |
| 22 | + |
| 23 | + it("returns http.Agent for ws://[::1] URLs", () => { |
| 24 | + const agent = getDirectAgentForCdp("ws://[::1]:9222"); |
| 25 | + expect(agent).toBeInstanceOf(http.Agent); |
| 26 | + }); |
| 27 | + |
| 28 | + it("returns undefined for non-loopback URLs", () => { |
| 29 | + expect(getDirectAgentForCdp("http://remote-host:9222")).toBeUndefined(); |
| 30 | + expect(getDirectAgentForCdp("https://example.com:9222")).toBeUndefined(); |
| 31 | + }); |
| 32 | + |
| 33 | + it("returns undefined for invalid URLs", () => { |
| 34 | + expect(getDirectAgentForCdp("not-a-url")).toBeUndefined(); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + describe("hasProxyEnv", () => { |
| 39 | + const proxyVars = [ |
| 40 | + "HTTP_PROXY", |
| 41 | + "http_proxy", |
| 42 | + "HTTPS_PROXY", |
| 43 | + "https_proxy", |
| 44 | + "ALL_PROXY", |
| 45 | + "all_proxy", |
| 46 | + ]; |
| 47 | + const saved: Record<string, string | undefined> = {}; |
| 48 | + |
| 49 | + beforeEach(() => { |
| 50 | + for (const v of proxyVars) { |
| 51 | + saved[v] = process.env[v]; |
| 52 | + } |
| 53 | + for (const v of proxyVars) { |
| 54 | + delete process.env[v]; |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + for (const v of proxyVars) { |
| 60 | + if (saved[v] !== undefined) { |
| 61 | + process.env[v] = saved[v]; |
| 62 | + } else { |
| 63 | + delete process.env[v]; |
| 64 | + } |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + it("returns false when no proxy vars set", () => { |
| 69 | + expect(hasProxyEnv()).toBe(false); |
| 70 | + }); |
| 71 | + |
| 72 | + it("returns true when HTTP_PROXY is set", () => { |
| 73 | + process.env.HTTP_PROXY = "http://proxy:8080"; |
| 74 | + expect(hasProxyEnv()).toBe(true); |
| 75 | + }); |
| 76 | + |
| 77 | + it("returns true when ALL_PROXY is set", () => { |
| 78 | + process.env.ALL_PROXY = "socks5://proxy:1080"; |
| 79 | + expect(hasProxyEnv()).toBe(true); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe("withNoProxyForLocalhost", () => { |
| 84 | + const saved: Record<string, string | undefined> = {}; |
| 85 | + const vars = ["HTTP_PROXY", "NO_PROXY", "no_proxy"]; |
| 86 | + |
| 87 | + beforeEach(() => { |
| 88 | + for (const v of vars) { |
| 89 | + saved[v] = process.env[v]; |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + afterEach(() => { |
| 94 | + for (const v of vars) { |
| 95 | + if (saved[v] !== undefined) { |
| 96 | + process.env[v] = saved[v]; |
| 97 | + } else { |
| 98 | + delete process.env[v]; |
| 99 | + } |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + it("sets NO_PROXY when proxy is configured", async () => { |
| 104 | + process.env.HTTP_PROXY = "http://proxy:8080"; |
| 105 | + delete process.env.NO_PROXY; |
| 106 | + delete process.env.no_proxy; |
| 107 | + |
| 108 | + let capturedNoProxy: string | undefined; |
| 109 | + await withNoProxyForLocalhost(async () => { |
| 110 | + capturedNoProxy = process.env.NO_PROXY; |
| 111 | + }); |
| 112 | + |
| 113 | + expect(capturedNoProxy).toContain("localhost"); |
| 114 | + expect(capturedNoProxy).toContain("127.0.0.1"); |
| 115 | + expect(capturedNoProxy).toContain("[::1]"); |
| 116 | + // Restored after |
| 117 | + expect(process.env.NO_PROXY).toBeUndefined(); |
| 118 | + }); |
| 119 | + |
| 120 | + it("extends existing NO_PROXY", async () => { |
| 121 | + process.env.HTTP_PROXY = "http://proxy:8080"; |
| 122 | + process.env.NO_PROXY = "internal.corp"; |
| 123 | + |
| 124 | + let capturedNoProxy: string | undefined; |
| 125 | + await withNoProxyForLocalhost(async () => { |
| 126 | + capturedNoProxy = process.env.NO_PROXY; |
| 127 | + }); |
| 128 | + |
| 129 | + expect(capturedNoProxy).toContain("internal.corp"); |
| 130 | + expect(capturedNoProxy).toContain("localhost"); |
| 131 | + // Restored |
| 132 | + expect(process.env.NO_PROXY).toBe("internal.corp"); |
| 133 | + }); |
| 134 | + |
| 135 | + it("skips when no proxy env is set", async () => { |
| 136 | + delete process.env.HTTP_PROXY; |
| 137 | + delete process.env.HTTPS_PROXY; |
| 138 | + delete process.env.ALL_PROXY; |
| 139 | + delete process.env.NO_PROXY; |
| 140 | + |
| 141 | + await withNoProxyForLocalhost(async () => { |
| 142 | + expect(process.env.NO_PROXY).toBeUndefined(); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + it("restores env even on error", async () => { |
| 147 | + process.env.HTTP_PROXY = "http://proxy:8080"; |
| 148 | + delete process.env.NO_PROXY; |
| 149 | + |
| 150 | + await expect( |
| 151 | + withNoProxyForLocalhost(async () => { |
| 152 | + throw new Error("boom"); |
| 153 | + }), |
| 154 | + ).rejects.toThrow("boom"); |
| 155 | + |
| 156 | + expect(process.env.NO_PROXY).toBeUndefined(); |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
0 commit comments