Skip to content

Commit

Permalink
common: pull out common changes
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Sep 9, 2016
1 parent c2b3648 commit a3eccbd
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 100 deletions.
10 changes: 1 addition & 9 deletions packages/common/src/grpc-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ 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 @@ -176,7 +170,6 @@ function GrpcService(config, options) {
}

this.maxRetries = options.maxRetries;
this.userAgent = util.getUserAgentFromPackageJson(config.packageJson);

var apiVersion = config.apiVersion;
var service = this.service = config.service;
Expand Down Expand Up @@ -739,8 +732,7 @@ GrpcService.prototype.getService_ = function(protoOpts) {
if (!service) {
service = new proto[protoOpts.service](
proto.baseUrl || this.baseUrl,
this.grpcCredentials,
{ 'grpc.primary_user_agent': this.userAgent }
this.grpcCredentials
);

this.activeServiceMap_.set(protoOpts.service, service);
Expand Down
5 changes: 0 additions & 5 deletions packages/common/src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ function Service(config, options) {
this.getCredentials = this.makeAuthenticatedRequest.getCredentials;
this.globalInterceptors = arrify(options.interceptors_);
this.interceptors = [];
this.packageJson = config.packageJson;
this.projectId = options.projectId;
this.projectIdRequired = config.projectIdRequired !== false;
}
Expand Down Expand Up @@ -115,10 +114,6 @@ Service.prototype.request = function(reqOpts, callback) {

delete reqOpts.interceptors_;

reqOpts.headers = extend({}, reqOpts.headers, {
'User-Agent': util.getUserAgentFromPackageJson(this.packageJson)
});

return this.makeAuthenticatedRequest(reqOpts, callback);
};

Expand Down
27 changes: 11 additions & 16 deletions packages/common/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ var streamEvents = require('stream-events');
var through = require('through2');
var uniq = require('array-uniq');

/** @const {object} @google-cloud/common's package.json file. */
var PKG = require('../package.json');

/** @const {string} User agent. */
var USER_AGENT = PKG.name + '/' + PKG.version;

var util = module.exports;

var errorMessage = format([
Expand Down Expand Up @@ -460,6 +466,11 @@ util.makeRequest = makeRequest;
*/
function decorateRequest(reqOpts, config) {
config = config || {};
reqOpts.headers = reqOpts.headers || {};

var headers = reqOpts.headers;
var userAgent = headers['User-Agent'] || headers['user-agent'];
headers['User-Agent'] = userAgent || config.userAgent || USER_AGENT;

if (is.object(reqOpts.qs)) {
delete reqOpts.qs.autoPaginate;
Expand Down Expand Up @@ -614,19 +625,3 @@ 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;
40 changes: 8 additions & 32 deletions packages/common/test/grpc-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ 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 Down Expand Up @@ -93,10 +92,6 @@ describe('GrpcService', function() {
proto: {},
service: 'Service',
apiVersion: 'v1',
packageJson: {
name: '@google-cloud/service',
version: '0.2.0'
},
grpcMetadata: {
property: 'value'
}
Expand All @@ -120,8 +115,7 @@ describe('GrpcService', function() {
'google-proto-files': fakeGoogleProtoFiles,
'retry-request': fakeRetryRequest,
grpc: fakeGrpc,
'./service.js': FakeService,
'./util.js': fakeUtil
'./service.js': FakeService
});
GrpcServiceCached = extend(true, {}, GrpcService);
});
Expand Down Expand Up @@ -249,12 +243,6 @@ describe('GrpcService', function() {
assert.strictEqual(calledWith[1], OPTIONS);
});

it('should set insecure credentials if using customEndpoint', function() {
var config = extend({}, CONFIG, { customEndpoint: true });
var grpcService = new GrpcService(config, OPTIONS);
assert.strictEqual(grpcService.grpcCredentials.name, 'createInsecure');
});

it('should default grpcMetadata to empty metadata', function() {
var fakeGrpcMetadata = {};

Expand Down Expand Up @@ -289,20 +277,6 @@ describe('GrpcService', function() {
assert.strictEqual(grpcService.maxRetries, OPTIONS.maxRetries);
});

it('should set the correct user-agent', function() {
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) {
googleProtoFilesOverride = function(path) {
assert.strictEqual(path, '..');
Expand All @@ -313,6 +287,12 @@ describe('GrpcService', function() {
new GrpcService(CONFIG, OPTIONS);
});

it('should set insecure credentials if using customEndpoint', function() {
var config = extend({}, CONFIG, { customEndpoint: true });
var grpcService = new GrpcService(config, OPTIONS);
assert.strictEqual(grpcService.grpcCredentials.name, 'createInsecure');
});

it('should localize the service', function() {
assert.strictEqual(grpcService.service, CONFIG.service);
});
Expand Down Expand Up @@ -1518,13 +1498,9 @@ describe('GrpcService', function() {

grpcService.protos = {
Service: {
Service: function(baseUrl, grpcCredentials, userAgent) {
Service: function(baseUrl, grpcCredentials) {
assert.strictEqual(baseUrl, grpcService.baseUrl);
assert.strictEqual(grpcCredentials, grpcService.grpcCredentials);
assert.deepEqual(userAgent, {
'grpc.primary_user_agent': grpcService.userAgent
});

return fakeService;
}
}
Expand Down
28 changes: 1 addition & 27 deletions packages/common/test/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ describe('Service', function() {
var CONFIG = {
scopes: [],
baseUrl: 'base-url',
projectIdRequired: false,
packageJson: {
name: '@google-cloud/service',
version: '0.2.0'
}
projectIdRequired: false
};

var OPTIONS = {
Expand Down Expand Up @@ -133,10 +129,6 @@ describe('Service', function() {
assert.deepEqual(service.interceptors, []);
});

it('should localize package.json', function() {
assert.strictEqual(service.packageJson, CONFIG.packageJson);
});

it('should localize the projectId', function() {
assert.strictEqual(service.projectId, OPTIONS.projectId);
});
Expand Down Expand Up @@ -220,24 +212,6 @@ describe('Service', function() {
service.request(reqOpts, assert.ifError);
});

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'], userAgent);
done();
};

service.request(reqOpts, assert.ifError);
});

describe('projectIdRequired', function() {
describe('false', function() {
it('should include the projectId', function(done) {
Expand Down
65 changes: 54 additions & 11 deletions packages/common/test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,60 @@ describe('common/util', function() {
});

describe('decorateRequest', function() {
it('should keep user agent', function() {
var USER_AGENT = 'test/v1';

var reqOpts = {
a: 'b',
c: 'd',
headers: {
'User-Agent': USER_AGENT
}
};

var expectedReqOpts = extend({}, reqOpts, {
headers: {
'User-Agent': USER_AGENT
}
});

var decoratedReqOpts = util.decorateRequest(reqOpts);
assert.deepEqual(decoratedReqOpts, expectedReqOpts);
});

it('should add the user agent', function() {
var USER_AGENT = 'test/v2';

var reqOpts = { a: 'b', c: 'd' };

var expectedReqOpts = extend({}, reqOpts, {
headers: {
'User-Agent': USER_AGENT
}
});

var decoratedReqOpts = util.decorateRequest(reqOpts, {
userAgent: USER_AGENT
});
assert.deepEqual(decoratedReqOpts, expectedReqOpts);
});

it('should add the default user agent', function() {
var PKG = require('../package.json');
var USER_AGENT = PKG.name + '/' + PKG.version;

var reqOpts = { a: 'b', c: 'd' };

var expectedReqOpts = extend({}, reqOpts, {
headers: {
'User-Agent': USER_AGENT
}
});

var decoratedReqOpts = util.decorateRequest(reqOpts);
assert.deepEqual(decoratedReqOpts, expectedReqOpts);
});

it('should delete qs.autoPaginate', function() {
var decoratedReqOpts = util.decorateRequest({
qs: {
Expand Down Expand Up @@ -1363,15 +1417,4 @@ 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');
});
});
});

0 comments on commit a3eccbd

Please sign in to comment.