-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
127 lines (109 loc) · 3.16 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
'use strict';
var IpUtil = require('./index.js');
var co = require('co');
var isIp = IpUtil.isIP;
describe('ipUtil utils method', function () {
it('#isIp()', function() {
isIp('1.1').should.be.false;
isIp('').should.be.false;
isIp().should.be.false;
isIp('300.255.255.255').should.be.false;
isIp('0.0.0').should.be.false;
isIp('0.0.0.0').should.be.true;
isIp('255.255.255.255').should.be.true;
});
it('#ipToLong()', function () {
var l = IpUtil.ip2Long('61.172.201.235');
(l).should.be.type('number');
(l).should.be.eql(1034734059);
var l2 = IpUtil.ip2Long('173.194.127.243');
(l2).should.be.eql(2915205107);
var l3 = IpUtil.ip2Long('224.224.224.224');
(l3).should.be.eql(3772834016);
});
it('#longToIp()', function () {
var ip = IpUtil.long2Ip(1034734059);
(ip).should.be.type('string');
(ip).should.be.eql('61.172.201.235');
});
});
describe('find ip info, gbk', function() {
var ipUtil;
before(function(done) {
ipUtil = new IpUtil('ip-gbk.txt');
ipUtil.on('done', function() {
done();
});
});
it('getIPInfo()', function() {
(ipUtil.getIpInfo('10.1.1.1') === null).should.be.ok;
(ipUtil.getIpInfo('10.1.') === null).should.be.ok;
ipUtil.getIpInfo('1.26.6.0').city.should.be.eql('呼伦贝尔');
});
});
describe('find ip info, utf8', function() {
var ipUtil;
before(function(done) {
ipUtil = new IpUtil('ip-utf8.txt', 'utf8');
ipUtil.on('done', function() {
done();
});
});
it('getIPInfo()', function() {
(ipUtil.getIpInfo('10.1.1.1') === null).should.be.ok;
(ipUtil.getIpInfo('10.1.') === null).should.be.ok;
ipUtil.getIpInfo('1.26.6.0').city.should.be.eql('呼伦贝尔');
});
});
describe('test generator', function() {
var ipUtil;
before(function(done) {
co(function*(){
ipUtil = yield IpUtil.getIpUtil('ip-utf8.txt', 'utf8');
done();
})();
});
it('getIPInfo()', function() {
(ipUtil.getIpInfo('10.1.1.1') === null).should.be.ok;
(ipUtil.getIpInfo('10.1.') === null).should.be.ok;
ipUtil.getIpInfo('1.26.6.0').city.should.be.eql('呼伦贝尔');
});
});
describe('test generator not found file.', function() {
var ipUtil;
before(function(done) {
co(function* (){
try {
ipUtil = yield IpUtil.getIpUtil('ip-utf81.txt', 'utf8');
} catch(e) {
ipUtil = null;
}
done();
})();
});
it('ipUtil is null.', function() {
(ipUtil === null).should.be.ok;
});
});
describe('test loading the foreign ip address.', function() {
var ipUtil;
before(function(done) {
ipUtil = new IpUtil('ip-gbk.txt', function(country) {
if (country === '中国') {
return false;
} else {
return true;
}
});
ipUtil.on('done', function() {
done();
});
});
it('国内 ip 返回都为空.', function() {
(ipUtil.getIpInfo('10.1.1.1') === null).should.be.ok;
(ipUtil.getIpInfo('1.26.6.0') === null).should.be.ok;
(ipUtil.getIpInfo('1.194.184.0') === null).should.be.ok;
ipUtil.getIpInfo('1.0.0.0').country.should.equal('澳大利亚');
ipUtil.getIpInfo('1.1.64.0').country.should.equal('日本');
});
});