From f61587e586d269b272a43c6482c5a52d118e9e14 Mon Sep 17 00:00:00 2001 From: Antony Jones Date: Mon, 18 Apr 2016 22:22:06 +0100 Subject: [PATCH 1/5] Allow mocked google api for local development --- lib/config/constants.json | 1 + lib/config/getDefault.js | 1 + lib/utils/makeRequest.js | 2 +- test/unit/constructorTest.js | 6 ++++-- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/config/constants.json b/lib/config/constants.json index 95b5cca..233f477 100644 --- a/lib/config/constants.json +++ b/lib/config/constants.json @@ -3,6 +3,7 @@ "ACCEPTED_CONFIG_KEYS": { "encode_polylines": "boolean", "google_client_id": "string", + "google_api_url": "string", "key": "string", "proxy": "string", "secure": "boolean", diff --git a/lib/config/getDefault.js b/lib/config/getDefault.js index 4a12826..c50bbd4 100644 --- a/lib/config/getDefault.js +++ b/lib/config/getDefault.js @@ -6,6 +6,7 @@ return { encode_polylines: true, google_client_id: null, + google_api_url: 'maps.googleapis.com', key: null, proxy: null, secure: false, diff --git a/lib/utils/makeRequest.js b/lib/utils/makeRequest.js index c539ed2..ad0951e 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 ? 'https' : 'http') + '://' + config.google_api_url + path }; if (encoding) options.encoding = encoding; diff --git a/test/unit/constructorTest.js b/test/unit/constructorTest.js index 4a63166..49bcb0f 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); }); - } + } } ); @@ -49,6 +49,7 @@ describe('constructor', function() { var config = { key: 'xxxxxxxxxxxxxxxx', google_client_id: 'test-client-id', + google_api_url: 'localhost', stagger_time: 1000, encode_polylines: false, secure: true, @@ -62,11 +63,12 @@ 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.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 ); }); From ae665d2cdce52064001c6f2befe7850ccb6bb520 Mon Sep 17 00:00:00 2001 From: Antony Jones Date: Tue, 19 Apr 2016 21:10:59 +0100 Subject: [PATCH 2/5] Update to handle secure urls as well as non secure urls --- lib/config/getDefault.js | 15 ++++++++------- lib/utils/makeRequest.js | 2 +- test/unit/constructorTest.js | 17 +++++++++-------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/lib/config/getDefault.js b/lib/config/getDefault.js index c50bbd4..ddbed73 100644 --- a/lib/config/getDefault.js +++ b/lib/config/getDefault.js @@ -4,13 +4,14 @@ module.exports = function() { return { - encode_polylines: true, - google_client_id: null, - google_api_url: 'maps.googleapis.com', - 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 ad0951e..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') + '://' + config.google_api_url + 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 49bcb0f..3002300 100644 --- a/test/unit/constructorTest.js +++ b/test/unit/constructorTest.js @@ -47,14 +47,15 @@ describe('constructor', function() { it('should accept configurations', function() { var config = { - key: 'xxxxxxxxxxxxxxxx', - google_client_id: 'test-client-id', - google_api_url: 'localhost', - 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 ); From 87b054c6bac957ae9b1bc8ce73c9b447667f6ccd Mon Sep 17 00:00:00 2001 From: Antony Jones Date: Tue, 19 Apr 2016 21:21:10 +0100 Subject: [PATCH 3/5] Add extra unit test for secure url --- lib/config/constants.json | 15 ++++++++------- test/unit/constructorTest.js | 1 + 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/config/constants.json b/lib/config/constants.json index 233f477..51a4e36 100644 --- a/lib/config/constants.json +++ b/lib/config/constants.json @@ -1,13 +1,14 @@ { "ACCEPTED_CONFIG_KEYS": { - "encode_polylines": "boolean", - "google_client_id": "string", - "google_api_url": "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/test/unit/constructorTest.js b/test/unit/constructorTest.js index 3002300..666d44d 100644 --- a/test/unit/constructorTest.js +++ b/test/unit/constructorTest.js @@ -65,6 +65,7 @@ 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 ); From 14334a4766384a8b1ffdd4a61531e6f87efdb84f Mon Sep 17 00:00:00 2001 From: Antony Jones Date: Wed, 29 Jun 2016 11:31:10 +0100 Subject: [PATCH 4/5] Document new configuration and an example of how to use it. --- README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/README.md b/README.md index 95a5590..8e0e0c5 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. + +``` +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: + +``` +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 From 07b32dfc8c3e320da1953e564a6f6a4f3d35c99d Mon Sep 17 00:00:00 2001 From: Antony Jones Date: Wed, 29 Jun 2016 11:31:59 +0100 Subject: [PATCH 5/5] Mark code blocks as javascript --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8e0e0c5..e3399ff 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ Check out the [unit tests](./tree/new-major-version/test/unit/) for more APIs ex 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) @@ -104,7 +104,7 @@ 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',