Skip to content

Commit

Permalink
fix: HEAD route search (#5078)
Browse files Browse the repository at this point in the history
* fix: HEAD route search

Co-authored-by: Ran Toledo <Ran.Toledo@netapp.com>

* Update lib/route.js

Co-authored-by: Gürgün Dayıoğlu <gurgun.dayioglu@icloud.com>

---------

Co-authored-by: Ran Toledo <Ran.Toledo@netapp.com>
Co-authored-by: Gürgün Dayıoğlu <gurgun.dayioglu@icloud.com>
  • Loading branch information
3 people committed Oct 11, 2023
1 parent b59b0a5 commit 797f27d
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ function buildRouting (options) {
constraints.version = opts.version
}

const headHandler = router.find('HEAD', opts.url, constraints)
const hasHEADHandler = headHandler != null
const headHandler = router.findRoute('HEAD', opts.url, constraints)
const hasHEADHandler = headHandler !== null

// remove the head route created by fastify
if (hasHEADHandler && !context[kRouteByFastify] && headHandler.store[kRouteByFastify]) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
"avvio": "^8.2.1",
"fast-content-type-parse": "^1.0.0",
"fast-json-stringify": "^5.7.0",
"find-my-way": "^7.6.0",
"find-my-way": "^7.7.0",
"light-my-request": "^5.9.1",
"pino": "^8.12.0",
"process-warning": "^2.2.0",
Expand Down
130 changes: 127 additions & 3 deletions test/constrained-routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,91 @@ test('Should allow registering custom constrained routes outside constructor', t
})
})

test('Custom constrained routes registered also for HEAD method generated by fastify', t => {
t.plan(3)

const constraint = {
name: 'secret',
storage: function () {
const secrets = {}
return {
get: (secret) => { return secrets[secret] || null },
set: (secret, store) => { secrets[secret] = store }
}
},
deriveConstraint: (req, ctx) => {
return req.headers['x-secret']
},
validate () { return true }
}

const fastify = Fastify({ constraints: { secret: constraint } })

fastify.route({
method: 'GET',
url: '/',
constraints: { secret: 'mySecret' },
handler: (req, reply) => {
reply.send('from mySecret - my length is 31')
}
})

fastify.inject({
method: 'HEAD',
url: '/',
headers: {
'X-Secret': 'mySecret'
}
}, (err, res) => {
t.error(err)
t.same(res.headers['content-length'], '31')
t.equal(res.statusCode, 200)
})
})

test('Custom constrained routes registered with addConstraintStrategy apply also for HEAD method generated by fastify', t => {
t.plan(3)

const constraint = {
name: 'secret',
storage: function () {
const secrets = {}
return {
get: (secret) => { return secrets[secret] || null },
set: (secret, store) => { secrets[secret] = store }
}
},
deriveConstraint: (req, ctx) => {
return req.headers['x-secret']
},
validate () { return true }
}

const fastify = Fastify()
fastify.addConstraintStrategy(constraint)

fastify.route({
method: 'GET',
url: '/',
constraints: { secret: 'mySecret' },
handler: (req, reply) => {
reply.send('from mySecret - my length is 31')
}
})

fastify.inject({
method: 'HEAD',
url: '/',
headers: {
'X-Secret': 'mySecret'
}
}, (err, res) => {
t.error(err)
t.same(res.headers['content-length'], '31')
t.equal(res.statusCode, 200)
})
})

test('Add a constraint strategy after fastify instance was started', t => {
t.plan(4)

Expand Down Expand Up @@ -561,7 +646,7 @@ test('Should allow registering a constrained GET route after an unconstrained HE
url: '/',
handler: (req, reply) => {
reply.header('content-type', 'text/plain')
reply.send('custom HEAD response')
reply.send('HEAD response: length is about 33')
}
})

Expand All @@ -570,7 +655,8 @@ test('Should allow registering a constrained GET route after an unconstrained HE
url: '/',
constraints: { host: 'fastify.io' },
handler: (req, reply) => {
reply.send({ hello: 'from any other domain' })
reply.header('content-type', 'text/plain')
reply.send('Hello from constrains: length is about 41')
}
})

Expand All @@ -582,7 +668,7 @@ test('Should allow registering a constrained GET route after an unconstrained HE
}
}, (err, res) => {
t.error(err)
t.same(res.payload, 'custom HEAD response')
t.same(res.headers['content-length'], '41')
t.equal(res.statusCode, 200)
})
})
Expand Down Expand Up @@ -774,3 +860,41 @@ test('error in async constraints', async (t) => {
t.equal(statusCode, 500)
}
})

test('Allow regex constraints in routes', t => {
t.plan(5)

const fastify = Fastify()

fastify.route({
method: 'GET',
url: '/',
constraints: { host: /.*\.fastify\.io/ },
handler: (req, reply) => {
reply.send({ hello: 'from fastify dev domain' })
}
})

fastify.inject({
method: 'GET',
url: '/',
headers: {
host: 'dev.fastify.io'
}
}, (err, res) => {
t.error(err)
t.same(JSON.parse(res.payload), { hello: 'from fastify dev domain' })
t.equal(res.statusCode, 200)
})

fastify.inject({
method: 'GET',
url: '/',
headers: {
host: 'google.com'
}
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 404)
})
})

0 comments on commit 797f27d

Please sign in to comment.