Skip to content

Commit

Permalink
set user agent correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Sep 9, 2016
1 parent 2fbd8ff commit db0f6d3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 36 deletions.
13 changes: 7 additions & 6 deletions packages/common/src/grpc-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,6 @@ function GrpcService(config, options) {

this.grpcMetadata = new grpc.Metadata();

this.grpcMetadata.add('User-Agent', [
config.packageJson.name.replace('@google-cloud', 'gcloud-node'),
config.packageJson.version
].join('/'));

if (config.grpcMetadata) {
for (var prop in config.grpcMetadata) {
if (config.grpcMetadata.hasOwnProperty(prop)) {
Expand All @@ -176,6 +171,11 @@ 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;

var apiVersion = config.apiVersion;
var service = this.service = config.service;

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

this.activeServiceMap_.set(protoOpts.service, service);
Expand Down
46 changes: 16 additions & 30 deletions packages/common/test/grpc-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,20 +247,14 @@ describe('GrpcService', function() {
assert.strictEqual(calledWith[1], OPTIONS);
});

it('should set grpcMetadata with the correct User Agent', function() {
var userAgentAdded = false;

var fakeGrpcMetadata = {
add: function(prop, value) {
assert.strictEqual(prop, 'User-Agent');
assert.strictEqual(value, [
CONFIG.packageJson.name.replace('@google-cloud', 'gcloud-node'),
CONFIG.packageJson.version
].join('/'));
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');
});

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

GrpcMetadataOverride = function() {
return fakeGrpcMetadata;
Expand All @@ -271,22 +265,13 @@ describe('GrpcService', function() {

var grpcService = new GrpcService(config, OPTIONS);
assert.strictEqual(grpcService.grpcMetadata, fakeGrpcMetadata);
assert.strictEqual(userAgentAdded, true);
});

it('should create and localize grpcMetadata', function() {
var userAgentAdded = false;

var fakeGrpcMetadata = {
add: function(prop, value) {
if (prop === 'User-Agent') {
return; // Already tested.
}

assert.strictEqual(prop, Object.keys(CONFIG.grpcMetadata)[0]);
assert.strictEqual(value, CONFIG.grpcMetadata[prop]);

userAgentAdded = true;
}
};

Expand All @@ -296,13 +281,16 @@ describe('GrpcService', function() {

var grpcService = new GrpcService(CONFIG, OPTIONS);
assert.strictEqual(grpcService.grpcMetadata, fakeGrpcMetadata);
assert.strictEqual(userAgentAdded, true);
});

it('should localize maxRetries', function() {
assert.strictEqual(grpcService.maxRetries, OPTIONS.maxRetries);
});

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

it('should get the root directory for the proto files', function(done) {
googleProtoFilesOverride = function(path) {
assert.strictEqual(path, '..');
Expand All @@ -313,12 +301,6 @@ 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 @@ -1524,9 +1506,13 @@ describe('GrpcService', function() {

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

return fakeService;
}
}
Expand Down

0 comments on commit db0f6d3

Please sign in to comment.