Skip to content

Commit

Permalink
fix(geolocate): return error where there is no ip info
Browse files Browse the repository at this point in the history
  • Loading branch information
Jun711 committed Feb 25, 2018
1 parent ebf86dc commit 5f142df
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
18 changes: 11 additions & 7 deletions src/geolocate.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import noIpInfoErrorMsg from './utils/constants';

export default class GeolocateService {
static async geolocateFreeGeoIp() {
const response = await fetch('https://freegeoip.net/json/');
const ipInfo = await response.json();
static async geolocateFreeGeoIp(ip) {
const ipQuery = ip || '';
const response = await fetch(`https://freegeoip.net/json/${ipQuery}`);
const ipInfo = await response.json().catch(() => new Error(noIpInfoErrorMsg));
return ipInfo;
}

static async geolocateIPAPI() {
const response = await fetch('https://ipapi.co/country');
const countryCode = await response.text();
return countryCode;
static async geolocateIPAPI(ip) {
const ipQuery = ip ? `${ip}/` : '';
const response = await fetch(`https://ipapi.co/${ipQuery}country`);
const countryCode = await response.text().catch(() => new Error(noIpInfoErrorMsg));
return countryCode !== 'Undefined' ? countryCode : new Error(noIpInfoErrorMsg);
}
}
3 changes: 3 additions & 0 deletions src/utils/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const noIpInfoErrorMsg = 'no ip information';

export default noIpInfoErrorMsg;
35 changes: 18 additions & 17 deletions tests/amazonGeotarget.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import amazon from 'geo-amazon';
import fetchMock from 'fetch-mock';
import GeolocateService from '../src/geolocate';
import AmazonGeotargetService from '../src/amazonGeotarget';
import noIpInfoErrorMsg from '../src/utils/constants';

describe('fn whereabout', () => {
describe('mock IP services', () => {
Expand All @@ -31,6 +32,11 @@ describe('fn whereabout', () => {
expect(response).to.equal('US');
}

function errorHandler(err) {
expect(err).to.be.an.instanceof(Error);
expect(err.message).to.equal('Service is not available');
}

it('whereabout should call IPAPI when no provider specified', (done) => {
AmazonGeotargetService.whereabout()
.then((response) => {
Expand Down Expand Up @@ -72,24 +78,18 @@ describe('fn whereabout', () => {
});
});

it('whereabout should rejects as promised when provider 2 specified', (done) => {
it('whereabout should reject as promised when provider 2 specified', (done) => {
AmazonGeotargetService.whereabout({ provider: 2 })
.then(() => {
assert.fail(0, 1, 'Expected rejected promise');
done();
}, (err) => {
expect(err).to.be.an.instanceof(Error);
expect(err.message).to.equal('Service is not available')
errorHandler(err);
done();
});
});

it('await - whereabout should rejects as promised when provider 2 specified', async () => {
const errorHandler = function errorHandler(err) {
expect(err).to.be.an.instanceof(Error);
expect(err.message).to.equal('Service is not available');
};

it('await - whereabout should reject as promised when provider 2 specified', async () => {
const response = await AmazonGeotargetService.whereabout({ provider: 2 })
.catch(err => errorHandler(err));
expect(response).to.be.an('undefined');
Expand All @@ -100,6 +100,7 @@ describe('fn whereabout', () => {
const ipapiRes = 'US';
const ipapiUndefined = 'Undefined';
const freeGeoIpRes = { country_code: 'US' };
const freeGeoIpUndefined = '404 page not found';

it('whereabout should returns US using provider 0 and IP from US is specified', async () => {
fetchMock.get('https://ipapi.co/76.72.167.90/country', ipapiRes);
Expand All @@ -124,9 +125,9 @@ describe('fn whereabout', () => {
}).catch((err) => {
throw err;
});
assert.strictEqual(response, undefined);
assert.notExists(response); // null or undefined
assert.isUndefined(response);
expect(response).to.be.an('error');
expect(response.message).to.be.a('string');
expect(response.message).to.equal(noIpInfoErrorMsg);
fetchMock.restore();
});

Expand All @@ -146,16 +147,16 @@ describe('fn whereabout', () => {

it('whereabout should returns US using provider 1 and IP is non-existent', async () => {
// freegeoip response when IP is not found
fetchMock.get('https://freegeoip.net/json/1234567', ' 404 page not found');
fetchMock.get('https://freegeoip.net/json/1234567', freeGeoIpUndefined);
const response = await AmazonGeotargetService.whereabout({
provider: 1,
ip: '1234567',
}).catch((err) => {
throw err;
});
assert.strictEqual(response, undefined);
assert.notExists(response); // null or undefined
assert.isUndefined(response);
expect(response).to.be.an('error');
expect(response.message).to.be.a('string');
expect(response.message).to.equal(noIpInfoErrorMsg);
fetchMock.restore();
});
});
Expand Down Expand Up @@ -236,7 +237,7 @@ describe('fn amazonGeotarget', () => {
});
});

describe('stub amazonStore and geolocate ', () => {
describe('stub whereabout and geolocate ', () => {
let amazonStoreStub;
let whereaboutStub;

Expand Down

0 comments on commit 5f142df

Please sign in to comment.