Skip to content

Commit

Permalink
Reintroduce x-goog-api-client for common-grpc (#2164)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmuk authored and stephenplusplus committed Mar 30, 2017
1 parent f7d4b05 commit 4c71ccd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
6 changes: 6 additions & 0 deletions packages/common-grpc/src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ function GrpcService(config, options) {

this.grpcMetadata = new grpc.Metadata();

this.grpcMetadata.add('x-goog-api-client', [
'gl-node/' + process.versions.node,
'gccl/' + config.packageJson.version,
'grpc/' + require('grpc/package.json').version
].join(' '));

if (config.grpcMetadata) {
for (var prop in config.grpcMetadata) {
if (config.grpcMetadata.hasOwnProperty(prop)) {
Expand Down
32 changes: 19 additions & 13 deletions packages/common-grpc/test/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ describe('GrpcService', function() {
};
var ROOT_DIR = '/root/dir';
var PROTO_FILE_PATH = 'filepath.proto';
var EXPECTED_API_CLIENT_HEADER = [
'gl-node/' + process.versions.node,
'gccl/' + CONFIG.packageJson.version,
'grpc/' + require('grpc/package.json').version
].join(' ');

var MOCK_GRPC_API = { google: {} };
MOCK_GRPC_API.google[CONFIG.service] = {};
Expand Down Expand Up @@ -273,33 +278,34 @@ describe('GrpcService', function() {
});

it('should default grpcMetadata to empty metadata', function() {
var fakeGrpcMetadata = {};
var fakeGrpcMetadata = {
'x-goog-api-client': EXPECTED_API_CLIENT_HEADER
};

GrpcMetadataOverride = function() {
return fakeGrpcMetadata;
GrpcMetadataOverride = function() {};
GrpcMetadataOverride.prototype.add = function(prop, val) {
this[prop] = val;
};

var config = extend({}, CONFIG);
delete config.grpcMetadata;

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

it('should create and localize grpcMetadata', function() {
var fakeGrpcMetadata = {
add: function(prop, value) {
assert.strictEqual(prop, Object.keys(CONFIG.grpcMetadata)[0]);
assert.strictEqual(value, CONFIG.grpcMetadata[prop]);
}
};
var fakeGrpcMetadata = extend({
'x-goog-api-client': EXPECTED_API_CLIENT_HEADER
}, CONFIG.grpcMetadata);

GrpcMetadataOverride = function() {
return fakeGrpcMetadata;
GrpcMetadataOverride = function() {};
GrpcMetadataOverride.prototype.add = function(prop, val) {
this[prop] = val;
};

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

it('should localize maxRetries', function() {
Expand Down

0 comments on commit 4c71ccd

Please sign in to comment.