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

feat: add request.routeOptions object #4397

Merged
merged 20 commits into from Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/Reference/Request.md
Expand Up @@ -27,6 +27,7 @@ Request is a core Fastify object containing the following fields:
- `protocol` - the protocol of the incoming request (`https` or `http`)
- `method` - the method of the incoming request
- `url` - the URL of the incoming request
- `routeBodyLimit` - either server limit or route limit
debadutta98 marked this conversation as resolved.
Show resolved Hide resolved
- `routerMethod` - the method defined for the router that is handling the
request
- `routerPath` - the path pattern defined for the router that is handling the
Expand Down Expand Up @@ -90,6 +91,7 @@ fastify.post('/:params', options, function (request, reply) {
console.log(request.protocol)
console.log(request.url)
console.log(request.routerMethod)
console.log(request.routeBodyLimit)
debadutta98 marked this conversation as resolved.
Show resolved Hide resolved
console.log(request.routerPath)
request.log.info('some info')
})
Expand Down
7 changes: 7 additions & 0 deletions lib/request.js
Expand Up @@ -165,6 +165,13 @@ Object.defineProperties(Request.prototype, {
return this[kRouteContext].config.url
}
},
routeBodyLimit: {
get () {
const serverLimit = this[kRouteContext]?.server?.initialConfig?.bodyLimit
debadutta98 marked this conversation as resolved.
Show resolved Hide resolved
const routeLimit = this[kRouteContext]?._parserOptions?.limit
debadutta98 marked this conversation as resolved.
Show resolved Hide resolved
return (routeLimit || serverLimit)
}
},
routerMethod: {
get () {
return this[kRouteContext].config.method
Expand Down
63 changes: 62 additions & 1 deletion test/bodyLimit.test.js
Expand Up @@ -6,7 +6,7 @@ const t = require('tap')
const test = t.test

test('bodyLimit', t => {
t.plan(5)
t.plan(10)
debadutta98 marked this conversation as resolved.
Show resolved Hide resolved

try {
Fastify({ bodyLimit: 1.3 })
Expand All @@ -28,6 +28,21 @@ test('bodyLimit', t => {
reply.send({ error: 'handler should not be called' })
})

fastify.post('/route-limit', {
bodyLimit: 1000,
handler (request, reply) {
t.equal(1000, request.routeBodyLimit)
reply.send({ error: 'handler should not be called' })
debadutta98 marked this conversation as resolved.
Show resolved Hide resolved
}
})

fastify.post('/server-limit', {
handler (request, reply) {
t.equal(1, request.routeBodyLimit)
reply.send({ error: 'handler should not be called' })
debadutta98 marked this conversation as resolved.
Show resolved Hide resolved
}
})

fastify.listen({ port: 0 }, function (err) {
t.error(err)
t.teardown(() => { fastify.close() })
Expand All @@ -42,5 +57,51 @@ test('bodyLimit', t => {
t.error(err)
t.equal(response.statusCode, 413)
})
sget({
method: 'POST',
url: 'http://localhost:' + fastify.server.address().port + '/route-limit',
headers: { 'Content-Type': 'application/json' },
body: [],
json: true
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
})
sget({
method: 'POST',
url: 'http://localhost:' + fastify.server.address().port + '/server-limit',
headers: { 'Content-Type': 'application/json' },
body: [],
json: true
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 413)
debadutta98 marked this conversation as resolved.
Show resolved Hide resolved
})
})
})

test('default request.routeBodyLimit should be 1048576', t => {
t.plan(4)
const fastify = Fastify()
fastify.post('/default-bodylimit', {
handler (request, reply) {
t.equal(1048576, request.routeBodyLimit)
reply.send({ error: 'handler should not be called' })
debadutta98 marked this conversation as resolved.
Show resolved Hide resolved
}
})
fastify.listen({ port: 0 }, function (err) {
t.error(err)
t.teardown(() => { fastify.close() })

sget({
method: 'POST',
url: 'http://localhost:' + fastify.server.address().port + '/default-bodylimit',
headers: { 'Content-Type': 'application/json' },
body: [],
json: true
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
})
})
})
1 change: 1 addition & 0 deletions test/types/request.test-d.ts
Expand Up @@ -66,6 +66,7 @@ const getHandler: RouteHandler = function (request, _reply) {
expectType<string>(request.method)
expectType<string>(request.routerPath)
expectType<string>(request.routerMethod)
expectType<number>(request.routeBodyLimit)
expectType<boolean>(request.is404)
expectType<string>(request.hostname)
expectType<string>(request.ip)
Expand Down
1 change: 1 addition & 0 deletions types/request.d.ts
Expand Up @@ -66,6 +66,7 @@ export interface FastifyRequest<RouteGeneric extends RouteGenericInterface = Rou
readonly method: string;
readonly routerPath: string;
readonly routerMethod: string;
readonly routeBodyLimit: number;
readonly is404: boolean;
readonly socket: RawRequest['socket'];

Expand Down