-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathfastify-multipart-incompatibility.test.js
84 lines (68 loc) · 2.29 KB
/
fastify-multipart-incompatibility.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
72
73
74
75
76
77
78
79
80
81
82
83
84
'use strict'
const fs = require('node:fs')
const path = require('node:path')
const t = require('tap')
const Fastify = require('fastify')
const { request } = require('undici')
const From = require('..')
const Multipart = require('@fastify/multipart')
const http = require('node:http')
const FormData = require('form-data')
const split = require('split2')
const logStream = split(JSON.parse)
const instance = Fastify({
logger: {
level: 'warn',
stream: logStream
}
})
instance.register(Multipart)
instance.register(From)
t.test('fastify-multipart-incompatibility', async (t) => {
t.plan(9)
t.teardown(instance.close.bind(instance))
const filetPath = path.join(__dirname, 'fixtures', 'file.txt')
const fileContent = fs.readFileSync(filetPath, { encoding: 'utf-8' })
const target = http.createServer((req, res) => {
t.pass('request proxied')
t.equal(req.method, 'POST')
t.match(req.headers['content-type'], /^multipart\/form-data/)
let data = ''
req.setEncoding('utf8')
req.on('data', (d) => {
data += d
})
req.on('end', () => {
t.notMatch(data, 'Content-Disposition: form-data; name="key"')
t.notMatch(data, 'value')
t.notMatch(data, 'Content-Disposition: form-data; name="file"')
t.notMatch(data, fileContent)
res.setHeader('content-type', 'application/json')
res.statusCode = 200
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))
logStream.on('data', (log) => {
if (
log.level === 40 &&
log.msg.match(/@fastify\/reply-from might not behave as expected when used with @fastify\/multipart/)
) {
t.pass('incompatibility warn message logged')
}
})
await new Promise(resolve => target.listen({ port: 0 }, resolve))
const form = new FormData()
form.append('key', 'value')
form.append('file', fs.createReadStream(filetPath, { encoding: 'utf-8' }))
const result = await request(`http://localhost:${instance.server.address().port}`, {
method: 'POST',
headers: form.getHeaders(),
body: form
})
t.same(await result.body.json(), { something: 'else' })
})