1616
1717import {
1818 ApiError ,
19- ClientError , InMemoryCache ,
19+ ClientError ,
20+ InMemoryCache ,
2021 IpInfo ,
2122 IpregistryClient ,
2223 IpregistryConfigBuilder ,
@@ -30,6 +31,107 @@ import {expect} from 'chai';
3031const API_KEY = process . env . IPREGISTRY_API_KEY || 'tryou' ;
3132const 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+
33135describe ( '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-
253254describe ( '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+ } ) ;
0 commit comments