|
| 1 | +const t = require('tap') |
| 2 | + |
| 3 | +const dns = require('../lib/dns.js') |
| 4 | +const DEFAULT_OPTS = { ttl: 5 * 60 * 1000 } |
| 5 | + |
| 6 | +t.afterEach(() => dns.lookupCache.clear()) |
| 7 | + |
| 8 | +t.test('supports no options passed', async (t) => { |
| 9 | + let lookupCalled = 0 |
| 10 | + const fakeLookup = (hostname, options, callback) => { |
| 11 | + lookupCalled += 1 |
| 12 | + t.match(options, dns.defaultOptions, 'applied default options') |
| 13 | + process.nextTick(callback, null, '127.0.0.1', 4) |
| 14 | + } |
| 15 | + const lookup = dns.getLookup({ ...DEFAULT_OPTS, lookup: fakeLookup }) |
| 16 | + |
| 17 | + return new Promise((resolve) => { |
| 18 | + lookup('localhost', (err, address, family) => { |
| 19 | + t.equal(err, null, 'no error') |
| 20 | + t.equal(address, '127.0.0.1', 'got address') |
| 21 | + t.equal(family, 4, 'got family') |
| 22 | + t.equal(lookupCalled, 1, 'lookup was called once') |
| 23 | + resolve() |
| 24 | + }) |
| 25 | + }) |
| 26 | +}) |
| 27 | + |
| 28 | +t.test('supports family passed directly as options', async (t) => { |
| 29 | + let lookupCalled = 0 |
| 30 | + const fakeLookup = (hostname, options, callback) => { |
| 31 | + lookupCalled += 1 |
| 32 | + t.match(options, { ...dns.defaultOptions, family: 4 }, 'kept family setting') |
| 33 | + process.nextTick(callback, null, '127.0.0.1', 4) |
| 34 | + } |
| 35 | + const lookup = dns.getLookup({ ...DEFAULT_OPTS, lookup: fakeLookup }) |
| 36 | + |
| 37 | + return new Promise((resolve) => { |
| 38 | + lookup('localhost', 4, (err, address, family) => { |
| 39 | + t.equal(err, null, 'no error') |
| 40 | + t.equal(address, '127.0.0.1', 'got address') |
| 41 | + t.equal(family, 4, 'got family') |
| 42 | + t.equal(lookupCalled, 1, 'lookup was called once') |
| 43 | + resolve() |
| 44 | + }) |
| 45 | + }) |
| 46 | +}) |
| 47 | + |
| 48 | +t.test('reads from cache', async (t) => { |
| 49 | + let lookupCalled = 0 |
| 50 | + const fakeLookup = (hostname, options, callback) => { |
| 51 | + lookupCalled += 1 |
| 52 | + t.match(options, dns.defaultOptions, 'applied default options') |
| 53 | + process.nextTick(callback, null, '127.0.0.1', 4) |
| 54 | + } |
| 55 | + const lookup = dns.getLookup({ ...DEFAULT_OPTS, lookup: fakeLookup }) |
| 56 | + |
| 57 | + return new Promise((resolve) => { |
| 58 | + lookup('localhost', (err, address, family) => { |
| 59 | + t.equal(err, null, 'no error') |
| 60 | + t.equal(address, '127.0.0.1', 'got address') |
| 61 | + t.equal(family, 4, 'got family') |
| 62 | + t.equal(lookupCalled, 1, 'lookup was called once') |
| 63 | + resolve() |
| 64 | + }) |
| 65 | + }).then(() => new Promise((resolve) => { |
| 66 | + lookup('localhost', (err, address, family) => { |
| 67 | + t.equal(err, null, 'no error') |
| 68 | + t.equal(address, '127.0.0.1', 'got address') |
| 69 | + t.equal(family, 4, 'got family') |
| 70 | + t.equal(lookupCalled, 1, 'lookup was still only called once') |
| 71 | + resolve() |
| 72 | + }) |
| 73 | + })) |
| 74 | +}) |
| 75 | + |
| 76 | +t.test('does not cache errors', async (t) => { |
| 77 | + let lookupCalled = 0 |
| 78 | + const fakeLookup = (hostname, options, callback) => { |
| 79 | + lookupCalled += 1 |
| 80 | + if (lookupCalled === 1) { |
| 81 | + process.nextTick(callback, new Error('failed')) |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + t.match(options, dns.defaultOptions, 'applied default options') |
| 86 | + process.nextTick(callback, null, '127.0.0.1', 4) |
| 87 | + } |
| 88 | + const lookup = dns.getLookup({ ...DEFAULT_OPTS, lookup: fakeLookup }) |
| 89 | + |
| 90 | + return new Promise((resolve) => { |
| 91 | + lookup('localhost', (err, address, family) => { |
| 92 | + t.match(err, { message: 'failed' }, 'got the error') |
| 93 | + t.equal(lookupCalled, 1, 'lookup was called once') |
| 94 | + resolve() |
| 95 | + }) |
| 96 | + }).then(() => new Promise((resolve) => { |
| 97 | + lookup('localhost', (err, address, family) => { |
| 98 | + t.equal(err, null, 'no error') |
| 99 | + t.equal(address, '127.0.0.1', 'got address') |
| 100 | + t.equal(family, 4, 'got family') |
| 101 | + t.equal(lookupCalled, 2, 'lookup was now called twice') |
| 102 | + resolve() |
| 103 | + }) |
| 104 | + })).then(() => new Promise((resolve) => { |
| 105 | + lookup('localhost', (err, address, family) => { |
| 106 | + t.equal(err, null, 'no error') |
| 107 | + t.equal(address, '127.0.0.1', 'got address') |
| 108 | + t.equal(family, 4, 'got family') |
| 109 | + t.equal(lookupCalled, 2, 'lookup was still only called twice') |
| 110 | + resolve() |
| 111 | + }) |
| 112 | + })) |
| 113 | +}) |
| 114 | + |
| 115 | +t.test('varies when options change', async (t) => { |
| 116 | + let lookupCalled = 0 |
| 117 | + const fakeLookup = (hostname, options, callback) => { |
| 118 | + lookupCalled += 1 |
| 119 | + if (lookupCalled === 1) { |
| 120 | + t.match(options, dns.defaultOptions, 'applied default options') |
| 121 | + process.nextTick(callback, null, '127.0.0.1', 4) |
| 122 | + } else { |
| 123 | + t.match(options, { ...dns.defaultOptions, family: 6 }, 'kept family from second lookup') |
| 124 | + process.nextTick(callback, null, '::1', 6) |
| 125 | + } |
| 126 | + } |
| 127 | + const lookup = dns.getLookup({ ...DEFAULT_OPTS, lookup: fakeLookup }) |
| 128 | + |
| 129 | + return new Promise((resolve) => { |
| 130 | + lookup('localhost', (err, address, family) => { |
| 131 | + t.equal(err, null, 'no error') |
| 132 | + t.equal(address, '127.0.0.1', 'got address') |
| 133 | + t.equal(family, 4, 'got family') |
| 134 | + t.equal(lookupCalled, 1, 'lookup was called once') |
| 135 | + resolve() |
| 136 | + }) |
| 137 | + }).then(() => new Promise((resolve) => { |
| 138 | + lookup('localhost', { family: 6 }, (err, address, family) => { |
| 139 | + t.equal(err, null, 'no error') |
| 140 | + t.equal(address, '::1', 'got address') |
| 141 | + t.equal(family, 6, 'got family') |
| 142 | + t.equal(lookupCalled, 2, 'lookup was called twice') |
| 143 | + resolve() |
| 144 | + }) |
| 145 | + })) |
| 146 | +}) |
| 147 | + |
| 148 | +t.test('lookup can return all results', async (t) => { |
| 149 | + let lookupCalled = 0 |
| 150 | + const fakeLookup = (hostname, options, callback) => { |
| 151 | + lookupCalled += 1 |
| 152 | + t.match(options, { ...dns.defaultOptions, all: true }, 'applied default options') |
| 153 | + process.nextTick(callback, null, [{ |
| 154 | + address: '127.0.0.1', family: 4, |
| 155 | + }, { |
| 156 | + address: '::1', family: 6, |
| 157 | + }]) |
| 158 | + } |
| 159 | + const lookup = dns.getLookup({ ...DEFAULT_OPTS, lookup: fakeLookup }) |
| 160 | + |
| 161 | + return new Promise((resolve) => { |
| 162 | + lookup('localhost', { all: true }, (err, addresses) => { |
| 163 | + t.equal(err, null, 'no error') |
| 164 | + t.match(addresses, [{ |
| 165 | + address: '127.0.0.1', family: 4, |
| 166 | + }, { |
| 167 | + address: '::1', family: 6, |
| 168 | + }], 'got all addresses') |
| 169 | + t.equal(lookupCalled, 1, 'lookup was called once') |
| 170 | + resolve() |
| 171 | + }) |
| 172 | + }).then(() => new Promise((resolve) => { |
| 173 | + lookup('localhost', { all: true }, (err, addresses) => { |
| 174 | + t.equal(err, null, 'no error') |
| 175 | + t.match(addresses, [{ |
| 176 | + address: '127.0.0.1', family: 4, |
| 177 | + }, { |
| 178 | + address: '::1', family: 6, |
| 179 | + }], 'got all addresses') |
| 180 | + t.equal(lookupCalled, 1, 'lookup was called once') |
| 181 | + resolve() |
| 182 | + }) |
| 183 | + })) |
| 184 | +}) |
| 185 | + |
| 186 | +t.test('respects ttl option', async (t) => { |
| 187 | + let lookupCalled = 0 |
| 188 | + const fakeLookup = (hostname, options, callback) => { |
| 189 | + lookupCalled += 1 |
| 190 | + t.match(options, dns.defaultOptions, 'applied default options') |
| 191 | + process.nextTick(callback, null, '127.0.0.1', 4) |
| 192 | + } |
| 193 | + const lookup = dns.getLookup({ ttl: 10, lookup: fakeLookup }) |
| 194 | + |
| 195 | + return new Promise((resolve) => { |
| 196 | + lookup('localhost', (err, address, family) => { |
| 197 | + t.equal(err, null, 'no error') |
| 198 | + t.equal(address, '127.0.0.1', 'got address') |
| 199 | + t.equal(family, 4, 'got family') |
| 200 | + t.equal(lookupCalled, 1, 'lookup was called once') |
| 201 | + resolve() |
| 202 | + }) |
| 203 | + }).then(() => new Promise((resolve) => { |
| 204 | + lookup('localhost', (err, address, family) => { |
| 205 | + t.equal(err, null, 'no error') |
| 206 | + t.equal(address, '127.0.0.1', 'got address') |
| 207 | + t.equal(family, 4, 'got family') |
| 208 | + t.equal(lookupCalled, 1, 'lookup was still only called once') |
| 209 | + // delay before the next request to allow the ttl to invalidate |
| 210 | + setTimeout(resolve, 15) |
| 211 | + }) |
| 212 | + })).then(() => new Promise((resolve) => { |
| 213 | + lookup('localhost', (err, address, family) => { |
| 214 | + t.equal(err, null, 'no error') |
| 215 | + t.equal(address, '127.0.0.1', 'got address') |
| 216 | + t.equal(family, 4, 'got family') |
| 217 | + t.equal(lookupCalled, 2, 'lookup was now called twice') |
| 218 | + resolve() |
| 219 | + }) |
| 220 | + })) |
| 221 | +}) |
0 commit comments