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 2 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
20 changes: 19 additions & 1 deletion lib/fourOhFour.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function fourOhFour (options) {
const { logger, genReqId } = 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 All @@ -58,6 +58,24 @@ function fourOhFour (options) {
})
}

function onBadUrl (path, req, res) {
const { url, method } = req
const message = `Route ${method}:${url} not found`
const body = `{"error":"Not Found","message":"${message}","statusCode":404}`

// simulate normal route logging
const id = genReqId(req)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to have some feedback about the logging. Do we have a better approach for this one?

const childLogger = logger.child({ reqId: id })
childLogger.info({ req }, 'incoming request')
childLogger.info({ req }, message)
climba03003 marked this conversation as resolved.
Show resolved Hide resolved

res.writeHead(404, {
'Content-Type': 'application/json',
'Content-Length': body.length
})
res.end(body)
}

function setContext (instance, context) {
const _404Context = Object.assign({}, instance[kFourOhFourContext])
_404Context.onSend = context.onSend
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, 404)
t.same(JSON.parse(response.payload), {
error: 'Not Found',
message: 'Route GET:/%c0 not found',
statusCode: 404
})
})
})

t.test('Only / is registered', t => {
climba03003 marked this conversation as resolved.
Show resolved Hide resolved
t.plan(3)
const fastify = Fastify({ logger: true })
fastify.get('/', () => t.fail('we should not be here'))
fastify.inject({
url: '/%c0',
method: 'GET'
}, (err, response) => {
t.error(err)
t.equal(response.statusCode, 404)
t.same(JSON.parse(response.payload), {
error: 'Not Found',
message: 'Route GET:/%c0 not found',
statusCode: 404
})
})
})

t.end()
})

Expand Down