-
-
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.
feat breaking: throw file size limit on toBuffer calls (#265)
* add toBuffer aligned to throwFileSizeLimit * add docs
- Loading branch information
Showing
3 changed files
with
155 additions
and
1 deletion.
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
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,133 @@ | ||
'use strict' | ||
|
||
const util = require('util') | ||
const crypto = require('crypto') | ||
const test = require('tap').test | ||
const FormData = require('form-data') | ||
const Fastify = require('fastify') | ||
const multipart = require('..') | ||
const http = require('http') | ||
const stream = require('stream') | ||
const pump = util.promisify(stream.pipeline) | ||
const EventEmitter = require('events') | ||
const { once } = EventEmitter | ||
|
||
test('should throw fileSize limitation error when consuming the stream', async function (t) { | ||
t.plan(4) | ||
|
||
const fastify = Fastify() | ||
t.teardown(fastify.close.bind(fastify)) | ||
|
||
fastify.register(multipart, { | ||
throwFileSizeLimit: true, | ||
limits: { | ||
fileSize: 524288 | ||
} | ||
}) | ||
|
||
fastify.post('/', async function (req, reply) { | ||
t.ok(req.isMultipart()) | ||
|
||
const part = await req.file() | ||
t.pass('the file is not consumed yet') | ||
|
||
try { | ||
await part.toBuffer() | ||
t.fail('it should throw') | ||
} catch (error) { | ||
t.ok(error) | ||
reply.send(error) | ||
} | ||
}) | ||
|
||
await fastify.listen(0) | ||
|
||
// request | ||
const form = new FormData() | ||
const opts = { | ||
protocol: 'http:', | ||
hostname: 'localhost', | ||
port: fastify.server.address().port, | ||
path: '/', | ||
headers: form.getHeaders(), | ||
method: 'POST' | ||
} | ||
|
||
const randomFileBuffer = Buffer.alloc(600000) | ||
crypto.randomFillSync(randomFileBuffer) | ||
|
||
const req = http.request(opts) | ||
form.append('upload', randomFileBuffer) | ||
|
||
pump(form, req) | ||
|
||
try { | ||
const [res] = await once(req, 'response') | ||
t.equal(res.statusCode, 413) | ||
res.resume() | ||
await once(res, 'end') | ||
} catch (error) { | ||
t.error(error, 'request') | ||
} | ||
}) | ||
|
||
test('should NOT throw fileSize limitation error when consuming the stream', async function (t) { | ||
t.plan(5) | ||
|
||
const fastify = Fastify() | ||
t.teardown(fastify.close.bind(fastify)) | ||
|
||
fastify.register(multipart, { | ||
throwFileSizeLimit: false, | ||
limits: { | ||
fileSize: 524288 | ||
} | ||
}) | ||
const fileInputLength = 600000 | ||
|
||
fastify.post('/', async function (req, reply) { | ||
t.ok(req.isMultipart()) | ||
|
||
const part = await req.file() | ||
t.pass('the file is not consumed yet') | ||
|
||
try { | ||
const buffer = await part.toBuffer() | ||
t.ok(part.file.truncated) | ||
t.notSame(buffer.length, fileInputLength) | ||
reply.send(new fastify.multipartErrors.FilesLimitError()) | ||
} catch (error) { | ||
t.fail('it should not throw') | ||
} | ||
}) | ||
|
||
await fastify.listen(0) | ||
|
||
// request | ||
const form = new FormData() | ||
const opts = { | ||
protocol: 'http:', | ||
hostname: 'localhost', | ||
port: fastify.server.address().port, | ||
path: '/', | ||
headers: form.getHeaders(), | ||
method: 'POST' | ||
} | ||
|
||
const randomFileBuffer = Buffer.alloc(fileInputLength) | ||
crypto.randomFillSync(randomFileBuffer) | ||
|
||
const req = http.request(opts) | ||
form.append('upload', randomFileBuffer) | ||
|
||
pump(form, req) | ||
|
||
try { | ||
const [res] = await once(req, 'response') | ||
t.equal(res.statusCode, 413) | ||
res.resume() | ||
await once(res, 'end') | ||
} catch (error) { | ||
t.error(error, 'request') | ||
} | ||
}) |