Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(adjust-agolgeocoder-response-parse): convert to json only if response typeof is string #361

Merged
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
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
Loading