Skip to content

Commit

Permalink
refactor setCookies and remove istanbul ignore else
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Jan 12, 2024
1 parent 7ad33bc commit 201685b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
23 changes: 11 additions & 12 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,30 @@ function onReqHandlerWrapper (fastify, hook) {
}

function setCookies (reply) {
let setCookie = reply.getHeader('Set-Cookie')
const setCookieIsUndefined = setCookie === undefined
const setCookieHeader = reply.getHeader('Set-Cookie')
let setCookie

/* istanbul ignore else */
if (setCookieIsUndefined) {
if (setCookieHeader === undefined) {
if (reply[kReplySetCookies].size === 1) {
for (const c of reply[kReplySetCookies].values()) {
reply.header('Set-Cookie', cookie.serialize(c.name, c.value, c.opts))
}

// Fast path for single cookie
const c = reply[kReplySetCookies].values().next().value
reply.header('Set-Cookie', cookie.serialize(c.name, c.value, c.opts))
reply[kReplySetCookies].clear()

return
}

setCookie = []
} else if (typeof setCookie === 'string') {
setCookie = [setCookie]
} else if (typeof setCookieHeader === 'string') {
setCookie = [setCookieHeader]
} else {
setCookie = setCookieHeader
}

for (const c of reply[kReplySetCookies].values()) {
setCookie.push(cookie.serialize(c.name, c.value, c.opts))
}

if (!setCookieIsUndefined) reply.removeHeader('Set-Cookie')
if (setCookieHeader !== undefined) reply.removeHeader('Set-Cookie')
reply.header('Set-Cookie', setCookie)
reply[kReplySetCookies].clear()
}
Expand Down
32 changes: 32 additions & 0 deletions test/cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,38 @@ test('should set multiple cookies', (t) => {
})
})

test('should set multiple cookies (an array already exists)', (t) => {
t.plan(10)
const fastify = Fastify()
fastify.register(plugin)

fastify.get('/test1', (req, reply) => {
reply
.header('Set-Cookie', ['bar=bar'])
.setCookie('foo', 'foo', { path: '/' })
.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, 'bar')
t.equal(cookies[0].value, 'bar')
t.equal(cookies[0].path, undefined)

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

test('cookies get set correctly with millisecond dates', (t) => {
t.plan(8)
const fastify = Fastify()
Expand Down
6 changes: 1 addition & 5 deletions types/plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ declare module "fastify" {
* @param value Cookie value
* @param options Serialize options
*/
setCookie(
name: string,
value: string,
options?: fastifyCookie.CookieSerializeOptions
): this;
setCookie: setCookieWrapper;

/**
* @alias setCookie
Expand Down

0 comments on commit 201685b

Please sign in to comment.