Skip to content

Commit

Permalink
Fix setting multiple cookies as multiple 'Set-Cookie' headers. (fix f…
Browse files Browse the repository at this point in the history
  • Loading branch information
digrich committed Jan 1, 2019
1 parent 3abbdeb commit 2980063
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
11 changes: 7 additions & 4 deletions lib/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,14 @@ Reply.prototype.header = function (key, value) {

if (this[kReplyHeaders][_key] && _key === 'set-cookie') {
// https://tools.ietf.org/html/rfc7230#section-3.2.2
var _cookies = this[kReplyHeaders][_key]
if (typeof _cookies === 'string') {
_cookies = [_cookies]
if (typeof this[kReplyHeaders][_key] === 'string') {
this[kReplyHeaders][_key] = [this[kReplyHeaders][_key]]
}
if (Array.isArray(value)) {
Array.prototype.push.apply(this[kReplyHeaders][_key], value)
} else {
this[kReplyHeaders][_key].push(value)
}
this[kReplyHeaders][_key] = _cookies.concat(value)
} else {
this[kReplyHeaders][_key] = value
}
Expand Down
12 changes: 9 additions & 3 deletions test/internals/reply.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ test('.status() is an alias for .code()', t => {
})

test('reply.header setting multiple cookies as multiple Set-Cookie headers', t => {
t.plan(4)
t.plan(7)

const fastify = require('../../')()

Expand All @@ -914,7 +914,7 @@ test('reply.header setting multiple cookies as multiple Set-Cookie headers', t =
.header('set-cookie', 'one')
.header('set-cookie', 'two')
.header('set-cookie', 'three')
.header('set-cookie', 'four')
.header('set-cookie', ['four', 'five', 'six'])
.send({})
})

Expand All @@ -928,7 +928,13 @@ test('reply.header setting multiple cookies as multiple Set-Cookie headers', t =
}, (err, response, body) => {
t.error(err)
t.ok(response.headers['set-cookie'])
t.strictDeepEqual(response.headers['set-cookie'], ['one', 'two', 'three', 'four'])
t.strictDeepEqual(response.headers['set-cookie'], ['one', 'two', 'three', 'four', 'five', 'six'])
})
})

fastify.inject('/headers', (error, response) => {
t.error(error)
t.ok(response.headers['set-cookie'])
t.strictDeepEqual(response.headers['set-cookie'], ['one', 'two', 'three', 'four', 'five', 'six'])
})
})

0 comments on commit 2980063

Please sign in to comment.