Skip to content

Commit

Permalink
Fixing incorrect headers join.
Browse files Browse the repository at this point in the history
Multiple headers were joined by comma which is unacceptable for
`Set-Cookie`. Now multiple headers are returned as array.
Fixes #465
  • Loading branch information
kompot committed Oct 11, 2016
1 parent 12341d8 commit c2a4b20
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/helpers/parseHeaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ module.exports = function parseHeaders(headers) {
val = utils.trim(line.substr(i + 1));

if (key) {
parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
if (parsed[key]) {
parsed[key] = utils.isArray(parsed[key])
// third+ value with that key
? parsed[key].concat(val)
// second value with that key
: [parsed[key]].concat(val);
} else {
parsed[key] = val;
}
}
});

Expand Down
10 changes: 9 additions & 1 deletion test/specs/helpers/parseHeaders.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ describe('helpers::parseHeaders', function () {
'Date: ' + date.toISOString() + '\n' +
'Content-Type: application/json\n' +
'Connection: keep-alive\n' +
'Transfer-Encoding: chunked'
'Transfer-Encoding: chunked\n' +
'Set-Cookie: cookie_key_1=yyy; Path=/\n' +
'Set-Cookie: cookie_key_2=xxx; expires=Mon, 17-Oct-2016 17:16:25 GMT; httponly; Max-Age=1209600; Path=/\n' +
'Set-Cookie: cookie_key_3=zzz; Path=/www'
);

expect(parsed['date']).toEqual(date.toISOString());
expect(parsed['content-type']).toEqual('application/json');
expect(parsed['connection']).toEqual('keep-alive');
expect(parsed['transfer-encoding']).toEqual('chunked');
expect(parsed['set-cookie']).toEqual([
'cookie_key_1=yyy; Path=/',
'cookie_key_2=xxx; expires=Mon, 17-Oct-2016 17:16:25 GMT; httponly; Max-Age=1209600; Path=/',
'cookie_key_3=zzz; Path=/www'
]);
});
});

0 comments on commit c2a4b20

Please sign in to comment.