Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

macosSystemProxy.test.ts

141 lines4.7 KB

1import { assert, describe, it } from "@effect/vitest";
2​
3import { macosSystemProxyEnv } from "./macosSystemProxy.ts";
4​
5const PROXY = [
6 "HTTPEnable : 1",
7 "HTTPPort : 3128",
8 "HTTPProxy : proxy.corp",
9 "HTTPSEnable : 1",
10 "HTTPSPort : 3128",
11 "HTTPSProxy : proxy.corp",
12];
13​
14const scutil = (...lines: ReadonlyArray<string>) =>
15 ["<dictionary> {", ...lines.map((line) => ` ${line}`), "}", ""].join("\n");
16​
17const exceptions = (...hosts: ReadonlyArray<string>) => [
18 "ExceptionsList : <array> {",
19 ...hosts.map((host, index) => ` ${index} : ${host}`),
20 "}",
21];
22​
23// npm's matcher (@npmcli/agent): NO_PROXY entries are dot-separated suffixes, never ranges.
24function npmBypasses(noProxy: string, hostname: string): boolean {
25 const host = hostname.split(".").toReversed();
26 return noProxy.split(",").some((entry) => {
27 const segments = entry.split(".").filter(Boolean).toReversed();
28 return segments.length > 0 && segments.every((segment, index) => host[index] === segment);
29 });
31​
32describe("macosSystemProxyEnv", () => {
33 it("maps one shared HTTP and HTTPS proxy and keeps macOS's direct hosts direct", () => {
34 const env = macosSystemProxyEnv(
35 scutil(
36 ...exceptions("*.Local", "169.254/16", "INTERNAL.CORP", "FD00::5"),
37 "FTPPassive : 1",
38 ...PROXY,
39 "__SUPPLEMENTAL__ : <array> {",
40 "}",
41 "__SCOPED__ : <dictionary> {",
42 " en0 : <dictionary> {",
43 " HTTPEnable : 1",
44 " HTTPPort : 9999",
45 " HTTPProxy : 10.9.9.9",
46 " }",
47 "}",
48 ),
49 ["registry.corp"],
50 );
51​
52 assert.strictEqual(env.HTTP_PROXY, "http://proxy.corp:3128");
53 assert.strictEqual(env.https_proxy, "http://proxy.corp:3128");
54 assert.strictEqual(env.NO_PROXY, env.no_proxy);
55 const noProxy = env.NO_PROXY ?? "";
56 assert.strictEqual(
57 noProxy.split(",").slice(0, 10).join(","),
58 "registry.corp,localhost,127.0.0.1,::1,[::1],.local,internal.corp,fd00::5,[fd00::5],0.0.0.0/1",
59 );
60 for (const direct of [
61 "127.0.0.2",
62 "169.254.10.20",
63 "10.1.2.3",
64 "printer.local",
65 "internal.corp",
66 "registry.corp",
67 "[fd00::5]",
68 "localhost",
69 ]) {
70 assert.isTrue(npmBypasses(noProxy, direct), direct);
71 }
72 assert.isFalse(npmBypasses(noProxy, "registry.npmjs.org"));
73 });
74​
75 it.each([
76 { name: "an HTTPS-only proxy", lines: ["HTTPEnable : 0"], http: undefined },
77 { name: "an HTTP-only proxy", lines: ["HTTPSEnable : 0"], https: undefined },
78 {
79 name: "different HTTP and HTTPS proxies",
80 lines: ["HTTPPort : 8080"],
81 http: "http://proxy.corp:8080",
82 },
83 ])("maps $name per scheme", ({ lines, ...expected }) => {
84 const env = macosSystemProxyEnv(scutil(...PROXY, ...lines));
85 const proxies = {
86 http: "http://proxy.corp:3128",
87 https: "http://proxy.corp:3128",
88 ...expected,
89 };
90 assert.strictEqual(env.HTTP_PROXY, proxies.http);
91 assert.strictEqual(env.http_proxy, proxies.http);
92 assert.strictEqual(env.HTTPS_PROXY, proxies.https);
93 assert.strictEqual(env.https_proxy, proxies.https);
94 assert.isTrue(npmBypasses(env.no_proxy ?? "", "127.0.0.2"));
95 });
96​
97 it("brackets an IPv6 proxy host", () => {
98 assert.strictEqual(
99 macosSystemProxyEnv(
100 scutil(
101 "HTTPEnable : 1",
102 "HTTPPort : 8443",
103 "HTTPProxy : fd00::1",
104 "HTTPSEnable : 1",
105 "HTTPSPort : 8443",
106 "HTTPSProxy : fd00::1",
107 ),
108 ).HTTPS_PROXY,
109 "http://[fd00::1]:8443",
110 );
111 });
112​
113 it.each([
114 { name: "no proxy", lines: [] },
115 { name: "a disabled proxy", lines: [...PROXY, "HTTPEnable : 0", "HTTPSEnable : 0"] },
116 { name: "a proxy without a port", lines: PROXY.filter((line) => !line.includes("Port")) },
117 { name: "an authenticated proxy", lines: [...PROXY, "HTTPUser : julia"] },
118 { name: "a PAC file", lines: [...PROXY, "ProxyAutoConfigEnable : 1"] },
119 { name: "proxy auto-discovery", lines: [...PROXY, "ProxyAutoDiscoveryEnable : 1"] },
120 {
121 name: "a per-domain VPN proxy",
122 lines: [
123 ...PROXY,
124 "__SUPPLEMENTAL__ : <array> {",
125 " 0 : <dictionary> {",
126 " HTTPEnable : 1",
127 " HTTPPort : 8080",
128 " HTTPProxy : vpn-proxy.corp",
129 " SupplementalMatchDomain : corp",
130 " }",
131 "}",
132 ],
133 },
134 { name: "simple hostnames excluded", lines: [...PROXY, "ExcludeSimpleHostnames : 1"] },
135 { name: "a bypass-all exception", lines: [...PROXY, ...exceptions("*")] },
136 { name: "an IPv6 range exception", lines: [...PROXY, ...exceptions("fe80::/10")] },
137 { name: "a mid-name wildcard exception", lines: [...PROXY, ...exceptions("build*.corp")] },
138 ])("sets nothing with $name", ({ lines }) => {
139 assert.deepStrictEqual(macosSystemProxyEnv(scutil(...lines)), {});
140 });
141});