From d5f249c3347add7ffb9c0467f6226e853ee8dbeb Mon Sep 17 00:00:00 2001 From: vincent178 Date: Thu, 19 Jul 2018 20:51:25 +0800 Subject: [PATCH] remove default plugin level bodyLimit (#18) --- formbody.js | 7 +------ test/integration.test.js | 30 +++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/formbody.js b/formbody.js index d7ece98..ae34fae 100644 --- a/formbody.js +++ b/formbody.js @@ -3,13 +3,8 @@ const fp = require('fastify-plugin') const qs = require('qs') -const DEFAULT_BODY_LIMIT = 1024 * 1024 // 1 MiB -const defaults = { - bodyLimit: DEFAULT_BODY_LIMIT -} - function formBodyPlugin (fastify, options, next) { - const opts = Object.assign({}, defaults, options || {}) + const opts = Object.assign({}, options || {}) function contentParser (req, body, done) { done(null, qs.parse(body.toString())) diff --git a/test/integration.test.js b/test/integration.test.js index 3601d0a..abb6f8d 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -110,7 +110,35 @@ test('cannot exceed body limit set on Fastify instance', (t) => { const fastify = Fastify({bodyLimit: 10}) fastify - .register(plugin, {bodyLimit: undefined}) + .register(plugin) + .post('/limited', (req, res) => { + res.send(Object.assign({}, req.body, {message: 'done'})) + }) + + fastify.listen(0, (err) => { + if (err) tap.error(err) + fastify.server.unref() + + const reqOpts = { + method: 'POST', + baseUrl: 'http://localhost:' + fastify.server.address().port + } + const req = request.defaults(reqOpts) + const payload = require('crypto').randomBytes(128).toString('hex') + req({uri: '/limited', form: {foo: payload}}, (err, response, body) => { + t.error(err) + t.strictEqual(response.statusCode, 413) + t.is(JSON.parse(body).message, 'Request body is too large') + }) + }) +}) + +test('plugin bodyLimit should overwrite Fastify instance bodyLimit', (t) => { + t.plan(3) + const fastify = Fastify({bodyLimit: 100000}) + + fastify + .register(plugin, {bodyLimit: 10}) .post('/limited', (req, res) => { res.send(Object.assign({}, req.body, {message: 'done'})) })