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 support for response that does not set a cookie #247

Merged
merged 1 commit into from
Aug 2, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' })
})
})