/
test-dns-lookup-promises.js
139 lines (130 loc) Β· 4.09 KB
/
test-dns-lookup-promises.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
// Flags: --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
const { internalBinding } = require('internal/test/binding');
const cares = internalBinding('cares_wrap');
// Stub `getaddrinfo` to proxy its call dynamic stub. This has to be done before
// we load the `dns` module to guarantee that the `dns` module uses the stub.
let getaddrinfoStub = null;
cares.getaddrinfo = (req) => getaddrinfoStub(req);
const dnsPromises = require('dns').promises;
function getaddrinfoNegative() {
return function getaddrinfoNegativeHandler(req) {
const originalReject = req.reject;
req.resolve = common.mustNotCall();
req.reject = common.mustCall(originalReject);
req.oncomplete(internalBinding('uv').UV_ENOMEM);
};
}
function getaddrinfoPositive(addresses) {
return function getaddrinfo_positive(req) {
const originalResolve = req.resolve;
req.reject = common.mustNotCall();
req.resolve = common.mustCall(originalResolve);
req.oncomplete(null, addresses);
};
}
async function lookupPositive() {
[
{
stub: getaddrinfoPositive(['::1']),
factory: () => dnsPromises.lookup('example.com'),
expectation: { address: '::1', family: 6 }
},
{
stub: getaddrinfoPositive(['127.0.0.1']),
factory: () => dnsPromises.lookup('example.com'),
expectation: { address: '127.0.0.1', family: 4 }
},
{
stub: getaddrinfoPositive(['127.0.0.1'], { family: 4 }),
factory: () => dnsPromises.lookup('example.com'),
expectation: { address: '127.0.0.1', family: 4 }
},
{
stub: getaddrinfoPositive(['some-address']),
factory: () => dnsPromises.lookup('example.com'),
expectation: { address: 'some-address', family: 0 }
},
{
stub: getaddrinfoPositive(['some-address2']),
factory: () => dnsPromises.lookup('example.com', { family: 6 }),
expectation: { address: 'some-address2', family: 6 }
}
].forEach(async ({ stub, factory, expectation }) => {
getaddrinfoStub = stub;
assert.deepStrictEqual(await factory(), expectation);
});
}
async function lookupNegative() {
getaddrinfoStub = getaddrinfoNegative();
const expected = {
code: 'ENOMEM',
hostname: 'example.com',
syscall: 'getaddrinfo'
};
return assert.rejects(dnsPromises.lookup('example.com'), expected);
}
async function lookupallPositive() {
[
{
stub: getaddrinfoPositive(['::1', '::2']),
factory: () => dnsPromises.lookup('example', { all: true }),
expectation: [
{ address: '::1', family: 6 },
{ address: '::2', family: 6 }
]
},
{
stub: getaddrinfoPositive(['::1', '::2']),
factory: () => dnsPromises.lookup('example', { all: true, family: 4 }),
expectation: [
{ address: '::1', family: 4 },
{ address: '::2', family: 4 }
]
},
{
stub: getaddrinfoPositive(['127.0.0.1', 'some-address']),
factory: () => dnsPromises.lookup('example', { all: true }),
expectation: [
{ address: '127.0.0.1', family: 4 },
{ address: 'some-address', family: 0 }
]
},
{
stub: getaddrinfoPositive(['127.0.0.1', 'some-address']),
factory: () => dnsPromises.lookup('example', { all: true, family: 6 }),
expectation: [
{ address: '127.0.0.1', family: 6 },
{ address: 'some-address', family: 6 }
]
},
{
stub: getaddrinfoPositive([]),
factory: () => dnsPromises.lookup('example', { all: true }),
expectation: []
}
].forEach(async ({ stub, factory, expectation }) => {
getaddrinfoStub = stub;
assert.deepStrictEqual(await factory(), expectation);
});
}
async function lookupallNegative() {
getaddrinfoStub = getaddrinfoNegative();
const expected = {
code: 'ENOMEM',
hostname: 'example.com',
syscall: 'getaddrinfo'
};
return assert.rejects(dnsPromises.lookup('example.com', { all: true }),
expected);
}
(async () => {
await Promise.all([
lookupPositive(),
lookupNegative(),
lookupallPositive(),
lookupallNegative()
]);
})().then(common.mustCall());