Skip to content
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

remove default plugin level bodyLimit #18

Merged
merged 1 commit into from
Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions formbody.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
30 changes: 29 additions & 1 deletion test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'}))
})
Expand Down