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

Removes double call to afterRouteAdded when ignoreTrailingSlash === true #1675

Merged
merged 2 commits into from
Jun 4, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function buildRouting (options) {
let modifyCoreObjects
let genReqId
let disableRequestLogging
let ignoreTrailingSlash

let closing = false

Expand All @@ -65,6 +66,7 @@ function buildRouting (options) {
modifyCoreObjects = options.modifyCoreObjects
genReqId = options.genReqId
disableRequestLogging = options.disableRequestLogging
ignoreTrailingSlash = options.ignoreTrailingSlash
},
routing: router.lookup.bind(router), // router func to find the right handler to call
route, // configure a route in the fastify instance
Expand Down Expand Up @@ -142,7 +144,10 @@ function buildRouting (options) {
case 'both':
default:
afterRouteAdded.call(this, '', notHandledErr, done)
afterRouteAdded.call(this, path, notHandledErr, done)
// If ignoreTrailingSlash is set to true we need to add only the '' route to prevent adding an incomplete one.
if (ignoreTrailingSlash !== true) {
fox1t marked this conversation as resolved.
Show resolved Hide resolved
afterRouteAdded.call(this, path, notHandledErr, done)
}
}
} else if (path[0] === '/' && prefix.endsWith('/')) {
// Ensure that '/prefix/' + '/route' gets registered as '/prefix/route'
Expand Down
31 changes: 31 additions & 0 deletions test/route-prefix.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,37 @@ test('matches both /prefix and /prefix/ with a / route - prefixTrailingSlash: "
})
})

test('returns 404 status code with /prefix/ and / route - prefixTrailingSlash: "both" (default), ignoreTrailingSlash: true', t => {
t.plan(2)
const fastify = Fastify({
ignoreTrailingSlash: true
})

fastify.register(function (fastify, opts, next) {
fastify.route({
method: 'GET',
url: '/',
handler: (req, reply) => {
reply.send({ hello: 'world' })
}
})

next()
}, { prefix: '/prefix/' })

fastify.inject({
method: 'GET',
url: '/prefix//'
}, (err, res) => {
t.error(err)
t.same(JSON.parse(res.payload), {
'error': 'Not Found',
'message': 'Not Found',
'statusCode': 404
})
})
})

test('matches only /prefix with a / route - prefixTrailingSlash: "no-slash", ignoreTrailingSlash: false', t => {
t.plan(4)
const fastify = Fastify({
Expand Down