-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathpost-formbody.test.js
54 lines (45 loc) · 1.61 KB
/
post-formbody.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, {
contentTypesToEncode: ['application/x-www-form-urlencoded']
})
instance.register(require('@fastify/formbody'))
t.test('post-formbody', async (t) => {
t.plan(6)
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/x-www-form-urlencoded')
let data = ''
req.setEncoding('utf8')
req.on('data', (d) => {
data += d
})
req.on('end', () => {
const str = data.toString()
t.same(JSON.parse(data), { some: 'info', another: 'detail' })
res.statusCode = 200
res.setHeader('content-type', 'application/x-www-form-urlencoded')
res.end(str)
})
})
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/x-www-form-urlencoded' },
body: 'some=info&another=detail'
})
t.equal(result.headers['content-type'], 'application/x-www-form-urlencoded')
t.same(await result.body.json(), { some: 'info', another: 'detail' })
})