Skip to content

Commit

Permalink
Check if onSend was ran and keep resetting the map (#242)
Browse files Browse the repository at this point in the history
* set cookie map to null after sendHeaders run

* don'r reassign reply to null

* remove kReplySetCookiesHookRan from onReqHandler

* nit: fix Symbol description
  • Loading branch information
gurgunday committed Aug 1, 2023
1 parent c926f46 commit 54e9bdc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
15 changes: 6 additions & 9 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ const cookie = require('cookie')
const { Signer, sign, unsign } = require('./signer')

const kReplySetCookies = Symbol('fastify.reply.setCookies')
const kReplySetCookiesHookRan = Symbol('fastify.reply.setCookiesHookRan')

function fastifyCookieSetCookie (reply, name, value, options) {
let sendHeaders = false
if (reply[kReplySetCookies] === null) {
sendHeaders = true
reply[kReplySetCookies] = new Map()
}
const opts = Object.assign({}, options)

if (opts.expires && Number.isInteger(opts.expires)) {
Expand All @@ -34,8 +30,9 @@ function fastifyCookieSetCookie (reply, name, value, options) {

reply[kReplySetCookies].set(`${name};${opts.domain};${opts.path || '/'}`, { name, value, opts })

if (sendHeaders) {
if (reply[kReplySetCookiesHookRan]) {
setCookies(reply)
reply[kReplySetCookies].clear()
}

return reply
Expand Down Expand Up @@ -103,9 +100,8 @@ function fastifyCookieOnSendHandler (fastifyReq, fastifyRes, payload, done) {
setCookies(fastifyRes)
}

// Explicitly set the property to null so that we can
// check if the header was already set
fastifyRes[kReplySetCookies] = null
fastifyRes[kReplySetCookies].clear()
fastifyRes[kReplySetCookiesHookRan] = true

done()
}
Expand Down Expand Up @@ -147,6 +143,7 @@ function plugin (fastify, options, next) {

fastify.decorateRequest('cookies', null)
fastify.decorateReply(kReplySetCookies, null)
fastify.decorateReply(kReplySetCookiesHookRan, false)

fastify.decorateReply('cookie', setCookie)
fastify.decorateReply('setCookie', setCookie)
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 54e9bdc

Please sign in to comment.