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