mirror of
https://github.com/tenrok/axios.git
synced 2026-06-02 16:04:10 +03:00
Adding ability to omit User-Agent header (#3703)
* 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>
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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 () {
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user