-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathunix-https-undici.test.js
71 lines (58 loc) · 1.72 KB
/
unix-https-undici.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
'use strict'
const t = require('tap')
const Fastify = require('fastify')
const From = require('..')
const https = require('node:https')
const fs = require('node:fs')
const { request, Agent } = require('undici')
const querystring = require('node:querystring')
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'))
}
if (process.platform === 'win32') {
t.pass()
process.exit(0)
}
const socketPath = `${__filename}.socket`
try {
fs.unlinkSync(socketPath)
} catch (_) {
}
const instance = Fastify({
https: certs
})
instance.register(From, {
base: `unix+https://${querystring.escape(socketPath)}`
})
t.test('unix https undici', 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, '/hello')
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('hello')
})
t.teardown(target.close.bind(target))
await instance.listen({ port: 0 })
await new Promise(resolve => target.listen(socketPath, 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')
})