-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
fix: route body limit #36
Changes from 1 commit
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
'use strict' | ||
|
||
const t = require('tap') | ||
const Fastify = require('fastify') | ||
const rawBody = require('../plugin') | ||
|
||
t.test('body limit per route', async t => { | ||
const app = Fastify({ bodyLimit: 5 }) | ||
|
||
const payload = { hello: '123456789012345678901234567890' } | ||
|
||
await app.register(rawBody, { | ||
field: 'rawBody', | ||
global: false, | ||
runFirst: false | ||
}) | ||
|
||
app.post('/100', { | ||
config: { rawBody: true }, | ||
bodyLimit: 100 | ||
}, (req, reply) => { | ||
t.pass('body is ok') | ||
return req.rawBody | ||
}) | ||
|
||
app.post('/50', { | ||
config: { | ||
rawBody: true | ||
}, | ||
bodyLimit: 41 | ||
}, (req, reply) => { | ||
t.fail('body is not ok') | ||
}) | ||
|
||
app.post('/server-limit', { | ||
config: { | ||
rawBody: true | ||
} | ||
}, (req, reply) => { | ||
t.fail('body is not ok') | ||
}) | ||
|
||
await t.test('must not throw if body is smaller than limit', async t => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd specify:
|
||
const res = await app.inject({ | ||
method: 'POST', | ||
url: '/100', | ||
payload | ||
}) | ||
|
||
t.equal(res.statusCode, 200) | ||
t.equal(res.payload, JSON.stringify(payload)) | ||
}) | ||
|
||
await t.test('must reject if body is bigger than limit', async t => { | ||
const res = await app.inject({ | ||
method: 'POST', | ||
url: '/50', | ||
payload | ||
}) | ||
|
||
t.equal(res.statusCode, 413) | ||
t.same(res.json(), { | ||
statusCode: 413, | ||
code: 'FST_ERR_CTP_BODY_TOO_LARGE', | ||
error: 'Payload Too Large', | ||
message: 'Request body is too large' | ||
}) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why using different routes instead of different payloads? I'd rather have just two routes:
And you test both with a bigger and a smaller payload There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd actually split route-limit into two tests: one were route-limit is bigger than server-limit and one where route-limit is smaller then server-limit. This way you can test that the route level limit is always respected There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
await t.test('must reject if body is bigger then the server limit', async t => { | ||
const res = await app.inject({ | ||
method: 'POST', | ||
url: '/server-limit', | ||
payload: { hello: '1' } | ||
}) | ||
|
||
t.equal(res.statusCode, 413) | ||
t.same(res.json(), { | ||
statusCode: 413, | ||
code: 'FST_ERR_CTP_BODY_TOO_LARGE', | ||
error: 'Payload Too Large', | ||
message: 'Request body is too large' | ||
}) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should you check for null or undefined instead? is 0 technically a valid limit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fastify rejects it
I fixed the check anyway by using the
??
operator instead