-
Notifications
You must be signed in to change notification settings - Fork 9
/
test.js
83 lines (67 loc) · 2.33 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
const assert = require('assert');
const dns = require('dns');
const net = require('net');
const icmp = require('./index');
const check = new Promise(resolve => {
dns.lookup('google.com', (err) => {
if (err && err.code == "ENOTFOUND") {
fail();
} else {
assert.ok(true);
resolve(true);
}
});
});
// describe('Offline ICMP', () => {
// describe('ping 127.0.0.1', () => {
// const ping = icmp.ping('127.0.0.1');
// try {
// it('ip property is 127.0.0.1', async () => {
// const { ip } = await ping;
// console.log(ip)
// assert.strictEqual(ip, '127.0.0.1');
// });
// it('open property is true', async () => {
// const { open } = await ping;
// assert.strictEqual(open, true);
// });
// } catch (e) { fail(e) }
// });
// });
describe('Online ICMP', () => {
it('internet connection', () => {
check.then(res => res ? assert.ok(true) : fail());
});
check.then(res => {
if (!res) return;
describe('ping www.google.com', () => {
const ping = icmp.ping('www.google.com');
try {
it('ip property is an IP', async () => {
const { ip } = await ping;
assert.notStrictEqual(net.isIP(ip), 0);
});
it('open property is true', async () => {
const { open } = await ping;
assert(open);
});
} catch (e) { fail(e) }
});
describe('ping a non-existing domain', async () => {
try {
await icmp.ping('this-domain-does.not.exist');
} catch (error) {
try {
it('rejects with the error object', (done) => {
ping.catch(error => {
assert(error.message.indexOf('ENOTFOUND this-dns-does.not.exist'));
assert(error.code === 'ENOTFOUND');
assert(error.hostname === 'this-dns-does.not.exist');
done();
})
});
} catch (e) { fail(e) }
}
});
});
});