/
custom-undici-instance.js
77 lines (65 loc) · 2.2 KB
/
custom-undici-instance.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
'use strict'
const t = require('tap')
const Fastify = require('fastify')
const undici = require('undici')
const http = require('http')
const get = require('simple-get').concat
const From = require('..')
const target = http.createServer((req, res) => {
t.pass('request proxied')
t.equal(req.method, 'GET')
t.equal(req.url, '/')
t.equal(req.headers.connection, 'keep-alive')
res.statusCode = 205
res.setHeader('Content-Type', 'text/plain')
res.setHeader('x-my-header', 'hello!')
res.end('hello world')
})
t.test('use a custom instance of \'undici\'', async t => {
t.teardown(target.close.bind(target))
await new Promise((resolve, reject) => target.listen({ port: 0 }, err => err ? reject(err) : resolve()))
t.test('custom Pool', t => {
const instance = Fastify()
t.teardown(instance.close.bind(instance))
instance.register(From, {
base: `http://localhost:${target.address().port}`,
undici: new undici.Pool(`http://localhost:${target.address().port}`)
})
instance.get('/', (request, reply) => {
reply.from()
})
instance.listen({ port: 0 }, (err) => {
t.error(err)
get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
t.error(err)
t.equal(res.headers['content-type'], 'text/plain')
t.equal(res.headers['x-my-header'], 'hello!')
t.equal(res.statusCode, 205)
t.equal(data.toString(), 'hello world')
t.end()
})
})
})
t.test('custom Client', t => {
const instance = Fastify()
t.teardown(instance.close.bind(instance))
instance.register(From, {
base: `http://localhost:${target.address().port}`,
undici: new undici.Client(`http://localhost:${target.address().port}`)
})
instance.get('/', (request, reply) => {
reply.from()
})
instance.listen({ port: 0 }, (err) => {
t.error(err)
get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
t.error(err)
t.equal(res.headers['content-type'], 'text/plain')
t.equal(res.headers['x-my-header'], 'hello!')
t.equal(res.statusCode, 205)
t.equal(data.toString(), 'hello world')
t.end()
})
})
})
})