Elffuss BraidHub

kikocis/continue public

c690979ba91a · 5.7 KiBRawPermalink
1import { globalAgent } from "https";
2import * as fs from "node:fs";
3import * as os from "node:os";
4import * as path from "node:path";
5import { afterEach, beforeEach, expect, test } from "vitest";
6import { getAgentOptions } from "./getAgentOptions.js";
7
8// Store original env
9const originalEnv = process.env;
10const originalGlobalAgentOptions = { ...globalAgent.options };
11
12// Temporary directory for test certificate files
13let tempDir: string;
14
15beforeEach(() => {
16 // Create a temporary directory for test files
17 tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "fetch-test-"));
18
19 process.env = { ...originalEnv };
20
21 // Reset globalAgent for each test
22 globalAgent.options = { ...originalGlobalAgentOptions };
23});
24
25afterEach(() => {
26 process.env = originalEnv;
27 globalAgent.options = originalGlobalAgentOptions;
28
29 // Clean up temporary directory
30 try {
31 fs.rmSync(tempDir, { recursive: true, force: true });
32 } catch (error) {
33 console.error(`Failed to remove temp directory: ${error}`);
34 }
35});
36
37// Helper function to create test certificate files
38function createTestCertFile(filename: string, content: string): string {
39 const filePath = path.join(tempDir, filename);
40 fs.writeFileSync(filePath, content, "utf8");
41 return filePath;
42}
43
44test("getAgentOptions returns basic configuration with default values", () => {
45 const options = getAgentOptions();
46
47 // Check default timeout (7200 seconds = 2 hours = 7,200,000 ms)
48 expect(options.timeout).toBe(7200000);
49 expect(options.sessionTimeout).toBe(7200000);
50 expect(options.keepAliveMsecs).toBe(7200000);
51 expect(options.keepAlive).toBe(true);
52
53 // Verify certificates array exists and contains items
54 expect(options.ca).toBeInstanceOf(Array);
55 expect(options.ca.length).toBeGreaterThan(0);
56
57 // Verify at least one of the real TLS root certificates is included
58 // This assumes there's at least one certificate with "CERTIFICATE" in it
59 expect(options.ca.some((cert: any) => cert.includes("CERTIFICATE"))).toBe(
60 true,
61 );
62});
63
64test("getAgentOptions respects custom timeout", () => {
65 const customTimeout = 300; // 5 minutes
66 const options = getAgentOptions({ timeout: customTimeout });
67
68 // Check timeout values (300 seconds = 300,000 ms)
69 expect(options.timeout).toBe(300000);
70 expect(options.sessionTimeout).toBe(300000);
71 expect(options.keepAliveMsecs).toBe(300000);
72});
73
74test("getAgentOptions uses verifySsl setting", () => {
75 // With verifySsl true
76 let options = getAgentOptions({ verifySsl: true });
77 expect(options.rejectUnauthorized).toBe(true);
78
79 // With verifySsl false
80 options = getAgentOptions({ verifySsl: false });
81 expect(options.rejectUnauthorized).toBe(false);
82});
83
84test("getAgentOptions incorporates custom CA bundle paths", () => {
85 // Create a test CA bundle file
86 const caBundleContent =
87 "-----BEGIN CERTIFICATE-----\nMIIDtTCCAp2gAwIBAgIJAMcuSp7chAYdMA==\n-----END CERTIFICATE-----";
88 const caBundlePath = createTestCertFile("ca-bundle.pem", caBundleContent);
89
90 // Single string path
91 let options = getAgentOptions({ caBundlePath });
92
93 // Verify that our test certificate is included in the CA list
94 expect(options.ca).toContain(caBundleContent);
95
96 // Create multiple test CA bundle files
97 const caContent1 =
98 "-----BEGIN CERTIFICATE-----\nABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\n-----END CERTIFICATE-----";
99 const caContent2 =
100 "-----BEGIN CERTIFICATE-----\n0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\n-----END CERTIFICATE-----";
101 const caPath1 = createTestCertFile("ca1.pem", caContent1);
102 const caPath2 = createTestCertFile("ca2.pem", caContent2);
103
104 // Array of paths
105 options = getAgentOptions({
106 caBundlePath: [caPath1, caPath2],
107 });
108
109 // Verify that both test certificates are included in the CA list
110 expect(options.ca).toContain(caContent1);
111 expect(options.ca).toContain(caContent2);
112});
113
114test("getAgentOptions includes global certs when running as binary", () => {
115 // Set up test certs in globalAgent
116 globalAgent.options.ca = ["global-cert-1", "global-cert-2"];
117
118 // Set IS_BINARY environment variable
119 process.env.IS_BINARY = "true";
120
121 const options = getAgentOptions();
122
123 // Test for global certs
124 expect(options.ca).toContain("global-cert-1");
125 expect(options.ca).toContain("global-cert-2");
126});
127
128test("getAgentOptions handles client certificate configuration", () => {
129 // Create test certificate files
130 const clientCertContent =
131 "-----BEGIN CERTIFICATE-----\nCLIENTCERT\n-----END CERTIFICATE-----";
132 const clientKeyContent =
133 "-----BEGIN PRIVATE KEY-----\nCLIENTKEY\n-----END PRIVATE KEY-----";
134 const certPath = createTestCertFile("client.cert", clientCertContent);
135 const keyPath = createTestCertFile("client.key", clientKeyContent);
136
137 const clientCertOptions = {
138 clientCertificate: {
139 cert: certPath,
140 key: keyPath,
141 passphrase: "secret-passphrase",
142 },
143 };
144
145 const options = getAgentOptions(clientCertOptions);
146
147 expect(options.cert).toBe(clientCertContent);
148 expect(options.key).toBe(clientKeyContent);
149 expect(options.passphrase).toBe("secret-passphrase");
150});
151
152test("getAgentOptions handles client certificate without passphrase", () => {
153 // Create test certificate files
154 const clientCertContent =
155 "-----BEGIN CERTIFICATE-----\nCLIENTCERT2\n-----END CERTIFICATE-----";
156 const clientKeyContent =
157 "-----BEGIN PRIVATE KEY-----\nCLIENTKEY2\n-----END PRIVATE KEY-----";
158 const certPath = createTestCertFile("client2.cert", clientCertContent);
159 const keyPath = createTestCertFile("client2.key", clientKeyContent);
160
161 const clientCertOptions = {
162 clientCertificate: {
163 cert: certPath,
164 key: keyPath,
165 },
166 };
167
168 const options = getAgentOptions(clientCertOptions);
169
170 expect(options.cert).toBe(clientCertContent);
171 expect(options.key).toBe(clientKeyContent);
172 expect(options.passphrase).toBeUndefined();
173});