diff --git a/lib/reply.js b/lib/reply.js index 533bad81ac..acc60826c3 100644 --- a/lib/reply.js +++ b/lib/reply.js @@ -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 } diff --git a/test/internals/reply.test.js b/test/internals/reply.test.js index 8e8ea8e52f..f429444d68 100644 --- a/test/internals/reply.test.js +++ b/test/internals/reply.test.js @@ -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']) + }) +})