-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest.js
110 lines (98 loc) · 2.52 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import createCert from 'create-cert'
import {promisify} from 'node:util'
import {strictEqual, fail} from 'node:assert'
import collect from 'get-stream'
import {
createServer,
DEFAULT_PORT,
request,
} from './index.js'
const r = promisify(request)
const onRequest = (req, res) => {
console.log('request', req.url)
if (req.clientFingerprint) console.log('client fingerprint:', req.clientFingerprint)
if (req.path === '/foo') {
setTimeout(() => {
if (!req.clientFingerprint) {
return res.requestTransientClientCert('/foo is secret!')
}
res.write('foo')
res.end('!')
}, 500)
} else if (req.path === '/bar') {
setTimeout(() => {
res.redirect('/foo')
}, 500)
} else {
res.gone()
}
}
const onError = (err) => {
console.error(err)
process.exit(1)
}
{
const server = createServer({
tlsOpt: await createCert('example.org'),
}, onRequest)
server.on('error', onError)
await promisify(server.listen.bind(server))(DEFAULT_PORT)
const res1 = await r('/bar', {
tlsOpt: {rejectUnauthorized: false},
})
strictEqual(res1.statusCode, 30)
strictEqual(res1.meta, '/foo')
const baseOpts = {
tlsOpt: {rejectUnauthorized: false},
followRedirects: true,
useClientCerts: true,
letUserConfirmClientCertUsage: (_, cb) => cb(true),
}
const res2 = await r('/bar', {
...baseOpts,
})
strictEqual(res2.statusCode, 20)
strictEqual(await collect(res2), 'foo!')
{
let threw = false
try {
await r('/bar', {
...baseOpts,
useClientCerts: false,
})
} catch (err) {
strictEqual(err.message, 'server request client cert, but client is configured not to send one', 'err.message is invalid')
threw = true
}
if (!threw) fail(`request() didn't throw despite short timeout`)
}
{
let threw = false
try {
await r('/bar', {
...baseOpts,
headersTimeout: 100, // too short for the mock server to respond
})
} catch (err) {
strictEqual(err.code, 'ETIMEDOUT', 'err.code is invalid')
strictEqual(err.message, 'timeout waiting for response headers', 'err.message is invalid')
threw = true
}
if (!threw) fail(`request() didn't throw despite short timeout`)
}
{
let threw = false
try {
await r('/bar', {
...baseOpts,
timeout: 100, // too short for the mock server to send the body
})
} catch (err) {
strictEqual(err.code, 'ETIMEDOUT', 'err.code is invalid')
strictEqual(err.message, 'timeout waiting for first byte of the response', 'err.message is invalid')
threw = true
}
if (!threw) fail(`request() didn't throw despite short timeout`)
}
server.close()
}