2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-14 18:42:33 +03:00

Caseless header comparing in HTTP adapter. (#2880)

* Caseless header comparing in HTTP adapter.

It was adding User-Agent and removing Authorization, but only when
existing headers had the exact right casing. Node uses caseless logic
when managing headers.

This was causing some requests to have `User-Agent` appended to the
headers object and overriding provided agent strings.

Also included is an update to not override the `Content-Length` if it
was already defined in the options. It can be desirable to manually
specify a content length that does not match the data on hand.
Especially for testing.

* Fix eslint error

* fixup: update state UA logic

Play nice with https://github.com/axios/axios/pull/3703

Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
Matt R. Wilson
2021-09-07 12:30:30 -06:00
committed by GitHub
parent 4091b075f6
commit f3ca6371ca
2 changed files with 50 additions and 9 deletions
+37 -2
View File
@@ -18,7 +18,7 @@ describe('supports http with nodejs', function () {
server = null;
}
if (proxy) {
proxy.close()
proxy.close();
proxy = null;
}
if (process.env.http_proxy) {
@@ -326,7 +326,7 @@ describe('supports http with nodejs', function () {
res.end(req.headers.authorization);
}).listen(4444, function () {
var auth = { username: 'foo', password: 'bar' };
var headers = { Authorization: 'Bearer 1234' };
var headers = { AuThOrIzAtIoN: 'Bearer 1234' }; // wonky casing to ensure caseless comparison
axios.get('http://localhost:4444/', { auth: auth, headers: headers }).then(function (res) {
var base64 = Buffer.from('foo:bar', 'utf8').toString('base64');
assert.equal(res.data, 'Basic ' + base64);
@@ -335,6 +335,41 @@ describe('supports http with nodejs', function () {
});
});
it('should provides a default User-Agent header', function (done) {
server = http.createServer(function (req, res) {
res.end(req.headers['user-agent']);
}).listen(4444, function () {
axios.get('http://localhost:4444/').then(function (res) {
assert.ok(/^axios\/[\d.]+$/.test(res.data), `User-Agent header does not match: ${res.data}`);
done();
});
});
});
it('should allow the User-Agent header to be overridden', function (done) {
server = http.createServer(function (req, res) {
res.end(req.headers['user-agent']);
}).listen(4444, function () {
var headers = { 'UsEr-AgEnT': 'foo bar' }; // wonky casing to ensure caseless comparison
axios.get('http://localhost:4444/', { headers }).then(function (res) {
assert.equal(res.data, 'foo bar');
done();
});
});
});
it('should allow the Content-Length header to be overridden', function (done) {
server = http.createServer(function (req, res) {
assert.strictEqual(req.headers['content-length'], '42');
res.end();
}).listen(4444, function () {
var headers = { 'CoNtEnT-lEnGtH': '42' }; // wonky casing to ensure caseless comparison
axios.post('http://localhost:4444/', 'foo', { headers }).then(function () {
done();
});
});
});
it('should support max content length', function (done) {
var str = Array(100000).join('ж');