Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions packages/common/src/grpc-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,18 @@ function GrpcService(config, options) {
protoServices[service] = googleProtoFiles[service][apiVersion];
}

for (var protoServiceName in protoServices) {
var protoService = this.loadProtoFile_(
protoServices[protoServiceName], config);
var self = this;

this.protos[protoServiceName] = protoService;
}
Object.keys(protoServices).forEach(function(name) {
var protoConfig = protoServices[name];
var service = self.loadProtoFile_(protoConfig, config);

self.protos[name] = service;

if (protoConfig.baseUrl) {
service.baseUrl = protoConfig.baseUrl;
}
});
}

nodeutil.inherits(GrpcService, Service);
Expand Down Expand Up @@ -677,7 +683,7 @@ GrpcService.prototype.loadProtoFile_ = function(protoConfig, config) {
var apiVersion = protoConfig.apiVersion || config.apiVersion;
var service = dotProp.get(services.google, serviceName);

return service[apiVersion];
return service[apiVersion] || service;
};

/**
Expand All @@ -701,7 +707,7 @@ GrpcService.prototype.getService_ = function(protoOpts) {

if (!service) {
service = new proto[protoOpts.service](
this.baseUrl,
proto.baseUrl || this.baseUrl,
this.grpcCredentials
);

Expand Down
62 changes: 62 additions & 0 deletions packages/common/test/grpc-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,30 @@ describe('GrpcService', function() {
);
});

it('should store the baseUrl properly', function() {
var fakeBaseUrl = 'a.googleapis.com';

grpcLoadOverride = function() {
return MOCK_GRPC_API;
};

var config = extend(true, {}, CONFIG, {
protoServices: {
CustomServiceName: {
path: '../file/path.proto',
baseUrl: fakeBaseUrl
}
}
});

var grpcService = new GrpcService(config, OPTIONS);

assert.strictEqual(
grpcService.protos.CustomServiceName.baseUrl,
fakeBaseUrl
);
});

it('should not run in the gcloud sandbox environment', function() {
global.GCLOUD_SANDBOX_ENV = {};
var grpcService = new GrpcService();
Expand Down Expand Up @@ -1361,6 +1385,26 @@ describe('GrpcService', function() {
var service = grpcService.loadProtoFile_(fakeProtoConfig, fakeMainConfig);
assert.strictEqual(service, fakeServices.google.FakeService.v1);
});

it('should return the services object if invalid version', function() {
var fakeProtoConfig = {
path: '/root/dir/path',
service: 'FakeService',
apiVersion: null
};

var fakeMainConfig = {
service: 'OtherFakeService',
apiVersion: 'v2'
};

grpcLoadOverride = function(pathOpts, type, grpOpts) {
return fakeServices;
};

var service = grpcService.loadProtoFile_(fakeProtoConfig, fakeMainConfig);
assert.strictEqual(service, fakeServices.google.FakeService);
});
});

describe('getService_', function() {
Expand Down Expand Up @@ -1423,5 +1467,23 @@ describe('GrpcService', function() {
var cachedService = grpcService.activeServiceMap_.get('Service');
assert.strictEqual(cachedService, fakeService);
});

it('should use the baseUrl override if applicable', function() {
var fakeBaseUrl = 'a.googleapis.com';
var fakeService = {};

grpcService.protos = {
Service: {
baseUrl: fakeBaseUrl,
Service: function(baseUrl, grpcCredentials) {
assert.strictEqual(baseUrl, fakeBaseUrl);
return fakeService;
}
}
};

var service = grpcService.getService_({ service: 'Service' });
assert.strictEqual(service, fakeService);
});
});
});