-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathhttps-agents.test.js
68 lines (56 loc) · 1.71 KB
/
https-agents.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
'use strict'
const t = require('tap')
const Fastify = require('fastify')
const { request } = require('undici')
const From = require('..')
const http = require('node:http')
const https = require('node:https')
const { Agent } = require('undici')
const fs = require('node:fs')
const path = require('node:path')
const certs = {
key: fs.readFileSync(path.join(__dirname, 'fixtures', 'fastify.key')),
cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'fastify.cert'))
}
const instance = Fastify({
https: certs
})
t.test('https agents', async (t) => {
t.plan(7)
t.teardown(instance.close.bind(instance))
const target = https.createServer(certs, (req, res) => {
t.pass('request proxied')
t.equal(req.method, 'GET')
t.equal(req.url, '/')
res.statusCode = 205
res.setHeader('Content-Type', 'text/plain')
res.setHeader('x-my-header', 'hello!')
res.end('hello world')
})
instance.get('/', (_request, reply) => {
reply.from()
})
t.teardown(target.close.bind(target))
await new Promise(resolve => target.listen({ port: 0 }, resolve))
instance.register(From, {
base: `https://localhost:${target.address().port}`,
http: {
agents: {
'http:': new http.Agent({}),
'https:': new https.Agent({})
}
}
})
await new Promise(resolve => instance.listen({ port: 0 }, resolve))
const result = await request(`https://localhost:${instance.server.address().port}`, {
dispatcher: new Agent({
connect: {
rejectUnauthorized: false
}
})
})
t.equal(result.headers['content-type'], 'text/plain')
t.equal(result.headers['x-my-header'], 'hello!')
t.equal(result.statusCode, 205)
t.equal(await result.body.text(), 'hello world')
})