-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: empty bodies does not crash anymore (#420)
- Loading branch information
1 parent
23666d9
commit 5109dce
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict' | ||
|
||
const test = require('tap').test | ||
const FormData = require('form-data') | ||
const Fastify = require('fastify') | ||
const multipart = require('..') | ||
const http = require('http') | ||
const { once } = require('events') | ||
|
||
test('should not break with a empty request body when attachFieldsToBody is true', async function (t) { | ||
t.plan(5) | ||
|
||
const fastify = Fastify() | ||
t.teardown(fastify.close.bind(fastify)) | ||
|
||
fastify.register(multipart, { attachFieldsToBody: true }) | ||
|
||
fastify.post('/', async function (req, reply) { | ||
t.ok(req.isMultipart()) | ||
|
||
const files = await req.saveRequestFiles() | ||
|
||
t.ok(Array.isArray(files)) | ||
t.equal(files.length, 0) | ||
|
||
reply.code(200).send() | ||
}) | ||
|
||
await fastify.listen({ port: 0 }) | ||
|
||
// request | ||
const form = new FormData() | ||
const opts = { | ||
protocol: 'http:', | ||
hostname: 'localhost', | ||
port: fastify.server.address().port, | ||
path: '/', | ||
headers: form.getHeaders(), | ||
method: 'POST' | ||
} | ||
|
||
const req = http.request(opts) | ||
form.pipe(req) | ||
|
||
const [res] = await once(req, 'response') | ||
t.equal(res.statusCode, 200) | ||
res.resume() | ||
await once(res, 'end') | ||
t.pass('res ended successfully') | ||
}) |