Skip to content

Commit

Permalink
feat: Do not set User-Agent if the header is explicitly set to null
Browse files Browse the repository at this point in the history
  • Loading branch information
paambaati authored and fengmk2 committed Oct 23, 2018
1 parent 6605500 commit c79eefc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/urllib.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ function requestWithCallback(url, args, callback) {
};
if (args.headers) {
for (var k in args.headers) {
// ignore null and undefined value header
if (args.headers[k] === null || args.headers[k] === undefined) {
continue;
}
options.headers[k] = args.headers[k];
}
}
Expand Down Expand Up @@ -568,9 +572,20 @@ function requestWithCallback(url, args, callback) {
};
}

// set user-agent
if (!options.headers['User-Agent'] && !options.headers['user-agent']) {
options.headers['User-Agent'] = USER_AGENT;
// don't set user-agent
if (args.headers && (args.headers['User-Agent'] === null || args.headers['user-agent'] === null)) {
if (options.headers['user-agent']) {
delete options.headers['user-agent'];
}
if (options.headers['User-Agent']) {
delete options.headers['User-Agent'];
}
} else {
// need to set user-agent
var hasAgentHeader = options.headers['User-Agent'] || options.headers['user-agent'];
if (!hasAgentHeader) {
options.headers['User-Agent'] = USER_AGENT;
}
}

if (args.gzip) {
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ var server = http.createServer(function (req, res) {
} else if (req.url.indexOf('/get') === 0) {
res.writeHeader(200);
return res.end(req.url);
} else if (req.url.indexOf('/headers') === 0) {
res.setHeader('content-type', 'application/json');
res.writeHeader(200);
return res.end(JSON.stringify(req.headers));
} else if (req.url === '/wrongjson') {
res.writeHeader(200);
return res.end(new Buffer('{"foo":""'));
Expand Down
25 changes: 25 additions & 0 deletions test/urllib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,21 @@ describe('test/urllib.test.js', function () {
});
});

it('should omit any header that is explicitly set to null', function (done) {
urllib.request(host + '/headers', {
headers: {
DNT: null
},
dataType: 'json'
}, function(err, data, res) {
assert(!err);
assert(res.statusCode === 200);
assert(!data.dnt);
assert(!data.DNT);
done();
});
});

if (process.platform !== 'win32') {
it('should redirect with writeStream and make sure res resume', function (done) {
coffee.fork(path.join(__dirname, 'redirect.js'))
Expand Down Expand Up @@ -1440,6 +1455,16 @@ describe('test/urllib.test.js', function () {
});
});

it('should return no user agent if user-agent header is set to null', function (done) {
urllib.request(host + '/ua', {dataType: 'json', headers: {'user-agent': null}}, function (err, data, res) {
console.log('data = ', data);
assert(!err);
assert(!data['user-agent']);
assert(res.statusCode === 200);
done();
});
});

it('should return mock user agent', function (done) {
urllib.request(host + '/ua', {dataType: 'json', headers: {'user-agent': 'mock agent'}},
function (err, data, res) {
Expand Down

0 comments on commit c79eefc

Please sign in to comment.