forked from derhuerst/gemini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
62 lines (52 loc) · 1.33 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
'use strict'
const createCert = require('create-cert')
const {promisify: pify} = require('util')
const {strictEqual} = require('assert')
const collect = require('get-stream')
const {
createServer,
DEFAULT_PORT,
request,
} = require('.')
const r = pify(request)
const onRequest = (req, res) => {
console.log('request', req.url)
if (req.clientFingerprint) console.log('client fingerprint:', req.clientFingerprint)
if (req.path === '/foo') {
if (!req.clientFingerprint) {
return res.requestTransientClientCert('/foo is secret!')
}
res.write('foo')
res.end('!')
} else if (req.path === '/bar') {
res.redirect('/foo')
} else {
res.gone()
}
}
const onError = (err) => {
console.error(err)
process.exit(1)
}
;(async () => {
const server = createServer({
tlsOpt: await createCert('example.org'),
}, onRequest)
server.on('error', onError)
await pify(server.listen.bind(server))(DEFAULT_PORT)
const res1 = await r('/bar', {
tlsOpt: {rejectUnauthorized: false},
})
strictEqual(res1.statusCode, 30)
strictEqual(res1.meta, '/foo')
const res2 = await r('/bar', {
tlsOpt: {rejectUnauthorized: false},
followRedirects: true,
useClientCerts: true,
letUserConfirmClientCertUsage: (_, cb) => cb(true),
})
strictEqual(res2.statusCode, 20)
strictEqual(await collect(res2), 'foo!')
server.close()
})()
.catch(onError)