-
-
Notifications
You must be signed in to change notification settings - Fork 116
feat: add per route busboy configuration #580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
0e5c11d
9fa4a21
6b07a88
5f3c511
8198666
2c398c4
b8f7038
22b19da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -316,3 +316,127 @@ test('should NOT throw fileSize limitation error when throwFileSizeLimit is glob | |
| t.error(error, 'request') | ||
| } | ||
| }) | ||
|
|
||
| test('should throw fileSize limitation error when used alongside attachFieldsToBody and set request config', async function (t) { | ||
| t.plan(1) | ||
|
|
||
| const fastify = Fastify() | ||
| t.teardown(fastify.close.bind(fastify)) | ||
|
|
||
| fastify.register(multipart, { | ||
| attachFieldsToBody: true | ||
| }) | ||
|
|
||
| const randomFileBuffer = Buffer.alloc(2_000_000) | ||
| crypto.randomFillSync(randomFileBuffer) | ||
|
|
||
| fastify.post('/', { | ||
| config: { | ||
| multipartOptions: { | ||
| limits: { | ||
| fileSize: 1_000_000 | ||
|
||
| } | ||
| } | ||
| } | ||
| }, async function (req, reply) { | ||
| t.fail('it should throw') | ||
is2ei marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| reply.status(200).send() | ||
| }) | ||
|
|
||
| await fastify.listen({ port: 0 }) | ||
|
|
||
| // request | ||
| const form = new FormData() | ||
| const opts = { | ||
| hostname: '127.0.0.1', | ||
| port: fastify.server.address().port, | ||
| path: '/', | ||
| headers: form.getHeaders(), | ||
| method: 'POST' | ||
| } | ||
|
|
||
| const tmpFile = 'test/random-file' | ||
| fs.writeFileSync(tmpFile, randomFileBuffer) | ||
|
|
||
| const req = http.request(opts) | ||
| form.append('upload', fs.createReadStream(tmpFile)) | ||
|
|
||
| form.pipe(req) | ||
|
|
||
| try { | ||
| const [res] = await once(req, 'response') | ||
| t.equal(res.statusCode, 413) | ||
| res.resume() | ||
| await once(res, 'end') | ||
|
|
||
| fs.unlinkSync(tmpFile) | ||
| } catch (error) { | ||
| t.error(error, 'request') | ||
| } | ||
| }) | ||
|
|
||
| test('should not throw fileSize limitation error when used alongside attachFieldsToBody and set request config', async function (t) { | ||
| t.plan(4) | ||
|
|
||
| const fastify = Fastify() | ||
| t.teardown(fastify.close.bind(fastify)) | ||
|
|
||
| fastify.register(multipart, { | ||
| attachFieldsToBody: true | ||
| }) | ||
|
|
||
| const randomFileBuffer = Buffer.alloc(900_000) | ||
| crypto.randomFillSync(randomFileBuffer) | ||
|
|
||
| fastify.post('/', { | ||
| config: { | ||
| multipartOptions: { | ||
| limits: { | ||
| fileSize: 1_000_000 | ||
| } | ||
| } | ||
| } | ||
| }, async function (req, reply) { | ||
| t.ok(req.isMultipart()) | ||
|
|
||
| t.same(Object.keys(req.body), ['upload']) | ||
|
|
||
| const content = await req.body.upload.toBuffer() | ||
|
|
||
| t.equal(content.toString(), randomFileBuffer.toString()) | ||
|
|
||
| reply.status(200).send() | ||
| }) | ||
|
|
||
| await fastify.listen({ port: 0 }) | ||
|
|
||
| // request | ||
| const form = new FormData() | ||
| const opts = { | ||
| hostname: '127.0.0.1', | ||
| port: fastify.server.address().port, | ||
| path: '/', | ||
| headers: form.getHeaders(), | ||
| method: 'POST' | ||
| } | ||
|
|
||
| const tmpFile = 'test/random-file' | ||
| fs.writeFileSync(tmpFile, randomFileBuffer) | ||
|
|
||
| const req = http.request(opts) | ||
| form.append('upload', fs.createReadStream(tmpFile)) | ||
|
|
||
| form.pipe(req) | ||
|
|
||
| try { | ||
| const [res] = await once(req, 'response') | ||
| t.equal(res.statusCode, 200) | ||
| res.resume() | ||
| await once(res, 'end') | ||
|
|
||
| fs.unlinkSync(tmpFile) | ||
| } catch (error) { | ||
| t.error(error, 'request') | ||
| } | ||
| }) | ||
Uh oh!
There was an error while loading. Please reload this page.