Skip to content

Commit ab4261a

Browse files
support PUBSUB_EMULATOR_HOST
1 parent 5362dbb commit ab4261a

File tree

4 files changed

+38
-19
lines changed

4 files changed

+38
-19
lines changed

lib/common/grpc-service.js

+4
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ function GrpcService(config, options) {
143143
var apiVersion = config.apiVersion;
144144
var rootDir = googleProtoFiles('..');
145145

146+
if (config.customEndpoint) {
147+
this.grpcCredentials = grpc.credentials.createInsecure();
148+
}
149+
146150
this.protoOpts = config.proto;
147151
this.proto = grpc.load({
148152
root: rootDir,

lib/pubsub/index.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -585,20 +585,19 @@ PubSub.prototype.topic = function(name) {
585585
*/
586586
PubSub.prototype.determineBaseUrl_ = function() {
587587
var baseUrl;
588+
var leadingProtocol = new RegExp('^https*://');
588589
var trailingSlashes = new RegExp('/*$');
589590

590591
if (process.env.PUBSUB_EMULATOR_HOST) {
591-
baseUrl = process.env.PUBSUB_EMULATOR_HOST;
592592
this.customEndpoint = true;
593+
baseUrl = process.env.PUBSUB_EMULATOR_HOST;
593594
} else {
594-
baseUrl = 'https://pubsub.googleapis.com';
595-
}
596-
597-
if (baseUrl.indexOf('http') !== 0) {
598-
baseUrl = 'http://' + baseUrl;
595+
baseUrl = 'pubsub.googleapis.com';
599596
}
600597

601-
this.baseUrl = baseUrl.replace(trailingSlashes, '');
598+
this.baseUrl = baseUrl
599+
.replace(leadingProtocol, '')
600+
.replace(trailingSlashes, '');
602601
};
603602

604603
/*! Developer Documentation

test/common/grpc-service.js

+12
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ var fakeGrpc = {
5656
name: 'createFromGoogleCredential',
5757
args: arguments
5858
};
59+
},
60+
createInsecure: function() {
61+
return {
62+
name: 'createInsecure',
63+
args: arguments
64+
};
5965
}
6066
}
6167
};
@@ -139,6 +145,12 @@ describe('GrpcService', function() {
139145
new GrpcService(CONFIG, OPTIONS);
140146
});
141147

148+
it('should set insecure credentials if using customEndpoint', function() {
149+
var config = extend({}, CONFIG, { customEndpoint: true });
150+
var grpcService = new GrpcService(config, OPTIONS);
151+
assert.strictEqual(grpcService.grpcCredentials.name, 'createInsecure');
152+
});
153+
142154
it('should call grpc.load correctly', function(done) {
143155
grpcLoadOverride = function(opts) {
144156
assert.strictEqual(opts.root, ROOT_DIR);

test/pubsub/index.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ describe('PubSub', function() {
127127
]);
128128
});
129129

130-
it('should use the PUBSUB_HOST env var', function() {
130+
it('should use the PUBSUB_EMULATOR_HOST env var', function() {
131131
var pubSubHost = 'pubsub-host';
132-
process.env.PUBSUB_HOST = pubSubHost;
132+
process.env.PUBSUB_EMULATOR_HOST = pubSubHost;
133133

134134
var pubsub = new PubSub({ projectId: 'project-id' });
135-
delete process.env.PUBSUB_HOST;
135+
delete process.env.PUBSUB_EMULATOR_HOST;
136136

137137
var calledWith = pubsub.calledWith_[0];
138138
assert.strictEqual(calledWith.baseUrl, pubSubHost);
@@ -719,29 +719,33 @@ describe('PubSub', function() {
719719
delete process.env.PUBSUB_EMULATOR_HOST;
720720
});
721721

722-
it('should default to pubsub.googleapis.com/v1', function() {
722+
it('should default to pubsub.googleapis.com', function() {
723723
pubsub.determineBaseUrl_();
724724

725-
var expectedBaseUrl = 'https://pubsub.googleapis.com/v1';
725+
var expectedBaseUrl = 'pubsub.googleapis.com';
726726
assert.strictEqual(pubsub.baseUrl, expectedBaseUrl);
727727
});
728728

729729
it('should remove slashes from the baseUrl', function() {
730-
var expectedBaseUrl = 'http://localhost:8080';
730+
var expectedBaseUrl = 'localhost:8080';
731731

732-
setHost('http://localhost:8080/');
732+
setHost('localhost:8080/');
733733
pubsub.determineBaseUrl_();
734734
assert.strictEqual(pubsub.baseUrl, expectedBaseUrl);
735735

736-
setHost('http://localhost:8080//');
736+
setHost('localhost:8080//');
737737
pubsub.determineBaseUrl_();
738738
assert.strictEqual(pubsub.baseUrl, expectedBaseUrl);
739739
});
740740

741-
it('should default to http if protocol is unspecified', function() {
742-
setHost('localhost:8080');
741+
it('should remove the protocol if specified', function() {
742+
setHost('http://localhost:8080');
743+
pubsub.determineBaseUrl_();
744+
assert.strictEqual(pubsub.baseUrl, 'localhost:8080');
745+
746+
setHost('https://localhost:8080');
743747
pubsub.determineBaseUrl_();
744-
assert.strictEqual(pubsub.baseUrl, 'http://localhost:8080');
748+
assert.strictEqual(pubsub.baseUrl, 'localhost:8080');
745749
});
746750

747751
it('should not set customEndpoint when using default endpoint', function() {
@@ -751,7 +755,7 @@ describe('PubSub', function() {
751755
});
752756

753757
describe('with PUBSUB_EMULATOR_HOST environment variable', function() {
754-
var PUBSUB_EMULATOR_HOST = 'http://localhost:9090';
758+
var PUBSUB_EMULATOR_HOST = 'localhost:9090';
755759

756760
beforeEach(function() {
757761
setHost(PUBSUB_EMULATOR_HOST);

0 commit comments

Comments
 (0)