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 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
24 changes: 22 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, disableRequestLogging } = 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,26 @@ 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
if (!disableRequestLogging) {
const id = genReqId(req)
const childLogger = logger.child({ reqId: id })
childLogger.info({ req }, 'incoming request')
childLogger.info({ req }, message)
}

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
19 changes: 19 additions & 0 deletions test/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,25 @@ test('should not log incoming request and outgoing response when disabled', t =>
})
})

test('should not log incoming request and outgoing response for 404 onBadUrl when disabled', t => {
t.plan(1)
const lines = []
const dest = new stream.Writable({
write: function (chunk, enc, cb) {
lines.push(JSON.parse(chunk))
cb()
}
})
const fastify = Fastify({ disableRequestLogging: true, logger: { level: 'info', stream: dest } })

fastify.inject({
url: '/%c0',
method: 'GET'
}, (e, res) => {
t.same(lines.length, 0)
})
})

test('should pass when using unWritable props in the logger option', t => {
t.plan(1)
Fastify({
Expand Down