-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
test.js
64 lines (50 loc) · 1.69 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
import {promisify} from 'util';
import dns from 'dns';
import http from 'http';
import test from 'ava';
import isReachable from '.';
const dnsResolveP = promisify(dns.resolve);
const dnsLookupP = promisify(dns.lookup);
test('hostname', async t => {
t.true(await isReachable('google.com'));
});
test('hostname and port', async t => {
t.true(await isReachable('google.com:443'));
});
test('ip', async t => {
t.true(await isReachable(await dnsResolveP('google.com')));
});
test('ip and protocol', async t => {
t.true(await isReachable(`https://${(await dnsLookupP('google.com')).address}`));
});
test('multiple https urls', async t => {
t.true(await isReachable(['https://google.com', 'https://baidu.com']));
});
test('http server on custom port', async t => {
const server = http.createServer((_, response) => {
response.writeHead(200).end();
}).listen(8080);
t.true(await isReachable('http://localhost:8080'));
server.close();
});
test('unreachable http server on custom port', async t => {
t.false(await isReachable('http://localhost:8081'));
});
test('imap host and port', async t => {
t.true(await isReachable('imap.gmail.com:995'));
});
test('unreachable hostname', async t => {
t.false(await isReachable('343645335341233123125235623452344123.local'));
});
test('unknown service', async t => {
t.false(await isReachable('343645335341233123125235623452344123.local:-1'));
});
test('unreachable pathname', async t => {
t.false(await isReachable('https://google.com/notfound.js'));
});
test('with timeout', async t => {
t.true(await isReachable('https://google.com', {timeout: 3000}));
});
test('with impossible timeout', async t => {
t.false(await isReachable('https://google.com', {timeout: 1}));
});