Fix User-Agent header

Prevent User-Agent header from growing with each request.

Fixes #83
This commit is contained in:
Matt Loring
2016-05-03 19:45:47 +02:00
parent 561748556d
commit 0d7d51242f
2 changed files with 15 additions and 2 deletions

View File

@@ -39,8 +39,11 @@ DefaultTransporter.prototype.USER_AGENT =
DefaultTransporter.prototype.configure = function(opts) {
// set transporter user agent
opts.headers = opts.headers || {};
opts.headers['User-Agent'] = opts.headers['User-Agent'] ?
opts.headers['User-Agent'] + ' ' + this.USER_AGENT : this.USER_AGENT;
if (!opts.headers['User-Agent']) {
opts.headers['User-Agent'] = this.USER_AGENT;
} else if (opts.headers['User-Agent'].indexOf(this.USER_AGENT) === -1) {
opts.headers['User-Agent'] = opts.headers['User-Agent'] + ' ' + this.USER_AGENT;
}
return opts;
};

View File

@@ -19,6 +19,7 @@
var assert = require('assert');
var DefaultTransporter = require('../lib/transporters');
var nock = require('nock');
var version = require('../package.json').version;
nock.disableNetConnect();
@@ -42,6 +43,15 @@ describe('Transporters', function() {
assert(re.test(opts.headers['User-Agent']));
});
it('should not append default client user agent to the existing user ' +
'agent more than once', function() {
var applicationName = 'MyTestApplication-1.0 google-api-nodejs-client/' + version;
var opts = transporter.configure({
headers: { 'User-Agent': applicationName }
});
assert.equal(opts.headers['User-Agent'], applicationName);
});
it('should create a single error from multiple response errors', function(done) {
var firstError = {
message: 'Error 1'