Skip to content

Commit 62dd3ee

Browse files
Add new parse method in IpregistryClient
1 parent 3e0e51e commit 62dd3ee

5 files changed

Lines changed: 218 additions & 145 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [4.3.0] - 2021-12-14
11+
### Added
12+
- New `parse` method in IpregistryClient for parsing user-agent header values.
13+
1014
## [4.2.0] - 2021-10-26
1115
### Added
1216
- _IpInfo_ responses have a new `company` field.
@@ -106,7 +110,8 @@ https://github.com/ipregistry/ipregistry-javascript#configuring-cache-max-age
106110
## [0.9.1] - 2019-07-23
107111
- First public release.
108112

109-
[Unreleased]: https://github.com/ipregistry/ipregistry-javascript/compare/4.2.0...HEAD
113+
[Unreleased]: https://github.com/ipregistry/ipregistry-javascript/compare/4.3.0...HEAD
114+
[4.3.0]: https://github.com/ipregistry/ipregistry-javascript/compare/4.2.0...4.3.0
110115
[4.2.0]: https://github.com/ipregistry/ipregistry-javascript/compare/4.1.0...4.2.0
111116
[4.1.0]: https://github.com/ipregistry/ipregistry-javascript/compare/4.0.0...4.1.0
112117
[4.0.0]: https://github.com/ipregistry/ipregistry-javascript/compare/3.1.0...4.0.0

integration_test/ipregistry.ts

