Skip to content

Commit

Permalink
fix(adjust-agolgeocoder-response-parse): convert to json only if resp…
Browse files Browse the repository at this point in the history
…onse typeof is string (#361)
  • Loading branch information
juliano-aiqfome authored Nov 15, 2024
1 parent c684196 commit 001b8b1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/geocoder/agolgeocoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions test/geocoder/agolgeocoder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {

Expand Down
2 changes: 2 additions & 0 deletions test/geocoder/locationiqgeocoder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 001b8b1

Please sign in to comment.