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

refactor setCookies and remove istanbul ignore else #271

Merged
merged 4 commits into from
Jan 12, 2024
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
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
jsumners marked this conversation as resolved.
Show resolved Hide resolved
}

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