diff --git a/README.md b/README.md index 95a5590..e3399ff 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,34 @@ gmAPI.reverseGeocode(reverseGeocodeParams, function(err, result){ Check out the [unit tests](./tree/new-major-version/test/unit/) for more APIs examples. +### Mocking responses + +Sometimes it is desirable (such as in CI or locally) to mock out the google API endpoints so that you don't end up hammering Google's servers, or using up all your account quota. In this instance you can override the default API urls with your own mocked ones. + +```javascript +const config = { + google_api_url: 'http://localhost:3000/fixture', // optional, mock the google API endpoint for dev/test + google_secure_api_url: 'https://localhost:3443/fixture' // optional, mock the google API endpoint (HTTPS) +} + +var gmAPI = new GoogleMapsAPI(config); +``` + +If you use a proxy or some other way to grab a real response from google, and serve it from the URLS you specify above, you can create a deterministic map lookup for testing purposes (i.e. no matter what you enter, you will always get the same result). My mock server (using (hapijs)[http://hapijs.com/]) looks a bit like this: + +```javascript +server.route({ + path: '/fixture/maps/api/directions/json', + method: 'GET', + handler: function(request, reply) { + const route = require('./my-real-saved-gmaps-response.json'); + reply(route); + } +}); +``` + +- note the 'path' in the route config, this is what googlemaps will call when you make a directions request. + ### Static Maps ```javascript diff --git a/lib/config/constants.json b/lib/config/constants.json index 95b5cca..51a4e36 100644 --- a/lib/config/constants.json +++ b/lib/config/constants.json @@ -1,12 +1,14 @@ { "ACCEPTED_CONFIG_KEYS": { - "encode_polylines": "boolean", - "google_client_id": "string", - "key": "string", - "proxy": "string", - "secure": "boolean", - "stagger_time": "number" + "encode_polylines": "boolean", + "google_client_id": "string", + "google_api_url": "string", + "google_secure_api_url": "string", + "key": "string", + "proxy": "string", + "secure": "boolean", + "stagger_time": "number" }, "ACCEPTED_PARAMS": { diff --git a/lib/config/getDefault.js b/lib/config/getDefault.js index 4a12826..ddbed73 100644 --- a/lib/config/getDefault.js +++ b/lib/config/getDefault.js @@ -4,12 +4,14 @@ module.exports = function() { return { - encode_polylines: true, - google_client_id: null, - key: null, - proxy: null, - secure: false, - stagger_time: 200, + encode_polylines: true, + google_client_id: null, + google_api_url: 'http://maps.googleapis.com', + google_secure_api_url: 'https://maps.googleapis.com', + key: null, + proxy: null, + secure: false, + stagger_time: 200, set google_private_key(value) { if (typeof value !== 'undefined' && value !== null) { // Google private keys are URL friendly base64, needs to be replaced with base64 valid characters diff --git a/lib/utils/makeRequest.js b/lib/utils/makeRequest.js index c539ed2..51f2a88 100644 --- a/lib/utils/makeRequest.js +++ b/lib/utils/makeRequest.js @@ -68,7 +68,7 @@ module.exports = function(request, config, path, args, callback, encoding) { } var options = { - uri: (secure ? 'https' : 'http') + '://maps.googleapis.com' + path + uri: (secure ? config.google_secure_api_url : config.google_api_url) + path }; if (encoding) options.encoding = encoding; diff --git a/test/unit/constructorTest.js b/test/unit/constructorTest.js index 4a63166..666d44d 100644 --- a/test/unit/constructorTest.js +++ b/test/unit/constructorTest.js @@ -32,7 +32,7 @@ describe('constructor', function() { should.exist( gmAPI.request ); gmAPI.request.should.be.instanceof(Function); }); - } + } } ); @@ -47,13 +47,15 @@ describe('constructor', function() { it('should accept configurations', function() { var config = { - key: 'xxxxxxxxxxxxxxxx', - google_client_id: 'test-client-id', - stagger_time: 1000, - encode_polylines: false, - secure: true, - proxy: 'http://127.0.0.1:9999', - google_private_key: 'test-private-key' + key: 'xxxxxxxxxxxxxxxx', + google_client_id: 'test-client-id', + google_api_url: 'http://localhost:3000', + google_secure_api_url: 'https://localhost:3443', + stagger_time: 1000, + encode_polylines: false, + secure: true, + proxy: 'http://127.0.0.1:9999', + google_private_key: 'test-private-key' }; var gmAPI = new GoogleMapsAPI( config ); @@ -62,11 +64,13 @@ describe('constructor', function() { gmAPI.config.key.should.equal( config.key ); gmAPI.config.google_client_id.should.equal( config.google_client_id ); + gmAPI.config.google_api_url.should.equal( config.google_api_url ); + gmAPI.config.google_secure_api_url.should.equal( config.google_secure_api_url ); gmAPI.config.stagger_time.should.equal( config.stagger_time ); gmAPI.config.encode_polylines.should.equal( config.encode_polylines ); gmAPI.config.secure.should.equal( config.secure ); gmAPI.config.proxy.should.equal( config.proxy ); - + should.exist( gmAPI.config.google_private_key ); });