Files
bun.sh/test/js/node/tls/test-use-system-ca.test.ts
Ciro Spaciari 7798e6638b Implement NODE_USE_SYSTEM_CA with --use-system-ca CLI flag (#22441)
### What does this PR do?
Resume work on https://github.com/oven-sh/bun/pull/21898
### How did you verify your code works?
Manually tested on MacOS, Windows 11 and Ubuntu 25.04. CI changes are
needed for the tests

---------

Co-authored-by: Claude Bot <claude-bot@bun.sh>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
2025-09-24 21:55:57 -07:00

70 lines
2.3 KiB
TypeScript

import { spawn } from "bun";
import { describe, expect, test } from "bun:test";
import { bunEnv, bunExe } from "harness";
describe("--use-system-ca", () => {
test("flag loads system certificates", async () => {
// Test that --use-system-ca loads system certificates
await using proc = spawn({
cmd: [bunExe(), "--use-system-ca", "-e", "console.log('OK')"],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(exitCode).toBe(0);
expect(stdout.trim()).toBe("OK");
expect(stderr).toBe("");
});
test("NODE_USE_SYSTEM_CA=1 loads system certificates", async () => {
// Test that NODE_USE_SYSTEM_CA environment variable works
await using proc = spawn({
cmd: [bunExe(), "-e", "console.log('OK')"],
env: { ...bunEnv, NODE_USE_SYSTEM_CA: "1" },
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(exitCode).toBe(0);
expect(stdout.trim()).toBe("OK");
expect(stderr).toBe("");
});
test("NODE_USE_SYSTEM_CA=0 doesn't load system certificates", async () => {
// Test that NODE_USE_SYSTEM_CA=0 doesn't load system certificates
await using proc = spawn({
cmd: [bunExe(), "-e", "console.log('OK')"],
env: { ...bunEnv, NODE_USE_SYSTEM_CA: "0" },
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(exitCode).toBe(0);
expect(stdout.trim()).toBe("OK");
expect(stderr).toBe("");
});
test("--use-system-ca overrides NODE_USE_SYSTEM_CA=0", async () => {
// Test that CLI flag takes precedence over environment variable
await using proc = spawn({
cmd: [bunExe(), "--use-system-ca", "-e", "console.log('OK')"],
env: { ...bunEnv, NODE_USE_SYSTEM_CA: "0" },
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(exitCode).toBe(0);
expect(stdout.trim()).toBe("OK");
expect(stderr).toBe("");
});
});