Skip to content

Commit 001b8b1

Browse files
fix(adjust-agolgeocoder-response-parse): convert to json only if response typeof is string (#361)
1 parent c684196 commit 001b8b1

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

lib/geocoder/agolgeocoder.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ AGOLGeocoder.prototype._getToken = function(callback) {
8383
if (err) {
8484
return callback(err);
8585
} else {
86-
result = JSON.parse(result);
86+
if (typeof result === 'string') { result = JSON.parse(result); }
8787
var tokenExpiration = (new Date()).getTime() + result.expires_in;
8888
var token = result.access_token;
8989
_this._cachedToken.put(token,tokenExpiration,_this.cache);
@@ -119,7 +119,7 @@ AGOLGeocoder.prototype.geocode = function(value, callback) {
119119
};
120120

121121
_this.httpAdapter.get(_this._endpoint, params, function(err, result) {
122-
result = JSON.parse(result);
122+
if (typeof result === 'string') { result = JSON.parse(result); }
123123
if (err) {
124124
return callback(err);
125125
} else {
@@ -242,7 +242,7 @@ AGOLGeocoder.prototype.reverse = function(query, callback) {
242242
};
243243

244244
_this.httpAdapter.get(_this._reverseEndpoint, params, function(err, result) {
245-
result = JSON.parse(result);
245+
if (typeof result === 'string') { result = JSON.parse(result); }
246246
if (err) {
247247
return callback(err);
248248
} else {

test/geocoder/agolgeocoder.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,26 @@ describe('AGOLGeocoder', () => {
191191
});
192192
});
193193

194+
test('Should handle a not "OK" status status with object response', done => {
195+
var mock = sinon.mock(mockedRequestifyAdapter);
196+
197+
mock.expects('get').once().callsArgWith(2, false,
198+
{"error":{"code":498,"message":"Invalid Token","details":[]}}
199+
);
200+
var geocoder = new AGOLGeocoder(mockedRequestifyAdapter,mockedOptions);
201+
202+
//Force valid tokens (this was tested separately)
203+
geocoder._getToken = function(callback) {
204+
callback(false,"ABCD");
205+
};
206+
geocoder.geocode('380 New York St, Redlands, CA 92373', function(err, results) {
207+
//err.should.to.equal(false);
208+
err.should.to.deep.equal({"code":498,"message":"Invalid Token","details":[]});
209+
mock.verify();
210+
done();
211+
});
212+
});
213+
194214
describe('#reverse' , () => {
195215
test('Should call httpAdapter get method', () => {
196216

test/geocoder/locationiqgeocoder.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
'countryCode': 'US',
108108
'latitude': 40.7487727,
109109
'longitude': -73.9849336,
110+
'formattedAddress': 'Empire State Building, 362, 5th Avenue, Diamond District, Manhattan, New York County, NYC, New York, 10035, United States of America',
110111
'state': 'New York',
111112
'streetName': '5th Avenue',
112113
'streetNumber': '362',
@@ -172,6 +173,7 @@
172173
'countryCode': 'US',
173174
'latitude': 40.7487727,
174175
'longitude': -73.9849336,
176+
'formattedAddress': 'Empire State Building, 362, 5th Avenue, Diamond District, Manhattan, New York County, NYC, New York, 10035, United States of America',
175177
'state': 'New York',
176178
'streetName': '5th Avenue',
177179
'streetNumber': '362',

0 commit comments

Comments
 (0)