Skip to content

Commit

Permalink
chore: tests NODE_TLS_REJECT_UNAUTHORIZED (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
reggi authored May 21, 2024
1 parent d523a93 commit 4a7fb89
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions test/https-with-ca-option.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,42 @@ t.before(() => new Promise((res) => {
s.end(`${q.method} ${q.url}`)
}).listen(port, res)
}))

t.teardown(() => server.close())

const failure = {
name: 'FetchError',
message: `request to ${base}hello failed, reason: unable to verify the first certificate`,
code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE',
errno: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE',
type: 'system',
}

// this test will fail after Jan 30 23:23:26 2025 GMT
t.test('make https request without ca, should fail', t =>
t.rejects(fetch(`${base}hello`), {
name: 'FetchError',
message: `request to ${base}hello failed, reason: unable to verify the first certificate`,
code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE',
errno: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE',
type: 'system',
}))

t.test('make https request with rejectUnauthorized:false, succeeds', async t =>
t.equal(await (await fetch(`${base}hello`, { rejectUnauthorized: false })).text(),
'GET /hello'))

t.test('make https request with ca, succeeds', async t =>
t.equal(await (await fetch(`${base}hello`, { ca })).text(),
'GET /hello'))
t.test('make https request without ca, should fail', async t => {
await t.rejects(fetch(`${base}hello`), failure)
})

t.test('make https request with NODE_TLS_REJECT_UNAUTHORIZED set to 1, should fail', async t => {
t.teardown(() => delete process.env.NODE_TLS_REJECT_UNAUTHORIZED)
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '1'
await t.rejects(fetch(`${base}hello`), failure)
})

t.test('make https request with rejectUnauthorized:true, should fail', async t => {
await t.rejects(fetch(`${base}hello`, { rejectUnauthorized: true }), failure)
})

t.test('make https request with NODE_TLS_REJECT_UNAUTHORIZED set to 0, succeeds', async t => {
t.teardown(() => delete process.env.NODE_TLS_REJECT_UNAUTHORIZED)
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
t.equal(await (await fetch(`${base}hello`)).text(), 'GET /hello')
})

t.test('make https request with rejectUnauthorized:false, succeeds', async t => {
t.equal(await (await fetch(`${base}hello`, { rejectUnauthorized: false })).text(), 'GET /hello')
})

t.test('make https request with ca, succeeds', async t => {
t.equal(await (await fetch(`${base}hello`, { ca })).text(), 'GET /hello')
})

0 comments on commit 4a7fb89

Please sign in to comment.