-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathfull-post.test.js
54 lines (44 loc) · 1.37 KB
/
full-post.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
'use strict'
const t = require('tap')
const Fastify = require('fastify')
const { request } = require('undici')
const From = require('..')
const http = require('node:http')
const instance = Fastify()
instance.register(From)
t.test('full post', async (t) => {
t.plan(5)
t.teardown(instance.close.bind(instance))
const target = http.createServer((req, res) => {
t.pass('request proxied')
t.equal(req.method, 'POST')
t.equal(req.headers['content-type'], 'application/json')
let data = ''
req.setEncoding('utf8')
req.on('data', (d) => {
data += d
})
req.on('end', () => {
t.same(JSON.parse(data), { hello: 'world' })
res.statusCode = 200
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify({ something: 'else' }))
})
})
instance.post('/', (_request, reply) => {
reply.from(`http://localhost:${target.address().port}`)
})
t.teardown(target.close.bind(target))
await new Promise(resolve => instance.listen({ port: 0 }, resolve))
await new Promise(resolve => target.listen({ port: 0 }, resolve))
const result = await request(`http://localhost:${instance.server.address().port}`, {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
hello: 'world'
})
})
t.same(await result.body.json(), { something: 'else' })
})