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

fix: set user-agent header case-insensitively for http adapter #3922

Closed
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
12 changes: 7 additions & 5 deletions lib/adapters/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var zlib = require('zlib');
var pkg = require('./../../package.json');
var createError = require('../core/createError');
var enhanceError = require('../core/enhanceError');
var normalizeHeaderName = require('../helpers/normalizeHeaderName');

var isHttps = /https:?/;

Expand Down Expand Up @@ -55,16 +56,17 @@ module.exports = function httpAdapter(config) {

// Set User-Agent (required by some servers)
// See https://github.com/axios/axios/issues/69
if ('User-Agent' in headers || 'user-agent' in headers) {
var USER_AGENT_HEADER = 'User-Agent';
normalizeHeaderName(headers, USER_AGENT_HEADER);
if (USER_AGENT_HEADER in headers) {
// 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[USER_AGENT_HEADER]) {
delete headers[USER_AGENT_HEADER];
}
// Otherwise, use specified value
} else {
// Only set header if it hasn't been set in config
headers['User-Agent'] = 'axios/' + pkg.version;
headers[USER_AGENT_HEADER] = 'axios/' + pkg.version;
}

if (data && !utils.isStream(data)) {
Expand Down
15 changes: 15 additions & 0 deletions test/unit/adapters/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -968,5 +968,20 @@ describe('supports http with nodejs', function () {
});
});

it('should apply custom user-agent case-insensitively if one is specified', function (done) {
server = http.createServer(function (req, res) {
assert.equal(req.headers["user-agent"], "custom-ua");
res.end();
}).listen(4444, function () {
axios.get('http://localhost:4444/', {
headers: {
"uSeR-AGenT": "custom-ua"
}
}
).then(function (res) {
done();
});
});
});
});