diff --git a/LICENSE b/LICENSE index 4df364498f..2d9d093448 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2016-2018 The Fastify Team +Copyright (c) 2016-2019 The Fastify Team The Fastify team members are listed at https://github.com/fastify/fastify#team and in the README file. diff --git a/fastify.js b/fastify.js index 26cf76bd94..db89ecf780 100644 --- a/fastify.js +++ b/fastify.js @@ -786,7 +786,7 @@ function build (options) { message: 'Client Error', statusCode: 400 }) - log.error({ err }, 'client error') + log.debug({ err }, 'client error') socket.end(`HTTP/1.1 400 Bad Request\r\nContent-Length: ${body.length}\r\nContent-Type: application/json\r\n\r\n${body}`) } 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']) + }) +})