Skip to content

Commit

Permalink
Fix support for response that does not set a cookie (#247)
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <hello@matteocollina.com>
  • Loading branch information
mcollina committed Aug 2, 2023
1 parent 06ecb1d commit fbed571
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ function setCookies (reply) {
}

function fastifyCookieOnSendHandler (fastifyReq, fastifyRes, payload, done) {
if (!fastifyRes[kReplySetCookies]) {
done()
return
}

if (fastifyRes[kReplySetCookies].size) {
setCookies(fastifyRes)
}
Expand Down
22 changes: 22 additions & 0 deletions test/cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1252,3 +1252,25 @@ test('cookies get set correctly if set inside onRequest', (t) => {
t.equal(cookies[0].path, '/')
})
})

test('do not crash if the onRequest hook is not run', (t) => {
t.plan(3)
const fastify = Fastify()
fastify.addHook('onRequest', async (req, reply) => {
return reply.send({ hello: 'world' })
})

fastify.register(plugin)

fastify.inject({
method: 'GET',
url: '/test1',
headers: {
cookie: 'foo=foo'
}
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.same(JSON.parse(res.body), { hello: 'world' })
})
})

0 comments on commit fbed571

Please sign in to comment.