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