Skip to content

Commit

Permalink
Removes double call to afterRouteAdded when ignoreTrailingSlash === t…
Browse files Browse the repository at this point in the history
…rue (#1675)

* Removes double call to afterRouteAdded when ignoreTrailingSlash === true

* Adds explaining comment when adding a prefixed plugin
  • Loading branch information
fox1t authored and delvedor committed Jun 4, 2019
1 parent fc52dc3 commit ecea232
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/route.js
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) {
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
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

0 comments on commit ecea232

Please sign in to comment.