|
| 1 | +import { describe, expect, it } from "bun:test"; |
| 2 | +import * as tls from "node:tls"; |
| 3 | + |
| 4 | +describe("tls.createSecureContext extra arguments test", () => { |
| 5 | + it("should throw an error if the privateKeyEngine is not a string", () => { |
| 6 | + // @ts-expect-error |
| 7 | + expect(() => tls.createSecureContext({ privateKeyIdentifier: "valid", privateKeyEngine: 0 })).toThrow( |
| 8 | + "string, null, or undefined", |
| 9 | + ); |
| 10 | + // @ts-expect-error |
| 11 | + expect(() => tls.createSecureContext({ privateKeyIdentifier: "valid", privateKeyEngine: true })).toThrow( |
| 12 | + "string, null, or undefined", |
| 13 | + ); |
| 14 | + // @ts-expect-error |
| 15 | + expect(() => tls.createSecureContext({ privateKeyIdentifier: "valid", privateKeyEngine: {} })).toThrow( |
| 16 | + "string, null, or undefined", |
| 17 | + ); |
| 18 | + }); |
| 19 | + |
| 20 | + it("should throw an error if the privateKeyIdentifier is not a string", () => { |
| 21 | + // @ts-expect-error |
| 22 | + expect(() => tls.createSecureContext({ privateKeyIdentifier: 0, privateKeyEngine: "valid" })).toThrow( |
| 23 | + "string, null, or undefined", |
| 24 | + ); |
| 25 | + // @ts-expect-error |
| 26 | + expect(() => tls.createSecureContext({ privateKeyIdentifier: true, privateKeyEngine: "valid" })).toThrow( |
| 27 | + "string, null, or undefined", |
| 28 | + ); |
| 29 | + // @ts-expect-error |
| 30 | + expect(() => tls.createSecureContext({ privateKeyIdentifier: {}, privateKeyEngine: "valid" })).toThrow( |
| 31 | + "string, null, or undefined", |
| 32 | + ); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should throw with a valid privateKeyIdentifier but missing privateKeyEngine", () => { |
| 36 | + expect(() => tls.createSecureContext({ privateKeyIdentifier: "valid" })).toThrow( |
| 37 | + "The property 'options.privateKeyEngine' is invalid. Received undefined", |
| 38 | + ); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should not throw for invalid privateKeyEngine when privateKeyIdentifier is not provided", () => { |
| 42 | + // Node.js does not throw an error in the case where only privateKeyEngine is provided, even if |
| 43 | + // the key is invalid. The checks for both keys are only done when privateKeyIdentifier is passed. |
| 44 | + // Verifiable with: `node -p 'tls.createSecureContext({ privateKeyEngine: 0 })'` |
| 45 | + |
| 46 | + // @ts-expect-error |
| 47 | + expect(() => tls.createSecureContext({ privateKeyEngine: 0 })).not.toThrow(); |
| 48 | + // @ts-expect-error |
| 49 | + expect(() => tls.createSecureContext({ privateKeyEngine: true })).not.toThrow(); |
| 50 | + // @ts-expect-error |
| 51 | + expect(() => tls.createSecureContext({ privateKeyEngine: {} })).not.toThrow(); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should throw for invalid privateKeyIdentifier", () => { |
| 55 | + // @ts-expect-error |
| 56 | + expect(() => tls.createSecureContext({ privateKeyIdentifier: 0 })).toThrow( |
| 57 | + "The property 'options.privateKeyEngine' is invalid. Received undefined", |
| 58 | + ); |
| 59 | + |
| 60 | + // @ts-expect-error |
| 61 | + expect(() => tls.createSecureContext({ privateKeyIdentifier: true })).toThrow( |
| 62 | + "The property 'options.privateKeyEngine' is invalid. Received undefined", |
| 63 | + ); |
| 64 | + |
| 65 | + // @ts-expect-error |
| 66 | + expect(() => tls.createSecureContext({ privateKeyIdentifier: {} })).toThrow( |
| 67 | + "The property 'options.privateKeyEngine' is invalid. Received undefined", |
| 68 | + ); |
| 69 | + }); |
| 70 | +}); |
0 commit comments