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

Adding ability to omit User-Agent header #3703

Merged
merged 5 commits into from Mar 29, 2021
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
11 changes: 9 additions & 2 deletions lib/adapters/http.js
Expand Up @@ -54,9 +54,16 @@ module.exports = function httpAdapter(config) {
var headers = config.headers;

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

Expand Down
31 changes: 31 additions & 0 deletions test/unit/adapters/http.js
Expand Up @@ -7,6 +7,7 @@ var zlib = require('zlib');
var assert = require('assert');
var fs = require('fs');
var path = require('path');
var pkg = require('./../../../package.json');
var server, proxy;

describe('supports http with nodejs', function () {
Expand Down Expand Up @@ -879,5 +880,35 @@ describe('supports http with nodejs', function () {
});
});
});

it('should supply a user-agent if one is not specified', function (done) {
server = http.createServer(function (req, res) {
assert.equal(req.headers["user-agent"], 'axios/' + pkg.version);
res.end();
}).listen(4444, function () {
axios.get('http://localhost:4444/'
).then(function (res) {
done();
});
});
});

it('should omit a user-agent if one is explicitly disclaimed', function (done) {
server = http.createServer(function (req, res) {
assert.equal("user-agent" in req.headers, false);
assert.equal("User-Agent" in req.headers, false);
res.end();
}).listen(4444, function () {
axios.get('http://localhost:4444/', {
headers: {
"User-Agent": null
}
}
).then(function (res) {
done();
});
});
});

});