Skip to content

Commit

Permalink
set cookie map to null after sendHeaders run
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Aug 1, 2023
1 parent d83e093 commit 0e38fe6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function fastifyCookieSetCookie (reply, name, value, options) {
sendHeaders = true
reply[kReplySetCookies] = new Map()
}

const opts = Object.assign({}, options)

if (opts.expires && Number.isInteger(opts.expires)) {
Expand All @@ -36,6 +37,7 @@ function fastifyCookieSetCookie (reply, name, value, options) {

if (sendHeaders) {
setCookies(reply)
reply[kReplySetCookies] = null
}

return reply
Expand Down
39 changes: 39 additions & 0 deletions test/cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1187,3 +1187,42 @@ test('cookies get set correctly if set inside onSend', (t) => {
t.equal(cookies[0].path, '/')
})
})

test('cookies get set correctly if set inside multiple onSends', (t) => {
t.plan(10)
const fastify = Fastify()
fastify.register(plugin)

fastify.addHook('onSend', async (req, reply, payload) => {
reply.setCookie('foo', 'foo', { path: '/' })
})

fastify.addHook('onSend', async (req, reply, payload) => {
reply.setCookie('foo', 'foos', { path: '/' })
return payload
})

fastify.get('/test1', (req, reply) => {
reply
.send({ hello: 'world' })
})

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

const cookies = res.cookies
t.equal(cookies.length, 2)
t.equal(cookies[0].name, 'foo')
t.equal(cookies[0].value, 'foo')
t.equal(cookies[0].path, '/')

t.equal(cookies[1].name, 'foo')
t.equal(cookies[1].value, 'foos')
t.equal(cookies[1].path, '/')
})
})

0 comments on commit 0e38fe6

Please sign in to comment.