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
16 changes: 9 additions & 7 deletions packages/pubsub/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -78,7 +79,7 @@ function PubSub(options) {
}

this.defaultBaseUrl_ = 'pubsub.googleapis.com';
this.determineBaseUrl_();
this.determineBaseUrl_(options.apiEndpoint);

var config = {
baseUrl: this.baseUrl_,
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions packages/pubsub/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down