Lines changed: 137 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
import {
1818
ApiError,
19-
ClientError, InMemoryCache,
19+
ClientError,
20+
InMemoryCache,
2021
IpInfo,
2122
IpregistryClient,
2223
IpregistryConfigBuilder,
@@ -30,6 +31,107 @@ import {expect} from 'chai';
3031
const API_KEY = process.env.IPREGISTRY_API_KEY || 'tryou';
3132
const API_KEY_THROTTLED = process.env.IPREGISTRY_API_KEY_THROTTLED || 'tryout';
3233

34+
describe('batchLookup', () => {
35+
it('should return fresh info with no cache', async () => {
36+
const client = new IpregistryClient(API_KEY, new NoCache());
37+
38+
expect(client.getCache()).instanceOf(NoCache);
39+
40+
const ips = ['66.165.2.7', '2001:4860:4860::8844', '8.8.4.4'];
41+
const response = await client.batchLookup(ips);
42+
const ipInfoList = response.data;
43+
for (let i = 0; i < ipInfoList.length; i++) {
44+
const ipInfo = ipInfoList[i] as IpInfo;
45+
expect(ipInfo.ip).equal(ips[i]);
46+
}
47+
48+
expect(ipInfoList[0]['type']).equal('IPv4');
49+
expect(ipInfoList[1]['type']).equal('IPv6');
50+
expect(ipInfoList[2]['type']).equal('IPv4');
51+
});
52+
53+
it('should return cached value if available', async () => {
54+
const client = new IpregistryClient(API_KEY, new InMemoryCache());
55+
56+
const response = await client.lookup('66.165.2.7');
57+
const ipInfo = response.data;
58+
ipInfo.time_zone.current_time = 'cachedTime';
59+
60+
const ips = ['66.165.2.7', '1.1.1.1', '8.8.4.4'];
61+
const response2 = await client.batchLookup(ips);
62+
const ipInfoList = response2.data;
63+
64+
for (let i = 0; i < ipInfoList.length; i++) {
65+
const ipData = ipInfoList[i] as IpInfo;
66+
expect(ipData.ip).equal(ips[i]);
67+
}
68+
69+
expect((ipInfoList[0] as IpInfo).time_zone.current_time).equal('cachedTime');
70+
});
71+
72+
it('should handle invalid input with no error', async () => {
73+
const client = new IpregistryClient(API_KEY);
74+
const ips = ['66.165.2.7a', '1.1.1.1', '8.8.4.4c'];
75+
const response = await client.batchLookup(ips);
76+
const ipInfoList = response.data;
77+
78+
expect(ipInfoList[0]).to.be.instanceOf(LookupError);
79+
expect(ipInfoList[1]).not.to.be.instanceOf(LookupError);
80+
expect(ipInfoList[2]).to.be.instanceOf(LookupError);
81+
});
82+
83+
it('should consume 1 credit if batch lookup contains only 1 entry', async () => {
84+
const client = new IpregistryClient(API_KEY, new NoCache());
85+
const response = await client.batchLookup(['8.8.4.4']);
86+
87+
expect(response.credits.consumed).equal(1);
88+
expect(response.credits.remaining).greaterThan(0);
89+
expect(response.throttling).null;
90+
});
91+
92+
it('should consume credits for a batch lookup with no cache', async () => {
93+
const client = new IpregistryClient(API_KEY, new NoCache());
94+
const response = await client.batchLookup(['8.8.4.4', '1.2.3.4', '1.2.3.2']);
95+
96+
expect(response.credits.consumed).equal(4);
97+
expect(response.credits.remaining).greaterThan(0);
98+
expect(response.throttling).null;
99+
});
100+
101+
it('should consume some credits for a batch lookup with partial results from cache', async () => {
102+
const client = new IpregistryClient(API_KEY, new InMemoryCache());
103+
await client.lookup('8.8.4.4');
104+
await client.lookup('1.2.3.2');
105+
const response = await client.batchLookup(['8.8.4.4', '1.2.3.4', '1.2.3.2']);
106+
107+
expect(response.credits.consumed).equal(1);
108+
expect(response.credits.remaining).greaterThan(0);
109+
expect(response.throttling).null;
110+
});
111+
112+
it('should consume no credit for a batch lookup with all data returned from cache', async () => {
113+
const client = new IpregistryClient(API_KEY, new InMemoryCache());
114+
await client.lookup('8.8.4.4');
115+
await client.lookup('1.2.3.4');
116+
await client.lookup('1.2.3.2');
117+
const response = await client.batchLookup(['8.8.4.4', '1.2.3.4', '1.2.3.2']);
118+
119+
expect(response.credits.consumed).equal(0);
120+
expect(response.credits.remaining).null;
121+
expect(response.throttling).null;
122+
});
123+
124+
it('should return throttling data if API key is rate limited', async () => {
125+
const client = new IpregistryClient(API_KEY_THROTTLED);
126+
const response = await client.batchLookup(['8.8.4.4', '9.9.9.9']);
127+
128+
expect(response.throttling).not.null;
129+
expect(response?.throttling?.limit).greaterThan(-1);
130+
expect(response?.throttling?.remaining).greaterThan(-1);
131+
expect(response?.throttling?.reset).greaterThan(-1);
132+
});
133+
});
134+
33135
describe('lookup', () => {
34136
it('should throw ApiError when input IP is reserved', async () => {
35137
try {
@@ -149,107 +251,6 @@ describe('lookup', () => {
149251
});
150252
});
151253

152-
describe('batchLookup', () => {
153-
it('should return fresh info with no cache', async () => {
154-
const client = new IpregistryClient(API_KEY, new NoCache());
155-
156-
expect(client.getCache()).instanceOf(NoCache);
157-
158-
const ips = ['66.165.2.7', '2001:4860:4860::8844', '8.8.4.4'];
159-
const response = await client.batchLookup(ips);
160-
const ipInfoList = response.data;
161-
for (let i = 0; i < ipInfoList.length; i++) {
162-
const ipInfo = ipInfoList[i] as IpInfo;
163-
expect(ipInfo.ip).equal(ips[i]);
164-
}
165-
166-
expect(ipInfoList[0]['type']).equal('IPv4');
167-
expect(ipInfoList[1]['type']).equal('IPv6');
168-
expect(ipInfoList[2]['type']).equal('IPv4');
169-
});
170-
171-
it('should return cached value if available', async () => {
172-
const client = new IpregistryClient(API_KEY, new InMemoryCache());
173-
174-
const response = await client.lookup('66.165.2.7');
175-
const ipInfo = response.data;
176-
ipInfo.time_zone.current_time = 'cachedTime';
177-
178-
const ips = ['66.165.2.7', '1.1.1.1', '8.8.4.4'];
179-
const response2 = await client.batchLookup(ips);
180-
const ipInfoList = response2.data;
181-
182-
for (let i = 0; i < ipInfoList.length; i++) {
183-
const ipData = ipInfoList[i] as IpInfo;
184-
expect(ipData.ip).equal(ips[i]);
185-
}
186-
187-
expect((ipInfoList[0] as IpInfo).time_zone.current_time).equal('cachedTime');
188-
});
189-
190-
it('should handle invalid input with no error', async () => {
191-
const client = new IpregistryClient(API_KEY);
192-
const ips = ['66.165.2.7a', '1.1.1.1', '8.8.4.4c'];
193-
const response = await client.batchLookup(ips);
194-
const ipInfoList = response.data;
195-
196-
expect(ipInfoList[0]).to.be.instanceOf(LookupError);
197-
expect(ipInfoList[1]).not.to.be.instanceOf(LookupError);
198-
expect(ipInfoList[2]).to.be.instanceOf(LookupError);
199-
});
200-
201-
it('should consume 1 credit if batch lookup contains only 1 entry', async () => {
202-
const client = new IpregistryClient(API_KEY, new NoCache());
203-
const response = await client.batchLookup(['8.8.4.4']);
204-
205-
expect(response.credits.consumed).equal(1);
206-
expect(response.credits.remaining).greaterThan(0);
207-
expect(response.throttling).null;
208-
});
209-
210-
it('should consume credits for a batch lookup with no cache', async () => {
211-
const client = new IpregistryClient(API_KEY, new NoCache());
212-
const response = await client.batchLookup(['8.8.4.4', '1.2.3.4', '1.2.3.2']);
213-
214-
expect(response.credits.consumed).equal(4);
215-
expect(response.credits.remaining).greaterThan(0);
216-
expect(response.throttling).null;
217-
});
218-
219-
it('should consume some credits for a batch lookup with partial results from cache', async () => {
220-
const client = new IpregistryClient(API_KEY, new InMemoryCache());
221-
await client.lookup('8.8.4.4');
222-
await client.lookup('1.2.3.2');
223-
const response = await client.batchLookup(['8.8.4.4', '1.2.3.4', '1.2.3.2']);
224-
225-
expect(response.credits.consumed).equal(1);
226-
expect(response.credits.remaining).greaterThan(0);
227-
expect(response.throttling).null;
228-
});
229-
230-
it('should consume no credit for a batch lookup with all data returned from cache', async () => {
231-
const client = new IpregistryClient(API_KEY, new InMemoryCache());
232-
await client.lookup('8.8.4.4');
233-
await client.lookup('1.2.3.4');
234-
await client.lookup('1.2.3.2');
235-
const response = await client.batchLookup(['8.8.4.4', '1.2.3.4', '1.2.3.2']);
236-
237-
expect(response.credits.consumed).equal(0);
238-
expect(response.credits.remaining).null;
239-
expect(response.throttling).null;
240-
});
241-
242-
it('should return throttling data if API key is rate limited', async () => {
243-
const client = new IpregistryClient(API_KEY_THROTTLED);
244-
const response = await client.batchLookup(['8.8.4.4', '9.9.9.9']);
245-
246-
expect(response.throttling).not.null;
247-
expect(response?.throttling?.limit).greaterThan(-1);
248-
expect(response?.throttling?.remaining).greaterThan(-1);
249-
expect(response?.throttling?.reset).greaterThan(-1);
250-
});
251-
});
252-
253254
describe('originLookup', () => {
254255
it('should return fresh info with no cache', async () => {
255256
const client = new IpregistryClient(API_KEY, new NoCache());
@@ -321,3 +322,37 @@ describe('originLookup', () => {
321322
expect(response?.throttling?.reset).greaterThan(-1);
322323
});
323324
});
325+
326+
describe('parse', () => {
327+
it('should throw an error when no user-agent value is inputted', async () => {
328+
try {
329+
const client = new IpregistryClient(API_KEY_THROTTLED);
330+
await client.parse();
331+
expect.fail();
332+
} catch (error) {
333+
expect(error).to.be.instanceOf(ApiError);
334+
}
335+
});
336+
337+
it('should return 1 parsed user-agent result when 1 valid user-agent value is inputted', async () => {
338+
const client = new IpregistryClient(API_KEY_THROTTLED);
339+
const response =
340+
await client.parse(
341+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
342+
);
343+
expect(response.data.length).eq(1);
344+
expect(response.data[0].name).not.null;
345+
});
346+
347+
it('should return 2 parsed user-agent result when 2 valid user-agent value is inputted', async () => {
348+
const client = new IpregistryClient(API_KEY_THROTTLED);
349+
const response =
350+
await client.parse(
351+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
352+
"Opera/9.80 (Linux armv7l) Presto/2.12.407 Version/12.51 , D50u-D1-UHD/V1.5.16-UHD (Vizio, D50u-D1, Wireless)"
353+
);
354+
expect(response.data.length).eq(2);
355+
expect(response.data[0].name).not.null;
356+
expect(response.data[1].name).not.null;
357+
});
358+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@ipregistry/client",
33
"description": "Official Ipregistry Javascript Library (ES5, ES6+, TypeScript).",
4-
"version": "4.2.0",
4+
"version": "4.3.0",
55
"main": "./dist/index.js",
66
"browser": "./dist/browser/index.js",
77
"module": "./dist/esm/index.js",

0 commit comments

Comments
 (0)