diff --git a/packages/pubsub/src/index.js b/packages/pubsub/src/index.js index a5b3d529ad5..4185bd757b9 100644 --- a/packages/pubsub/src/index.js +++ b/packages/pubsub/src/index.js @@ -61,7 +61,8 @@ var GAX_CONFIG = { * reliable, many-to-many, asynchronous messaging service from Cloud * Platform. * - * The `PUBSUB_EMULATOR_HOST` environment variable from the gcloud SDK is + * The `apiEndpoint` from options will set the host. If not set, the + * `PUBSUB_EMULATOR_HOST` environment variable from the gcloud SDK is * honored, otherwise the actual API endpoint will be used. * * @constructor @@ -78,7 +79,7 @@ function PubSub(options) { } this.defaultBaseUrl_ = 'pubsub.googleapis.com'; - this.determineBaseUrl_(); + this.determineBaseUrl_(options.apiEndpoint); var config = { baseUrl: this.baseUrl_, @@ -797,19 +798,20 @@ PubSub.prototype.request = function(protoOpts) { /** * Determine the appropriate endpoint to use for API requests, first trying the - * local Pub/Sub emulator environment variable (PUBSUB_EMULATOR_HOST), otherwise - * the default JSON API. + * local `apiEndpoint` parameter. If the `apiEndpoint` parameter is null we try + * Pub/Sub emulator environment variable (PUBSUB_EMULATOR_HOST), otherwise the + * default JSON API. * * @private */ -PubSub.prototype.determineBaseUrl_ = function() { +PubSub.prototype.determineBaseUrl_ = function(apiEndpoint) { var baseUrl = this.defaultBaseUrl_; var leadingProtocol = new RegExp('^https*://'); var trailingSlashes = new RegExp('/*$'); - if (process.env.PUBSUB_EMULATOR_HOST) { + if (apiEndpoint || process.env.PUBSUB_EMULATOR_HOST) { this.customEndpoint_ = true; - baseUrl = process.env.PUBSUB_EMULATOR_HOST; + baseUrl = apiEndpoint || process.env.PUBSUB_EMULATOR_HOST; } this.baseUrl_ = baseUrl diff --git a/packages/pubsub/test/index.js b/packages/pubsub/test/index.js index 96197677c03..13a6d9fa58e 100644 --- a/packages/pubsub/test/index.js +++ b/packages/pubsub/test/index.js @@ -1040,6 +1040,17 @@ describe('PubSub', function() { delete process.env.PUBSUB_EMULATOR_HOST; }); + it('should set base url to parameter sent', function() { + var defaultBaseUrl_ = 'defaulturl'; + var testingUrl = 'localhost:8085'; + + setHost(defaultBaseUrl_); + pubsub.defaultBaseUrl_ = defaultBaseUrl_; + + pubsub.determineBaseUrl_(testingUrl); + assert.strictEqual(pubsub.baseUrl_, testingUrl); + }); + it('should default to defaultBaseUrl_', function() { var defaultBaseUrl_ = 'defaulturl'; pubsub.defaultBaseUrl_ = defaultBaseUrl_;