Skip to content

Commit

Permalink
Fix setting multiple cookies as multiple 'Set-Cookie' headers. (fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
digrich authored and mcollina committed Jan 3, 2019
1 parent 0ee57e7 commit d7238f3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/reply.js
Expand Up @@ -129,7 +129,14 @@ Reply.prototype.header = function (key, value) {

if (this._headers[_key] && _key === 'set-cookie') {
// https://tools.ietf.org/html/rfc7230#section-3.2.2
this._headers[_key] = [this._headers[_key]].concat(value)
if (typeof this._headers[_key] === 'string') {
this._headers[_key] = [this._headers[_key]]
}
if (Array.isArray(value)) {
Array.prototype.push.apply(this._headers[_key], value)
} else {
this._headers[_key].push(value)
}
} else {
this._headers[_key] = value
}
Expand Down
35 changes: 35 additions & 0 deletions test/internals/reply.test.js
Expand Up @@ -916,3 +916,38 @@ test('.status() is an alias for .code()', t => {
t.is(res.statusCode, 418)
})
})

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

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

fastify.get('/headers', function (req, reply) {
reply
.header('set-cookie', 'one')
.header('set-cookie', 'two')
.header('set-cookie', 'three')
.header('set-cookie', ['four', 'five', 'six'])
.send({})
})

fastify.listen(0, err => {
t.error(err)
fastify.server.unref()

sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/headers'
}, (err, response, body) => {
t.error(err)
t.ok(response.headers['set-cookie'])
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 d7238f3

Please sign in to comment.