Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caseless header comparing in HTTP adapter. #2880

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 13 additions & 7 deletions lib/adapters/http.js
Expand Up @@ -52,14 +52,18 @@ module.exports = function httpAdapter(config) {
};
var data = config.data;
var headers = config.headers;
var headerNames = {};

Object.keys(headers).forEach(function storeLowerName(name) {
headerNames[name.toLowerCase()] = name;
});

// Set User-Agent (required by some servers)
// See https://github.com/axios/axios/issues/69
if ('User-Agent' in headers || 'user-agent' in headers) {
if ('user-agent' in headerNames) {
// User-Agent is specified; handle case where no UA header is desired
if (!headers['User-Agent'] && !headers['user-agent']) {
delete headers['User-Agent'];
delete headers['user-agent'];
if (!headers[headerNames['user-agent']]) {
delete headers[headerNames['user-agent']];
}
// Otherwise, use specified value
} else {
Expand All @@ -82,7 +86,9 @@ module.exports = function httpAdapter(config) {
}

// Add Content-Length header if data exists
headers['Content-Length'] = data.length;
if (!headerNames['content-length']) {
headers['Content-Length'] = data.length;
}
}

// HTTP basic authentication
Expand All @@ -105,8 +111,8 @@ module.exports = function httpAdapter(config) {
auth = urlUsername + ':' + urlPassword;
}

if (auth) {
delete headers.Authorization;
if (auth && headerNames.authorization) {
delete headers[headerNames.authorization];
}

var isHttpsRequest = isHttps.test(protocol);
Expand Down
39 changes: 37 additions & 2 deletions test/unit/adapters/http.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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('ж');

Expand Down