-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.js
172 lines (144 loc) · 3.98 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
Name: geoip2ws - test.js
Description: Maxmind GeoIP2 Web Services for node.js
Author: Franklin (https://fvdm.com)
Source & docs: https://github.com/fvdm/nodejs-geoip2ws
Feedback: https://github.com/fvdm/nodejs-geoip2ws/issues
License: Unlicense (public domain, see LICENSE file)
*/
const doTest = require( 'dotest' );
const pkg = require( './' );
// Setup
// set env GEOIP2WS_USERID and GEOIP2WS_LICENSE (CI tests)
const config = {
userId: process.env.GEOIP2WS_USERID || null,
licenseKey: process.env.GEOIP2WS_LICENSE || null,
service: process.env.GEOIP2WS_SERVICE || 'city',
endpoint: process.env.GEOIP2WS_ENDPOINT || 'https://geoip.maxmind.com',
timeout: process.env.GEOIP2WS_TIMEOUT || 5000,
};
// TESTS
doTest.add( 'Configuration', test => {
if ( ! config.userId || ! config.licenseKey ) {
config.endpoint = 'https://fvdm.com/u/ci_test.php?a=geoip2ws&b=';
test()
.warn( 'GEOIP2WS_USERID or GEOIP2WS_LICENSE not set' )
.info( 'Using test endpoint with fake data' )
;
}
test()
.info( `config.service: ${config.service}` )
.info( `config.endpoint: ${config.endpoint}` )
.info( `config.timeout: ${config.timeout}` )
.done()
;
} );
// Module
doTest.add( 'Module', test => {
test()
.isFunction( 'fail', 'exports', pkg )
.done();
} );
// Test success
doTest.add( 'lookup - object', async test => {
try {
const data = await pkg( {
...config,
ip: '194.109.6.66',
service: config.service,
} );
test()
.isObject( 'fail', 'data', data )
.isArray( 'fail', 'data.subdivisions', data?.subdivisions )
.isString( 'fail', 'data.most_specific_subdivision', data?.most_specific_subdivision?.iso_code )
.isNotEmpty( 'fail', 'data.most_specific_subdivision', data?.most_specific_subdivision?.iso_code )
.done()
;
}
catch ( err ) {
test( err ).done();
}
} );
// Satellite IPs have no geo location
doTest.add( 'lookup - data.subdivisions array', async test => {
try {
const data = await pkg( {
...config,
ip: '194.109.6.93',
} );
test()
.isArray( 'fail', 'data.subdivisions', data.subdivisions )
.isNotEmpty( 'warn', 'data.subdivisions', data.subdivisions )
.done()
;
}
catch ( err ) {
test( err ).done();
}
} );
// IP without subdivisions
doTest.add( 'lookup - IP without subdivisions (Japan)', async test => {
try {
const data = await pkg( {
...config,
ip: '117.104.133.1',
} );
test()
.isArray( 'fail', 'data.subdivisions', data.subdivisions )
.isEmpty( 'fail', 'data.subdivisions', data.subdivisions )
.isObject( 'fail', 'data.most_specific_subdivision', data.most_specific_subdivision )
.isEmpty( 'fail', 'data.most_specific_subdivision', data.most_specific_subdivision )
.done()
;
}
catch ( err ) {
test( err ).done();
}
} );
// Errors
doTest.add( 'Error: from API', async test => {
let data;
let error;
try {
data = await pkg( {
userId: config.userId,
licenseKey: config.licenseKey,
ip: '0.0.0.0',
} );
}
catch ( err ) {
error = err;
}
test()
.isError( 'fail', 'catch', error )
.isNotEmpty( 'fail', 'error.message', error?.message )
.isRegexpMatch( 'fail', 'error.message', error?.message, /^API: .+/ )
.isString( 'fail', 'error.reason', error?.reason )
.isNotEmpty( 'warn', 'error.reason', error?.reason )
.isUndefined( 'fail', 'data', data )
.done()
;
} );
doTest.add( 'Error: timeout', async test => {
let data;
let error;
try {
data = await pkg( {
userId: config.userId,
licenseKey: config.licenseKey,
timeout: 1,
} );
}
catch ( err ) {
error = err;
}
test()
.isError( 'fail', 'catch', error )
.isNotEmpty( 'fail', 'error.message', error?.message )
.isExactly( 'fail', 'error.name', error?.name, 'TimeoutError' )
.isUndefined( 'fail', 'data', data )
.done()
;
} );
// Start the tests
doTest.run();