|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| -/*global describe, it, beforeEach */ |
18 |
| - |
19 | 17 | 'use strict';
|
20 | 18 |
|
21 | 19 | var assert = require('assert');
|
22 |
| -var Dataset = require('../../lib/datastore/dataset'); |
23 | 20 | var util = require('../../lib/common/util.js');
|
24 | 21 |
|
25 | 22 | 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; |
31 | 24 |
|
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 | + }); |
36 | 29 |
|
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'; |
41 | 34 |
|
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 | + }; |
46 | 39 |
|
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); |
50 | 42 | });
|
51 | 43 | });
|
52 | 44 |
|
@@ -175,4 +167,59 @@ describe('Dataset', function() {
|
175 | 167 | assert.strictEqual(query.namespace, null);
|
176 | 168 | });
|
177 | 169 | });
|
| 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 | + }); |
178 | 225 | });
|
0 commit comments