Skip to content

Commit

Permalink
move UA formatting to util
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Sep 9, 2016
1 parent db0f6d3 commit ed14c4a
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 22 deletions.
12 changes: 7 additions & 5 deletions packages/common/src/grpc-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ var through = require('through2');
*/
var Service = require('./service.js');

/**
* @type {module:common/util}
* @private
*/
var util = require('./util.js');

/**
* @const {object} - A map of protobuf codes to HTTP status codes.
* @private
Expand Down Expand Up @@ -170,11 +176,7 @@ function GrpcService(config, options) {
}

this.maxRetries = options.maxRetries;

var hyphenatedPackageName = config.packageJson.name
.replace('@google-cloud', 'gcloud-node') // For legacy purposes.
.replace('/', '-'); // For UA spec-compliance purposes.
this.userAgent = hyphenatedPackageName + '/' + config.packageJson.version;
this.userAgent = util.getUserAgentFromPackageJson(config.packageJson);

var apiVersion = config.apiVersion;
var service = this.service = config.service;
Expand Down
5 changes: 1 addition & 4 deletions packages/common/src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,7 @@ Service.prototype.request = function(reqOpts, callback) {
delete reqOpts.interceptors_;

reqOpts.headers = extend({}, reqOpts.headers, {
'User-Agent': [
this.packageJson.name.replace('@google-cloud', 'gcloud-node'),
this.packageJson.version
].join('/')
'User-Agent': util.getUserAgentFromPackageJson(this.packageJson)
});

return this.makeAuthenticatedRequest(reqOpts, callback);
Expand Down
16 changes: 16 additions & 0 deletions packages/common/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,3 +614,19 @@ function isCustomType(unknown, module) {
}

util.isCustomType = isCustomType;

/**
* Create a properly-formatted User-Agent string from a package.json file.
*
* @param {object} packageJson - A module's package.json file.
* @return {string} userAgent - The formatted User-Agent string.
*/
function getUserAgentFromPackageJson(packageJson) {
var hyphenatedPackageName = packageJson.name
.replace('@google-cloud', 'gcloud-node') // For legacy purposes.
.replace('/', '-'); // For UA spec-compliance purposes.

return hyphenatedPackageName + '/' + packageJson.version;
}

util.getUserAgentFromPackageJson = getUserAgentFromPackageJson;
17 changes: 15 additions & 2 deletions packages/common/test/grpc-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var sinon = require('sinon').sandbox.create();
var through = require('through2');

var util = require('../src/util.js');
var fakeUtil = extend({}, util);

function FakeService() {
this.calledWith_ = arguments;
Expand All @@ -43,6 +44,7 @@ function fakeRetryRequest() {
return (retryRequestOverride || retryRequest).apply(null, arguments);
}


var GrpcMetadataOverride;
var grpcLoadOverride;
var fakeGrpc = {
Expand Down Expand Up @@ -119,7 +121,8 @@ describe('GrpcService', function() {
'google-proto-files': fakeGoogleProtoFiles,
'retry-request': fakeRetryRequest,
grpc: fakeGrpc,
'./service.js': FakeService
'./service.js': FakeService,
'./util.js': fakeUtil
});
GrpcServiceCached = extend(true, {}, GrpcService);
});
Expand Down Expand Up @@ -288,7 +291,17 @@ describe('GrpcService', function() {
});

it('should set the correct user-agent', function() {
assert.strictEqual(grpcService.userAgent, 'gcloud-node-service/0.2.0');
var userAgent = 'user-agent/0.0.0';

var getUserAgentFn = fakeUtil.getUserAgentFromPackageJson;
fakeUtil.getUserAgentFromPackageJson = function(packageJson) {
fakeUtil.getUserAgentFromPackageJson = getUserAgentFn;
assert.strictEqual(packageJson, CONFIG.packageJson);
return userAgent;
};

var grpcService = new GrpcService(CONFIG, OPTIONS);
assert.strictEqual(grpcService.userAgent, userAgent);
});

it('should get the root directory for the proto files', function(done) {
Expand Down
14 changes: 10 additions & 4 deletions packages/common/test/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,17 @@ describe('Service', function() {
});

it('should add the User Agent', function(done) {
var userAgent = 'user-agent/0.0.0';

var getUserAgentFn = util.getUserAgentFromPackageJson;
util.getUserAgentFromPackageJson = function(packageJson) {
util.getUserAgentFromPackageJson = getUserAgentFn;
assert.strictEqual(packageJson, service.packageJson);
return userAgent;
};

service.makeAuthenticatedRequest = function(reqOpts) {
assert.strictEqual(reqOpts.headers['User-Agent'], [
CONFIG.packageJson.name.replace('@google-cloud', 'gcloud-node'),
CONFIG.packageJson.version
].join('/'));
assert.strictEqual(reqOpts.headers['User-Agent'], userAgent);
done();
};

Expand Down
11 changes: 11 additions & 0 deletions packages/common/test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1363,4 +1363,15 @@ describe('common/util', function() {
});
});
});

describe('getUserAgentFromPackageJson', function() {
it('should format a User Agent string from a package.json', function() {
var userAgent = util.getUserAgentFromPackageJson({
name: '@google-cloud/storage',
version: '0.1.0'
});

assert.strictEqual(userAgent, 'gcloud-node-storage/0.1.0');
});
});
});
5 changes: 1 addition & 4 deletions packages/translate/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,7 @@ Translate.prototype.request = function(reqOpts, callback) {
key: this.key
},
headers: {
'User-Agent': [
PKG.name.replace('@google-cloud', 'gcloud-node'),
PKG.version
].join('/')
'User-Agent': common.util.getUserAgentFromPackageJson(PKG)
}
});

Expand Down
13 changes: 10 additions & 3 deletions packages/translate/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ var extend = require('extend');
var proxyquire = require('proxyquire');
var util = require('@google-cloud/common').util;

var PKG = require('../package.json');

var makeRequestOverride;
var fakeUtil = extend({}, util, {
makeRequest: function() {
Expand Down Expand Up @@ -386,6 +384,15 @@ describe('Translate', function() {

describe('request', function() {
it('should make the correct request', function(done) {
var userAgent = 'user-agent/0.0.0';

var getUserAgentFn = fakeUtil.getUserAgentFromPackageJson;
fakeUtil.getUserAgentFromPackageJson = function(packageJson) {
fakeUtil.getUserAgentFromPackageJson = getUserAgentFn;
assert.deepEqual(packageJson, require('../package.json'));
return userAgent;
};

var reqOpts = {
uri: '/test',
a: 'b',
Expand All @@ -401,7 +408,7 @@ describe('Translate', function() {
key: translate.key
},
headers: {
'User-Agent': 'gcloud-node/translate/' + PKG.version
'User-Agent': userAgent
}
});
var BASE_URL = 'https://www.googleapis.com/language/translate/v2';
Expand Down

0 comments on commit ed14c4a

Please sign in to comment.