mirror of
https://github.com/tenrok/axios.git
synced 2026-06-23 20:40:40 +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:
@@ -2,6 +2,15 @@
|
|||||||
|
|
||||||
var utils = require('./../utils');
|
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
|
* Parse headers into an object
|
||||||
*
|
*
|
||||||
@@ -29,7 +38,14 @@ module.exports = function parseHeaders(headers) {
|
|||||||
val = utils.trim(line.substr(i + 1));
|
val = utils.trim(line.substr(i + 1));
|
||||||
|
|
||||||
if (key) {
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -15,5 +15,31 @@ describe('helpers::parseHeaders', function () {
|
|||||||
expect(parsed['connection']).toEqual('keep-alive');
|
expect(parsed['connection']).toEqual('keep-alive');
|
||||||
expect(parsed['transfer-encoding']).toEqual('chunked');
|
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');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user