2
0
mirror of https://github.com/tenrok/axios.git synced 2026-05-15 11:59:42 +03:00

Duplicate header handling (#874)

* Update parseHeaders to match node http behavior

Node ignores duplicate entries for certain HTTP headers.

It also always converts the `set-cookie` header into an array.

* add tests for new duplicate header handling

* clarify comment
This commit is contained in:
Tyler Brown
2017-08-12 08:58:10 -04:00
committed by Rubén Norte
parent 2b8562694e
commit fb08e95603
2 changed files with 44 additions and 2 deletions
+17 -1
View File
@@ -2,6 +2,15 @@
var utils = require('./../utils');
// Headers whose duplicates are ignored by node
// c.f. https://nodejs.org/api/http.html#http_message_headers
var ignoreDuplicateOf = [
'age', 'authorization', 'content-length', 'content-type', 'etag',
'expires', 'from', 'host', 'if-modified-since', 'if-unmodified-since',
'last-modified', 'location', 'max-forwards', 'proxy-authorization',
'referer', 'retry-after', 'user-agent'
];
/**
* Parse headers into an object
*
@@ -29,7 +38,14 @@ 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] && ignoreDuplicateOf.indexOf(key) >= 0) {
return;
}
if (key === 'set-cookie') {
parsed[key] = (parsed[key] ? parsed[key] : []).concat([val]);
} else {
parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
}
}
});
+27 -1
View File
@@ -15,5 +15,31 @@ describe('helpers::parseHeaders', function () {
expect(parsed['connection']).toEqual('keep-alive');
expect(parsed['transfer-encoding']).toEqual('chunked');
});
});
it('should use array for set-cookie', function() {
var parsedZero = parseHeaders('');
var parsedSingle = parseHeaders(
'Set-Cookie: key=val;'
);
var parsedMulti = parseHeaders(
'Set-Cookie: key=val;\n' +
'Set-Cookie: key2=val2;\n'
);
expect(parsedZero['set-cookie']).toBeUndefined();
expect(parsedSingle['set-cookie']).toEqual(['key=val;']);
expect(parsedMulti['set-cookie']).toEqual(['key=val;', 'key2=val2;']);
});
it('should handle duplicates', function() {
var parsed = parseHeaders(
'Age: age-a\n' + // age is in ignore duplicates blacklist
'Age: age-b\n' +
'Foo: foo-a\n' +
'Foo: foo-b\n'
);
expect(parsed['age']).toEqual('age-a');
expect(parsed['foo']).toEqual('foo-a, foo-b');
});
});