Skip to content

Commit 047429e

Browse files
authored
avoid warning with exposeHeadRoute route option (fastify#2797)
1 parent 237d534 commit 047429e

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lib/route.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ function buildRouting (options) {
235235
opts.schemaErrorFormatter || this[kSchemaErrorFormatter]
236236
)
237237

238+
const headRouteExists = router.find('HEAD', path) != null
239+
238240
try {
239241
router.on(opts.method, opts.url, { version: opts.version }, routeHandler, context)
240242
} catch (err) {
@@ -245,7 +247,6 @@ function buildRouting (options) {
245247
const { exposeHeadRoute } = opts
246248
const hasRouteExposeHeadRouteFlag = exposeHeadRoute != null
247249
const shouldExposeHead = hasRouteExposeHeadRouteFlag ? exposeHeadRoute : globalExposeHeadRoutes
248-
const headRouteExists = router.find('HEAD', path) != null
249250

250251
if (shouldExposeHead && options.method === 'GET' && !headRouteExists) {
251252
const onSendHandlers = parseHeadOnSendHandlers(opts.onSend)

test/route.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,32 @@ test('HEAD route should respect custom onSend handlers', t => {
819819
})
820820
})
821821

822+
test('no warning for exposeHeadRoute', async t => {
823+
const fastify = Fastify()
824+
825+
fastify.route({
826+
method: 'GET',
827+
path: '/more-coffee',
828+
exposeHeadRoute: true,
829+
async handler () {
830+
return 'hello world'
831+
}
832+
})
833+
834+
const listener = (w) => {
835+
console.error(w)
836+
t.fail('no warning')
837+
}
838+
839+
process.on('warning', listener)
840+
841+
await fastify.listen(0)
842+
843+
process.removeListener('warning', listener)
844+
845+
await fastify.close()
846+
})
847+
822848
test("HEAD route should handle stream.on('error')", t => {
823849
t.plan(6)
824850

0 commit comments

Comments
 (0)