Skip to content

Commit

Permalink
refactor setCookies and remove istanbul ignore else (#271)
Browse files Browse the repository at this point in the history
* refactor setCookies and remove istanbul ignore else

* remove redundant condition

* update test

* rename variables
  • Loading branch information
gurgunday authored Jan 12, 2024
1 parent 7ad33bc commit 3c666ae
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
29 changes: 14 additions & 15 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,31 @@ function onReqHandlerWrapper (fastify, hook) {
}

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

/* istanbul ignore else */
if (setCookieIsUndefined) {
if (setCookieHeaderValue === 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]
cookieValue = []
} else if (typeof setCookieHeaderValue === 'string') {
cookieValue = [setCookieHeaderValue]
} else {
cookieValue = setCookieHeaderValue
}

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

if (!setCookieIsUndefined) reply.removeHeader('Set-Cookie')
reply.header('Set-Cookie', setCookie)
reply.removeHeader('Set-Cookie')
reply.header('Set-Cookie', cookieValue)
reply[kReplySetCookies].clear()
}

Expand Down
33 changes: 33 additions & 0 deletions test/cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,39 @@ 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: '/' })
.setCookie('foo', 'foo', { path: '/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, 3)
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[2].path, '/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 3c666ae

Please sign in to comment.