Skip to content

Commit

Permalink
Adding ability to omit User-Agent header (#3703)
Browse files Browse the repository at this point in the history
* Adding ability to omit User-Agent header

* Update line to be more compact

* Add unit tests for user-agent cases

Co-authored-by: Jay <jasonsaayman@gmail.com>
  • Loading branch information
MikeBishop and jasonsaayman committed Mar 29, 2021
1 parent c0317b7 commit b0959f0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
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();
});
});
});

});

0 comments on commit b0959f0

Please sign in to comment.