diff --git a/test/multipart-security.test.js b/test/multipart-security.test.js index af4dd1d5..320cde4f 100644 --- a/test/multipart-security.test.js +++ b/test/multipart-security.test.js @@ -115,18 +115,20 @@ test('should use default for fileSize', async function (t) { }) fastify.post('/', async function (req, reply) { - t.ok(req.isMultipart()) + t.ok(req.isMultipart(), 'is multipart') const part = await req.file() t.pass('the file is not consumed yet') try { await part.toBuffer() + reply.send('not ok') t.fail('it should throw') } catch (error) { t.ok(error) reply.send(error) } + return reply }) await fastify.listen({ port: 0 }) @@ -151,7 +153,7 @@ test('should use default for fileSize', async function (t) { try { const [res] = await once(req, 'response') - t.equal(res.statusCode, 413) + t.equal(res.statusCode, 413, 'status code equal') res.resume() await once(res, 'end') } catch (error) { diff --git a/test/multipart.test.js b/test/multipart.test.js index 95b1fdc3..5c8f077c 100644 --- a/test/multipart.test.js +++ b/test/multipart.test.js @@ -258,8 +258,6 @@ test('should error if boundary is empty', function (t) { }) test('should throw error due to filesLimit (The max number of file fields (Default: Infinity))', function (t) { - t.plan(4) - const fastify = Fastify() t.teardown(fastify.close.bind(fastify)) @@ -269,12 +267,12 @@ test('should throw error due to filesLimit (The max number of file fields (Defau try { const parts = req.files({ limits: { files: 1 } }) for await (const part of parts) { - t.ok(part.file) + t.ok(part.file, 'part received') await sendToWormhole(part.file) } reply.code(200).send() } catch (error) { - t.ok(error instanceof fastify.multipartErrors.FilesLimitError) + t.ok(error instanceof fastify.multipartErrors.FilesLimitError, 'error') reply.code(500).send() } }) @@ -291,22 +289,34 @@ test('should throw error due to filesLimit (The max number of file fields (Defau method: 'POST' } + let ended = false const req = http.request(opts, (res) => { - t.equal(res.statusCode, 500) + t.equal(res.statusCode, 500, 'status code') res.resume() res.on('end', () => { + if (ended) { + return + } + ended = true t.pass('res ended successfully') + t.end() }) }) form.append('upload', fs.createReadStream(filePath)) form.append('upload2', fs.createReadStream(filePath)) form.pipe(req) + req.on('error', (err) => { + if (ended) { + return + } + ended = true + t.equal(err.code, 'ECONNRESET') + t.end() + }) }) }) test('should be able to configure limits globally with plugin register options', function (t) { - t.plan(4) - const fastify = Fastify() t.teardown(fastify.close.bind(fastify)) @@ -326,7 +336,7 @@ test('should be able to configure limits globally with plugin register options', } }) - fastify.listen({ port: 0 }, async function () { + fastify.listen({ port: 0 }, function () { // request const form = new FormData() const opts = { @@ -338,21 +348,31 @@ test('should be able to configure limits globally with plugin register options', method: 'POST' } + let ended = false const req = http.request(opts, (res) => { t.equal(res.statusCode, 500) res.resume() res.on('end', () => { + if (ended) { + return + } + ended = true t.pass('res ended successfully') + t.end() }) }) form.append('upload', fs.createReadStream(filePath)) form.append('upload2', fs.createReadStream(filePath)) + req.on('error', (err) => { + if (ended) { + return + } + ended = true + t.equal(err.code, 'ECONNRESET') + t.end() + }) - try { - await pump(form, req) - } catch (error) { - t.error(error, 'formData request pump: no err') - } + pump(form, req).catch(() => {}) }) }) @@ -376,7 +396,7 @@ test('should throw error due to fieldsLimit (Max number of non-file fields (Defa } }) - fastify.listen({ port: 0 }, async function () { + fastify.listen({ port: 0 }, function () { // request const form = new FormData() const opts = {