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

fix: handle invalid url #3128

Merged
merged 3 commits into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ function fastify (options) {
})

// 404 router, used for handling encapsulated 404 handlers
const fourOhFour = build404(options)
const fourOhFour = build404({
logger: options.logger,
genReqId: options.genReqId,
onBadUrl: onBadUrl
})

// HTTP server and its handler
const httpHandler = wrapRouting(router.routing, options)
Expand Down
4 changes: 2 additions & 2 deletions lib/fourOhFour.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ const fourOhFourContext = {
* kFourOhFourContext: the context in the reply object where the handler will be executed
*/
function fourOhFour (options) {
const { logger, genReqId } = options
const { logger, genReqId, onBadUrl } = options

// 404 router, used for handling encapsulated 404 handlers
const router = FindMyWay({ defaultRoute: fourOhFourFallBack })
const router = FindMyWay({ onBadUrl: onBadUrl, defaultRoute: fourOhFourFallBack })

return { router, setNotFoundHandler, setContext, arrange404 }

Expand Down
35 changes: 35 additions & 0 deletions test/404s.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1738,6 +1738,41 @@ test('400 in case of bad url (pre find-my-way v2.2.0 was a 404)', t => {
})
})

t.test('No route registered', t => {
t.plan(3)
const fastify = Fastify()
fastify.inject({
url: '/%c0',
method: 'GET'
}, (err, response) => {
t.error(err)
t.equal(response.statusCode, 400)
t.same(JSON.parse(response.payload), {
error: 'Bad Request',
message: "'%c0' is not a valid url component",
statusCode: 400
climba03003 marked this conversation as resolved.
Show resolved Hide resolved
})
})
})

t.test('Only / is registered', t => {
climba03003 marked this conversation as resolved.
Show resolved Hide resolved
t.plan(3)
const fastify = Fastify()
fastify.get('/', () => t.fail('we should not be here'))
fastify.inject({
url: '/%c0',
method: 'GET'
}, (err, response) => {
t.error(err)
t.equal(response.statusCode, 400)
t.same(JSON.parse(response.payload), {
error: 'Bad Request',
message: "'%c0' is not a valid url component",
statusCode: 400
})
})
})

t.end()
})

Expand Down