Skip to content
Open
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
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions lib/config/constants.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
14 changes: 8 additions & 6 deletions lib/config/getDefault.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/makeRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 13 additions & 9 deletions test/unit/constructorTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('constructor', function() {
should.exist( gmAPI.request );
gmAPI.request.should.be.instanceof(Function);
});
}
}
}
);

Expand All @@ -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 );
Expand All @@ -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 );

});
Expand Down