Skip to content

Commit 845416d

Browse files
committed
Merge pull request #497 from stephenplusplus/spp--datastore-local-server
datastore: support local devserver automatically - fixes #104
2 parents 64b6140 + c8a593e commit 845416d

File tree

2 files changed

+98
-31
lines changed

2 files changed

+98
-31
lines changed

lib/datastore/dataset.js

+28-8
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,40 @@ function Dataset(options) {
110110
email: options.email
111111
});
112112

113-
if (options.apiEndpoint && options.apiEndpoint.indexOf('http') !== 0) {
114-
options.apiEndpoint = 'http://' + options.apiEndpoint;
115-
}
116-
117-
var trailingSlashes = new RegExp('/*$');
118-
this.apiEndpoint = options.apiEndpoint || 'https://www.googleapis.com';
119-
this.apiEndpoint = this.apiEndpoint.replace(trailingSlashes, '');
120-
113+
this.apiEndpoint = Dataset.determineApiEndpoint_(options);
121114
this.namespace = options.namespace;
122115
this.projectId = options.projectId;
123116
}
124117

125118
nodeutil.inherits(Dataset, DatastoreRequest);
126119

120+
/**
121+
* Determine the appropriate endpoint to use for API requests. If not explicitly
122+
* defined, check for the "DATASTORE_HOST" environment variable, used to connect
123+
* to a local Datastore server.
124+
*
125+
* @private
126+
*
127+
* @param {object} options - Configuration object.
128+
* @param {string=} options.apiEndpoint - Custom API endpoint.
129+
*/
130+
Dataset.determineApiEndpoint_ = function(options) {
131+
var apiEndpoint = 'https://www.googleapis.com';
132+
var trailingSlashes = new RegExp('/*$');
133+
134+
if (options.apiEndpoint) {
135+
apiEndpoint = options.apiEndpoint;
136+
} else if (process.env.DATASTORE_HOST) {
137+
apiEndpoint = process.env.DATASTORE_HOST;
138+
}
139+
140+
if (apiEndpoint.indexOf('http') !== 0) {
141+
apiEndpoint = 'http://' + apiEndpoint;
142+
}
143+
144+
return apiEndpoint.replace(trailingSlashes, '');
145+
};
146+
127147
/**
128148
* Helper to create a Key object, scoped to the dataset's namespace by default.
129149
*

test/datastore/dataset.js

+70-23
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,31 @@
1414
* limitations under the License.
1515
*/
1616

17-
/*global describe, it, beforeEach */
18-
1917
'use strict';
2018

2119
var assert = require('assert');
22-
var Dataset = require('../../lib/datastore/dataset');
2320
var util = require('../../lib/common/util.js');
2421

2522
describe('Dataset', function() {
26-
describe('instantiation', function() {
27-
it('should set default API connection details', function() {
28-
var ds = new Dataset();
29-
assert.equal(ds.apiEndpoint, 'https://www.googleapis.com');
30-
});
23+
var Dataset;
3124

32-
it('should set API connection details', function() {
33-
var ds = new Dataset({ apiEndpoint: 'http://localhost:8080' });
34-
assert.equal(ds.apiEndpoint, 'http://localhost:8080');
35-
});
25+
beforeEach(function() {
26+
delete require.cache[require.resolve('../../lib/datastore/dataset')];
27+
Dataset = require('../../lib/datastore/dataset');
28+
});
3629

37-
it('should remove slashes from the apiEndpoint', function() {
38-
var ds1 = new Dataset({ apiEndpoint: 'http://localhost:8080' });
39-
var ds2 = new Dataset({ apiEndpoint: 'http://localhost:8080/' });
40-
var ds3 = new Dataset({ apiEndpoint: 'http://localhost:8080//' });
30+
describe('instantiation', function() {
31+
it('should set default API connection details', function() {
32+
var options = { a: 'b', c: 'd' };
33+
var mockApiEndpoint = 'http://localhost:8080';
4134

42-
assert.equal(ds1.apiEndpoint, 'http://localhost:8080');
43-
assert.equal(ds2.apiEndpoint, 'http://localhost:8080');
44-
assert.equal(ds3.apiEndpoint, 'http://localhost:8080');
45-
});
35+
Dataset.determineApiEndpoint_ = function (opts) {
36+
assert.deepEqual(opts, options);
37+
return mockApiEndpoint;
38+
};
4639

47-
it('should default to http if protocol is unspecified', function() {
48-
var ds = new Dataset({ apiEndpoint: 'localhost:8080' });
49-
assert.equal(ds.apiEndpoint, 'http://localhost:8080');
40+
var ds = new Dataset(options);
41+
assert.equal(ds.apiEndpoint, mockApiEndpoint);
5042
});
5143
});
5244

@@ -175,4 +167,59 @@ describe('Dataset', function() {
175167
assert.strictEqual(query.namespace, null);
176168
});
177169
});
170+
171+
describe('determineApiEndpoint_', function() {
172+
it('should default to googleapis.com', function() {
173+
delete process.env.DATASTORE_HOST;
174+
var expectedApiEndpoint = 'https://www.googleapis.com';
175+
assert.equal(Dataset.determineApiEndpoint_({}), expectedApiEndpoint);
176+
});
177+
178+
it('should remove slashes from the apiEndpoint', function() {
179+
var expectedApiEndpoint = 'http://localhost:8080';
180+
181+
assert.equal(Dataset.determineApiEndpoint_({
182+
apiEndpoint: expectedApiEndpoint
183+
}), expectedApiEndpoint);
184+
185+
assert.equal(Dataset.determineApiEndpoint_({
186+
apiEndpoint: 'http://localhost:8080/'
187+
}), expectedApiEndpoint);
188+
189+
assert.equal(Dataset.determineApiEndpoint_({
190+
apiEndpoint: 'http://localhost:8080//'
191+
}), expectedApiEndpoint);
192+
});
193+
194+
it('should default to http if protocol is unspecified', function() {
195+
var apiEndpoint = Dataset.determineApiEndpoint_({
196+
apiEndpoint: 'localhost:8080'
197+
});
198+
assert.equal(apiEndpoint, 'http://localhost:8080');
199+
});
200+
201+
describe('with DATASTORE_HOST environment variable', function() {
202+
var DATASTORE_HOST = 'http://localhost:8080';
203+
204+
before(function() {
205+
process.env.DATASTORE_HOST = DATASTORE_HOST;
206+
});
207+
208+
after(function() {
209+
delete process.env.DATASTORE_HOST;
210+
});
211+
212+
it('should use the DATASTORE_HOST env var', function() {
213+
assert.equal(Dataset.determineApiEndpoint_({}), DATASTORE_HOST);
214+
});
215+
216+
it('should favor an explicit apiEndpoint option', function() {
217+
var expectedApiEndpoint = 'http://apiendpointoverride';
218+
219+
assert.equal(Dataset.determineApiEndpoint_({
220+
apiEndpoint: expectedApiEndpoint
221+
}), expectedApiEndpoint);
222+
});
223+
});
224+
});
178225
});

0 commit comments

Comments
 (0)