diff --git a/lib/geocoder/agolgeocoder.js b/lib/geocoder/agolgeocoder.js index b66fac0..b67e6ba 100644 --- a/lib/geocoder/agolgeocoder.js +++ b/lib/geocoder/agolgeocoder.js @@ -83,7 +83,7 @@ AGOLGeocoder.prototype._getToken = function(callback) { if (err) { return callback(err); } else { - result = JSON.parse(result); + if (typeof result === 'string') { result = JSON.parse(result); } var tokenExpiration = (new Date()).getTime() + result.expires_in; var token = result.access_token; _this._cachedToken.put(token,tokenExpiration,_this.cache); @@ -119,7 +119,7 @@ AGOLGeocoder.prototype.geocode = function(value, callback) { }; _this.httpAdapter.get(_this._endpoint, params, function(err, result) { - result = JSON.parse(result); + if (typeof result === 'string') { result = JSON.parse(result); } if (err) { return callback(err); } else { @@ -242,7 +242,7 @@ AGOLGeocoder.prototype.reverse = function(query, callback) { }; _this.httpAdapter.get(_this._reverseEndpoint, params, function(err, result) { - result = JSON.parse(result); + if (typeof result === 'string') { result = JSON.parse(result); } if (err) { return callback(err); } else { diff --git a/test/geocoder/agolgeocoder.test.js b/test/geocoder/agolgeocoder.test.js index 93dccd5..f96f78a 100644 --- a/test/geocoder/agolgeocoder.test.js +++ b/test/geocoder/agolgeocoder.test.js @@ -191,6 +191,26 @@ describe('AGOLGeocoder', () => { }); }); + test('Should handle a not "OK" status status with object response', done => { + var mock = sinon.mock(mockedRequestifyAdapter); + + mock.expects('get').once().callsArgWith(2, false, + {"error":{"code":498,"message":"Invalid Token","details":[]}} + ); + var geocoder = new AGOLGeocoder(mockedRequestifyAdapter,mockedOptions); + + //Force valid tokens (this was tested separately) + geocoder._getToken = function(callback) { + callback(false,"ABCD"); + }; + geocoder.geocode('380 New York St, Redlands, CA 92373', function(err, results) { + //err.should.to.equal(false); + err.should.to.deep.equal({"code":498,"message":"Invalid Token","details":[]}); + mock.verify(); + done(); + }); + }); + describe('#reverse' , () => { test('Should call httpAdapter get method', () => { diff --git a/test/geocoder/locationiqgeocoder.test.js b/test/geocoder/locationiqgeocoder.test.js index 79a6353..0c29469 100644 --- a/test/geocoder/locationiqgeocoder.test.js +++ b/test/geocoder/locationiqgeocoder.test.js @@ -107,6 +107,7 @@ 'countryCode': 'US', 'latitude': 40.7487727, 'longitude': -73.9849336, + 'formattedAddress': 'Empire State Building, 362, 5th Avenue, Diamond District, Manhattan, New York County, NYC, New York, 10035, United States of America', 'state': 'New York', 'streetName': '5th Avenue', 'streetNumber': '362', @@ -172,6 +173,7 @@ 'countryCode': 'US', 'latitude': 40.7487727, 'longitude': -73.9849336, + 'formattedAddress': 'Empire State Building, 362, 5th Avenue, Diamond District, Manhattan, New York County, NYC, New York, 10035, United States of America', 'state': 'New York', 'streetName': '5th Avenue', 'streetNumber': '362',