| 1 |
'use strict' |
| 2 |
|
| 3 |
const FakeTimers = require('@sinonjs/fake-timers') |
| 4 |
const { test, after } = require('node:test') |
| 5 |
const { isIP } = require('node:net') |
| 6 |
const { lookup } = require('node:dns') |
| 7 |
const { createServer } = require('node:http') |
| 8 |
const { createServer: createSecureServer } = require('node:https') |
| 9 |
const { once } = require('node:events') |
| 10 |
|
| 11 |
const { tspl } = require('@matteo.collina/tspl') |
| 12 |
const pem = require('@metcoder95/https-pem') |
| 13 |
|
| 14 |
const { interceptors, Agent, Client, Pool, request } = require('../..') |
| 15 |
const { dns } = interceptors |
| 16 |
|
| 17 |
// Helper to check if IPv6 is available for localhost |
| 18 |
// This is called synchronously at test definition time |
| 19 |
function hasIPv6LocalhostSync () { |
| 20 |
let ipv6Available = false |
| 21 |
try { |
| 22 |
const addresses = lookup.sync('localhost', { all: true, family: 6 }) |
| 23 |
ipv6Available = addresses != null && addresses.length > 0 && addresses.some(addr => addr.family === 6) |
| 24 |
} catch (err) { |
| 25 |
// If lookup fails, assume IPv6 is not available |
| 26 |
ipv6Available = false |
| 27 |
} |
| 28 |
return ipv6Available |
| 29 |
} |
| 30 |
|
| 31 |
// Check IPv6 availability once at module load time |
| 32 |
const ipv6Available = hasIPv6LocalhostSync() |
| 33 |
|
| 34 |
test('Should validate options', t => { |
| 35 |
t = tspl(t, { plan: 11 }) |
| 36 |
|
| 37 |
t.throws(() => dns({ dualStack: 'true' }), { code: 'UND_ERR_INVALID_ARG' }) |
| 38 |
t.throws(() => dns({ dualStack: 0 }), { code: 'UND_ERR_INVALID_ARG' }) |
| 39 |
t.throws(() => dns({ affinity: '4' }), { code: 'UND_ERR_INVALID_ARG' }) |
| 40 |
t.throws(() => dns({ affinity: 7 }), { code: 'UND_ERR_INVALID_ARG' }) |
| 41 |
t.throws(() => dns({ maxTTL: -1 }), { code: 'UND_ERR_INVALID_ARG' }) |
| 42 |
t.throws(() => dns({ maxTTL: '0' }), { code: 'UND_ERR_INVALID_ARG' }) |
| 43 |
t.throws(() => dns({ maxItems: '1' }), { code: 'UND_ERR_INVALID_ARG' }) |
| 44 |
t.throws(() => dns({ maxItems: -1 }), { code: 'UND_ERR_INVALID_ARG' }) |
| 45 |
t.throws(() => dns({ lookup: {} }), { code: 'UND_ERR_INVALID_ARG' }) |
| 46 |
t.throws(() => dns({ pick: [] }), { code: 'UND_ERR_INVALID_ARG' }) |
| 47 |
t.throws(() => dns({ storage: new Map() }), { code: 'UND_ERR_INVALID_ARG' }) |
| 48 |
}) |
| 49 |
|
| 50 |
test('Should automatically resolve IPs (dual stack)', async t => { |
| 51 |
t = tspl(t, { plan: 8 }) |
| 52 |
|
| 53 |
const hostsnames = [] |
| 54 |
const server = createServer({ joinDuplicateHeaders: true }) |
| 55 |
const requestOptions = { |
| 56 |
method: 'GET', |
| 57 |
path: '/', |
| 58 |
headers: { |
| 59 |
'content-type': 'application/json' |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
server.on('request', (req, res) => { |
| 64 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 65 |
res.end('hello world!') |
| 66 |
}) |
| 67 |
|
| 68 |
server.listen(0) |
| 69 |
|
| 70 |
await once(server, 'listening') |
| 71 |
|
| 72 |
const client = new Agent().compose([ |
| 73 |
dispatch => { |
| 74 |
return (opts, handler) => { |
| 75 |
const url = new URL(opts.origin) |
| 76 |
|
| 77 |
t.equal(hostsnames.includes(url.hostname), false) |
| 78 |
|
| 79 |
if (url.hostname[0] === '[') { |
| 80 |
// [::1] -> ::1 |
| 81 |
t.equal(isIP(url.hostname.slice(1, 4)), 6) |
| 82 |
} else { |
| 83 |
t.equal(isIP(url.hostname), 4) |
| 84 |
} |
| 85 |
|
| 86 |
hostsnames.push(url.hostname) |
| 87 |
|
| 88 |
return dispatch(opts, handler) |
| 89 |
} |
| 90 |
}, |
| 91 |
dns({ |
| 92 |
lookup: (_origin, _opts, cb) => { |
| 93 |
cb(null, [ |
| 94 |
{ |
| 95 |
address: '::1', |
| 96 |
family: 6 |
| 97 |
}, |
| 98 |
{ |
| 99 |
address: '127.0.0.1', |
| 100 |
family: 4 |
| 101 |
} |
| 102 |
]) |
| 103 |
} |
| 104 |
}) |
| 105 |
]) |
| 106 |
|
| 107 |
after(async () => { |
| 108 |
await client.close() |
| 109 |
server.close() |
| 110 |
|
| 111 |
await once(server, 'close') |
| 112 |
}) |
| 113 |
|
| 114 |
const response = await client.request({ |
| 115 |
...requestOptions, |
| 116 |
origin: `http://localhost:${server.address().port}` |
| 117 |
}) |
| 118 |
|
| 119 |
t.equal(response.statusCode, 200) |
| 120 |
t.equal(await response.body.text(), 'hello world!') |
| 121 |
|
| 122 |
const response2 = await client.request({ |
| 123 |
...requestOptions, |
| 124 |
origin: `http://localhost:${server.address().port}` |
| 125 |
}) |
| 126 |
|
| 127 |
t.equal(response2.statusCode, 200) |
| 128 |
t.equal(await response2.body.text(), 'hello world!') |
| 129 |
}) |
| 130 |
|
| 131 |
test('Should respect DNS origin hostname for SNI on TLS', async t => { |
| 132 |
t = tspl(t, { plan: 12 }) |
| 133 |
|
| 134 |
const hostsnames = [] |
| 135 |
const server = createSecureServer(pem) |
| 136 |
const requestOptions = { |
| 137 |
method: 'GET', |
| 138 |
path: '/', |
| 139 |
headers: { |
| 140 |
'content-type': 'application/json' |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
server.on('request', (req, res) => { |
| 145 |
t.equal(req.headers.host, `localhost:${server.address().port}`) |
| 146 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 147 |
res.end('hello world!') |
| 148 |
}) |
| 149 |
|
| 150 |
server.listen(0) |
| 151 |
|
| 152 |
await once(server, 'listening') |
| 153 |
|
| 154 |
const client = new Agent({ |
| 155 |
connect: { |
| 156 |
rejectUnauthorized: false |
| 157 |
} |
| 158 |
}).compose([ |
| 159 |
dispatch => { |
| 160 |
return (opts, handler) => { |
| 161 |
const url = new URL(opts.origin) |
| 162 |
|
| 163 |
t.equal(hostsnames.includes(url.hostname), false) |
| 164 |
t.equal(opts.servername, 'localhost') |
| 165 |
|
| 166 |
if (url.hostname[0] === '[') { |
| 167 |
// [::1] -> ::1 |
| 168 |
t.equal(isIP(url.hostname.slice(1, 4)), 6) |
| 169 |
} else { |
| 170 |
t.equal(isIP(url.hostname), 4) |
| 171 |
} |
| 172 |
|
| 173 |
hostsnames.push(url.hostname) |
| 174 |
|
| 175 |
return dispatch(opts, handler) |
| 176 |
} |
| 177 |
}, |
| 178 |
dns({ |
| 179 |
lookup: (_origin, _opts, cb) => { |
| 180 |
cb(null, [ |
| 181 |
{ |
| 182 |
address: '::1', |
| 183 |
family: 6 |
| 184 |
}, |
| 185 |
{ |
| 186 |
address: '127.0.0.1', |
| 187 |
family: 4 |
| 188 |
} |
| 189 |
]) |
| 190 |
} |
| 191 |
}) |
| 192 |
]) |
| 193 |
|
| 194 |
after(async () => { |
| 195 |
await client.close() |
| 196 |
server.close() |
| 197 |
|
| 198 |
await once(server, 'close') |
| 199 |
}) |
| 200 |
|
| 201 |
const response = await client.request({ |
| 202 |
...requestOptions, |
| 203 |
origin: `https://localhost:${server.address().port}` |
| 204 |
}) |
| 205 |
|
| 206 |
t.equal(response.statusCode, 200) |
| 207 |
t.equal(await response.body.text(), 'hello world!') |
| 208 |
|
| 209 |
const response2 = await client.request({ |
| 210 |
...requestOptions, |
| 211 |
origin: `https://localhost:${server.address().port}` |
| 212 |
}) |
| 213 |
|
| 214 |
t.equal(response2.statusCode, 200) |
| 215 |
t.equal(await response2.body.text(), 'hello world!') |
| 216 |
}) |
| 217 |
|
| 218 |
test('Should recover on network errors (dual stack - 4)', async t => { |
| 219 |
t = tspl(t, { plan: 7 }) |
| 220 |
|
| 221 |
let counter = 0 |
| 222 |
const server = createServer({ joinDuplicateHeaders: true }) |
| 223 |
const requestOptions = { |
| 224 |
method: 'GET', |
| 225 |
path: '/', |
| 226 |
headers: { |
| 227 |
'content-type': 'application/json' |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
server.on('request', (req, res) => { |
| 232 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 233 |
res.end('hello world!') |
| 234 |
}) |
| 235 |
|
| 236 |
server.listen(0, '::1') |
| 237 |
|
| 238 |
await once(server, 'listening') |
| 239 |
|
| 240 |
const client = new Agent().compose([ |
| 241 |
dispatch => { |
| 242 |
return (opts, handler) => { |
| 243 |
++counter |
| 244 |
const url = new URL(opts.origin) |
| 245 |
|
| 246 |
switch (counter) { |
| 247 |
case 1: |
| 248 |
t.equal(isIP(url.hostname), 4) |
| 249 |
break |
| 250 |
|
| 251 |
case 2: |
| 252 |
// [::1] -> ::1 |
| 253 |
t.equal(isIP(url.hostname.slice(1, 4)), 6) |
| 254 |
break |
| 255 |
|
| 256 |
case 3: |
| 257 |
// [::1] -> ::1 |
| 258 |
t.equal(isIP(url.hostname.slice(1, 4)), 6) |
| 259 |
break |
| 260 |
default: |
| 261 |
t.fail('should not reach this point') |
| 262 |
} |
| 263 |
|
| 264 |
return dispatch(opts, handler) |
| 265 |
} |
| 266 |
}, |
| 267 |
dns({ |
| 268 |
lookup: (_origin, _opts, cb) => { |
| 269 |
cb(null, [ |
| 270 |
{ |
| 271 |
address: '::1', |
| 272 |
family: 6 |
| 273 |
}, |
| 274 |
{ |
| 275 |
address: '127.0.0.1', |
| 276 |
family: 4 |
| 277 |
} |
| 278 |
]) |
| 279 |
} |
| 280 |
}) |
| 281 |
]) |
| 282 |
|
| 283 |
after(async () => { |
| 284 |
await client.close() |
| 285 |
server.close() |
| 286 |
|
| 287 |
await once(server, 'close') |
| 288 |
}) |
| 289 |
|
| 290 |
const response = await client.request({ |
| 291 |
...requestOptions, |
| 292 |
origin: `http://localhost:${server.address().port}` |
| 293 |
}) |
| 294 |
|
| 295 |
t.equal(response.statusCode, 200) |
| 296 |
t.equal(await response.body.text(), 'hello world!') |
| 297 |
|
| 298 |
const response2 = await client.request({ |
| 299 |
...requestOptions, |
| 300 |
origin: `http://localhost:${server.address().port}` |
| 301 |
}) |
| 302 |
|
| 303 |
t.equal(response2.statusCode, 200) |
| 304 |
t.equal(await response2.body.text(), 'hello world!') |
| 305 |
}) |
| 306 |
|
| 307 |
test('Should recover on network errors (dual stack - 6)', async t => { |
| 308 |
t = tspl(t, { plan: 7 }) |
| 309 |
|
| 310 |
let counter = 0 |
| 311 |
const server = createServer({ joinDuplicateHeaders: true }) |
| 312 |
const requestOptions = { |
| 313 |
method: 'GET', |
| 314 |
path: '/', |
| 315 |
headers: { |
| 316 |
'content-type': 'application/json' |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
server.on('request', (req, res) => { |
| 321 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 322 |
res.end('hello world!') |
| 323 |
}) |
| 324 |
|
| 325 |
server.listen(0, '127.0.0.1') |
| 326 |
|
| 327 |
await once(server, 'listening') |
| 328 |
|
| 329 |
const client = new Agent().compose([ |
| 330 |
dispatch => { |
| 331 |
return (opts, handler) => { |
| 332 |
++counter |
| 333 |
const url = new URL(opts.origin) |
| 334 |
|
| 335 |
switch (counter) { |
| 336 |
case 1: |
| 337 |
t.equal(isIP(url.hostname), 4) |
| 338 |
break |
| 339 |
|
| 340 |
case 2: |
| 341 |
// [::1] -> ::1 |
| 342 |
t.equal(isIP(url.hostname.slice(1, 4)), 6) |
| 343 |
break |
| 344 |
|
| 345 |
case 3: |
| 346 |
// [::1] -> ::1 |
| 347 |
t.equal(isIP(url.hostname), 4) |
| 348 |
break |
| 349 |
default: |
| 350 |
t.fail('should not reach this point') |
| 351 |
} |
| 352 |
|
| 353 |
return dispatch(opts, handler) |
| 354 |
} |
| 355 |
}, |
| 356 |
dns({ |
| 357 |
lookup: (_origin, _opts, cb) => { |
| 358 |
cb(null, [ |
| 359 |
{ |
| 360 |
address: '::1', |
| 361 |
family: 6 |
| 362 |
}, |
| 363 |
{ |
| 364 |
address: '127.0.0.1', |
| 365 |
family: 4 |
| 366 |
} |
| 367 |
]) |
| 368 |
} |
| 369 |
}) |
| 370 |
]) |
| 371 |
|
| 372 |
after(async () => { |
| 373 |
await client.close() |
| 374 |
server.close() |
| 375 |
|
| 376 |
await once(server, 'close') |
| 377 |
}) |
| 378 |
|
| 379 |
const response = await client.request({ |
| 380 |
...requestOptions, |
| 381 |
origin: `http://localhost:${server.address().port}` |
| 382 |
}) |
| 383 |
|
| 384 |
t.equal(response.statusCode, 200) |
| 385 |
t.equal(await response.body.text(), 'hello world!') |
| 386 |
|
| 387 |
const response2 = await client.request({ |
| 388 |
...requestOptions, |
| 389 |
origin: `http://localhost:${server.address().port}` |
| 390 |
}) |
| 391 |
|
| 392 |
t.equal(response2.statusCode, 200) |
| 393 |
t.equal(await response2.body.text(), 'hello world!') |
| 394 |
}) |
| 395 |
|
| 396 |
test('Should throw when on dual-stack disabled (4)', async t => { |
| 397 |
t = tspl(t, { plan: 2 }) |
| 398 |
|
| 399 |
let counter = 0 |
| 400 |
const requestOptions = { |
| 401 |
method: 'GET', |
| 402 |
path: '/', |
| 403 |
headers: { |
| 404 |
'content-type': 'application/json' |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
const client = new Agent().compose([ |
| 409 |
dispatch => { |
| 410 |
return (opts, handler) => { |
| 411 |
++counter |
| 412 |
const url = new URL(opts.origin) |
| 413 |
|
| 414 |
switch (counter) { |
| 415 |
case 1: |
| 416 |
t.equal(isIP(url.hostname), 4) |
| 417 |
break |
| 418 |
|
| 419 |
default: |
| 420 |
t.fail('should not reach this point') |
| 421 |
} |
| 422 |
|
| 423 |
return dispatch(opts, handler) |
| 424 |
} |
| 425 |
}, |
| 426 |
dns({ dualStack: false, affinity: 4 }) |
| 427 |
]) |
| 428 |
|
| 429 |
const promise = client.request({ |
| 430 |
...requestOptions, |
| 431 |
origin: 'http://localhost:1234' |
| 432 |
}) |
| 433 |
|
| 434 |
await t.rejects(promise, 'ECONNREFUSED') |
| 435 |
|
| 436 |
await t.completed |
| 437 |
}) |
| 438 |
|
| 439 |
test('Should throw when on dual-stack disabled (6)', { skip: !ipv6Available }, async t => { |
| 440 |
t = tspl(t, { plan: 2 }) |
| 441 |
|
| 442 |
let counter = 0 |
| 443 |
const requestOptions = { |
| 444 |
method: 'GET', |
| 445 |
path: '/', |
| 446 |
headers: { |
| 447 |
'content-type': 'application/json' |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
const client = new Agent().compose([ |
| 452 |
dispatch => { |
| 453 |
return (opts, handler) => { |
| 454 |
++counter |
| 455 |
const url = new URL(opts.origin) |
| 456 |
|
| 457 |
switch (counter) { |
| 458 |
case 1: |
| 459 |
// [::1] -> ::1 |
| 460 |
t.equal(isIP(url.hostname.slice(1, 4)), 6) |
| 461 |
break |
| 462 |
|
| 463 |
default: |
| 464 |
t.fail('should not reach this point') |
| 465 |
} |
| 466 |
|
| 467 |
return dispatch(opts, handler) |
| 468 |
} |
| 469 |
}, |
| 470 |
dns({ dualStack: false, affinity: 6 }) |
| 471 |
]) |
| 472 |
|
| 473 |
const promise = client.request({ |
| 474 |
...requestOptions, |
| 475 |
origin: 'http://localhost:9999' |
| 476 |
}) |
| 477 |
|
| 478 |
await t.rejects(promise, 'ECONNREFUSED') |
| 479 |
|
| 480 |
await t.completed |
| 481 |
}) |
| 482 |
|
| 483 |
test('Should automatically resolve IPs (dual stack disabled - 4)', async t => { |
| 484 |
t = tspl(t, { plan: 6 }) |
| 485 |
|
| 486 |
let counter = 0 |
| 487 |
const server = createServer({ joinDuplicateHeaders: true }) |
| 488 |
const requestOptions = { |
| 489 |
method: 'GET', |
| 490 |
path: '/', |
| 491 |
headers: { |
| 492 |
'content-type': 'application/json' |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
server.on('request', (req, res) => { |
| 497 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 498 |
res.end('hello world!') |
| 499 |
}) |
| 500 |
|
| 501 |
server.listen(0) |
| 502 |
|
| 503 |
await once(server, 'listening') |
| 504 |
|
| 505 |
const client = new Agent().compose([ |
| 506 |
dispatch => { |
| 507 |
return (opts, handler) => { |
| 508 |
++counter |
| 509 |
const url = new URL(opts.origin) |
| 510 |
|
| 511 |
switch (counter) { |
| 512 |
case 1: |
| 513 |
t.equal(isIP(url.hostname), 4) |
| 514 |
break |
| 515 |
|
| 516 |
case 2: |
| 517 |
// [::1] -> ::1 |
| 518 |
t.equal(isIP(url.hostname), 4) |
| 519 |
break |
| 520 |
default: |
| 521 |
t.fail('should not reach this point') |
| 522 |
} |
| 523 |
|
| 524 |
return dispatch(opts, handler) |
| 525 |
} |
| 526 |
}, |
| 527 |
dns({ dualStack: false }) |
| 528 |
]) |
| 529 |
|
| 530 |
after(async () => { |
| 531 |
await client.close() |
| 532 |
server.close() |
| 533 |
|
| 534 |
await once(server, 'close') |
| 535 |
}) |
| 536 |
|
| 537 |
const response = await client.request({ |
| 538 |
...requestOptions, |
| 539 |
origin: `http://localhost:${server.address().port}` |
| 540 |
}) |
| 541 |
|
| 542 |
t.equal(response.statusCode, 200) |
| 543 |
t.equal(await response.body.text(), 'hello world!') |
| 544 |
|
| 545 |
const response2 = await client.request({ |
| 546 |
...requestOptions, |
| 547 |
origin: `http://localhost:${server.address().port}` |
| 548 |
}) |
| 549 |
|
| 550 |
t.equal(response2.statusCode, 200) |
| 551 |
t.equal(await response2.body.text(), 'hello world!') |
| 552 |
}) |
| 553 |
|
| 554 |
test('Should automatically resolve IPs (dual stack disabled - 6)', { skip: !ipv6Available }, async t => { |
| 555 |
t = tspl(t, { plan: 6 }) |
| 556 |
|
| 557 |
let counter = 0 |
| 558 |
const server = createServer({ joinDuplicateHeaders: true }) |
| 559 |
const requestOptions = { |
| 560 |
method: 'GET', |
| 561 |
path: '/', |
| 562 |
headers: { |
| 563 |
'content-type': 'application/json' |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
server.on('request', (req, res) => { |
| 568 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 569 |
res.end('hello world!') |
| 570 |
}) |
| 571 |
|
| 572 |
server.listen(0) |
| 573 |
|
| 574 |
await once(server, 'listening') |
| 575 |
|
| 576 |
const client = new Agent().compose([ |
| 577 |
dispatch => { |
| 578 |
return (opts, handler) => { |
| 579 |
++counter |
| 580 |
const url = new URL(opts.origin) |
| 581 |
|
| 582 |
switch (counter) { |
| 583 |
case 1: |
| 584 |
// [::1] -> ::1 |
| 585 |
t.equal(isIP(url.hostname.slice(1, 4)), 6) |
| 586 |
break |
| 587 |
|
| 588 |
case 2: |
| 589 |
// [::1] -> ::1 |
| 590 |
t.equal(isIP(url.hostname.slice(1, 4)), 6) |
| 591 |
break |
| 592 |
default: |
| 593 |
t.fail('should not reach this point') |
| 594 |
} |
| 595 |
|
| 596 |
return dispatch(opts, handler) |
| 597 |
} |
| 598 |
}, |
| 599 |
dns({ dualStack: false, affinity: 6 }) |
| 600 |
]) |
| 601 |
|
| 602 |
after(async () => { |
| 603 |
await client.close() |
| 604 |
server.close() |
| 605 |
|
| 606 |
await once(server, 'close') |
| 607 |
}) |
| 608 |
|
| 609 |
const response = await client.request({ |
| 610 |
...requestOptions, |
| 611 |
origin: `http://localhost:${server.address().port}` |
| 612 |
}) |
| 613 |
|
| 614 |
t.equal(response.statusCode, 200) |
| 615 |
t.equal(await response.body.text(), 'hello world!') |
| 616 |
|
| 617 |
const response2 = await client.request({ |
| 618 |
...requestOptions, |
| 619 |
origin: `http://localhost:${server.address().port}` |
| 620 |
}) |
| 621 |
|
| 622 |
t.equal(response2.statusCode, 200) |
| 623 |
t.equal(await response2.body.text(), 'hello world!') |
| 624 |
}) |
| 625 |
|
| 626 |
test('Should we handle TTL (4)', async t => { |
| 627 |
t = tspl(t, { plan: 10 }) |
| 628 |
|
| 629 |
const clock = FakeTimers.install() |
| 630 |
let counter = 0 |
| 631 |
let lookupCounter = 0 |
| 632 |
const server = createServer({ joinDuplicateHeaders: true }) |
| 633 |
const requestOptions = { |
| 634 |
method: 'GET', |
| 635 |
path: '/', |
| 636 |
headers: { |
| 637 |
'content-type': 'application/json' |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
server.on('request', (req, res) => { |
| 642 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 643 |
res.end('hello world!') |
| 644 |
}) |
| 645 |
|
| 646 |
server.listen(0, '127.0.0.1') |
| 647 |
|
| 648 |
await once(server, 'listening') |
| 649 |
|
| 650 |
const client = new Agent().compose([ |
| 651 |
dispatch => { |
| 652 |
return (opts, handler) => { |
| 653 |
++counter |
| 654 |
const url = new URL(opts.origin) |
| 655 |
|
| 656 |
switch (counter) { |
| 657 |
case 1: |
| 658 |
t.equal(isIP(url.hostname), 4) |
| 659 |
break |
| 660 |
|
| 661 |
case 2: |
| 662 |
t.equal(isIP(url.hostname), 4) |
| 663 |
break |
| 664 |
|
| 665 |
case 3: |
| 666 |
t.equal(isIP(url.hostname), 4) |
| 667 |
break |
| 668 |
default: |
| 669 |
t.fail('should not reach this point') |
| 670 |
} |
| 671 |
|
| 672 |
return dispatch(opts, handler) |
| 673 |
} |
| 674 |
}, |
| 675 |
dns({ |
| 676 |
dualStack: false, |
| 677 |
affinity: 4, |
| 678 |
maxTTL: 400, |
| 679 |
lookup: (origin, opts, cb) => { |
| 680 |
++lookupCounter |
| 681 |
lookup( |
| 682 |
origin.hostname, |
| 683 |
{ all: true, family: opts.affinity }, |
| 684 |
cb |
| 685 |
) |
| 686 |
} |
| 687 |
}) |
| 688 |
]) |
| 689 |
|
| 690 |
after(async () => { |
| 691 |
clock.uninstall() |
| 692 |
await client.close() |
| 693 |
server.close() |
| 694 |
|
| 695 |
await once(server, 'close') |
| 696 |
}) |
| 697 |
|
| 698 |
const response = await client.request({ |
| 699 |
...requestOptions, |
| 700 |
origin: `http://localhost:${server.address().port}` |
| 701 |
}) |
| 702 |
|
| 703 |
t.equal(response.statusCode, 200) |
| 704 |
t.equal(await response.body.text(), 'hello world!') |
| 705 |
|
| 706 |
clock.tick(200) |
| 707 |
|
| 708 |
const response2 = await client.request({ |
| 709 |
...requestOptions, |
| 710 |
origin: `http://localhost:${server.address().port}` |
| 711 |
}) |
| 712 |
|
| 713 |
t.equal(response2.statusCode, 200) |
| 714 |
t.equal(await response2.body.text(), 'hello world!') |
| 715 |
|
| 716 |
clock.tick(300) |
| 717 |
|
| 718 |
const response3 = await client.request({ |
| 719 |
...requestOptions, |
| 720 |
origin: `http://localhost:${server.address().port}` |
| 721 |
}) |
| 722 |
|
| 723 |
t.equal(response3.statusCode, 200) |
| 724 |
t.equal(await response3.body.text(), 'hello world!') |
| 725 |
|
| 726 |
t.equal(lookupCounter, 2) |
| 727 |
}) |
| 728 |
|
| 729 |
test('Should we handle TTL (6)', { skip: !ipv6Available }, async t => { |
| 730 |
t = tspl(t, { plan: 10 }) |
| 731 |
|
| 732 |
const clock = FakeTimers.install() |
| 733 |
let counter = 0 |
| 734 |
let lookupCounter = 0 |
| 735 |
const server = createServer({ joinDuplicateHeaders: true }) |
| 736 |
const requestOptions = { |
| 737 |
method: 'GET', |
| 738 |
path: '/', |
| 739 |
headers: { |
| 740 |
'content-type': 'application/json' |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
server.on('request', (req, res) => { |
| 745 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 746 |
res.end('hello world!') |
| 747 |
}) |
| 748 |
|
| 749 |
server.listen(0, '::1') |
| 750 |
|
| 751 |
await once(server, 'listening') |
| 752 |
|
| 753 |
const client = new Agent().compose([ |
| 754 |
dispatch => { |
| 755 |
return (opts, handler) => { |
| 756 |
++counter |
| 757 |
const url = new URL(opts.origin) |
| 758 |
|
| 759 |
switch (counter) { |
| 760 |
case 1: |
| 761 |
// [::1] -> ::1 |
| 762 |
t.equal(isIP(url.hostname.slice(1, 4)), 6) |
| 763 |
break |
| 764 |
|
| 765 |
case 2: |
| 766 |
// [::1] -> ::1 |
| 767 |
t.equal(isIP(url.hostname.slice(1, 4)), 6) |
| 768 |
break |
| 769 |
|
| 770 |
case 3: |
| 771 |
// [::1] -> ::1 |
| 772 |
t.equal(isIP(url.hostname.slice(1, 4)), 6) |
| 773 |
break |
| 774 |
default: |
| 775 |
t.fail('should not reach this point') |
| 776 |
} |
| 777 |
|
| 778 |
return dispatch(opts, handler) |
| 779 |
} |
| 780 |
}, |
| 781 |
dns({ |
| 782 |
dualStack: false, |
| 783 |
affinity: 6, |
| 784 |
maxTTL: 400, |
| 785 |
lookup: (origin, opts, cb) => { |
| 786 |
++lookupCounter |
| 787 |
lookup( |
| 788 |
origin.hostname, |
| 789 |
{ all: true, family: opts.affinity }, |
| 790 |
cb |
| 791 |
) |
| 792 |
} |
| 793 |
}) |
| 794 |
]) |
| 795 |
|
| 796 |
after(async () => { |
| 797 |
clock.uninstall() |
| 798 |
await client.close() |
| 799 |
server.close() |
| 800 |
|
| 801 |
await once(server, 'close') |
| 802 |
}) |
| 803 |
|
| 804 |
const response = await client.request({ |
| 805 |
...requestOptions, |
| 806 |
origin: `http://localhost:${server.address().port}` |
| 807 |
}) |
| 808 |
|
| 809 |
t.equal(response.statusCode, 200) |
| 810 |
t.equal(await response.body.text(), 'hello world!') |
| 811 |
|
| 812 |
clock.tick(200) |
| 813 |
|
| 814 |
const response2 = await client.request({ |
| 815 |
...requestOptions, |
| 816 |
origin: `http://localhost:${server.address().port}` |
| 817 |
}) |
| 818 |
|
| 819 |
t.equal(response2.statusCode, 200) |
| 820 |
t.equal(await response2.body.text(), 'hello world!') |
| 821 |
|
| 822 |
clock.tick(300) |
| 823 |
|
| 824 |
const response3 = await client.request({ |
| 825 |
...requestOptions, |
| 826 |
origin: `http://localhost:${server.address().port}` |
| 827 |
}) |
| 828 |
|
| 829 |
t.equal(response3.statusCode, 200) |
| 830 |
t.equal(await response3.body.text(), 'hello world!') |
| 831 |
t.equal(lookupCounter, 2) |
| 832 |
}) |
| 833 |
|
| 834 |
test('Should set lowest TTL between resolved and option maxTTL', async t => { |
| 835 |
t = tspl(t, { plan: 9 }) |
| 836 |
|
| 837 |
const clock = FakeTimers.install() |
| 838 |
let lookupCounter = 0 |
| 839 |
const server = createServer({ joinDuplicateHeaders: true }) |
| 840 |
const requestOptions = { |
| 841 |
method: 'GET', |
| 842 |
path: '/', |
| 843 |
headers: { |
| 844 |
'content-type': 'application/json' |
| 845 |
} |
| 846 |
} |
| 847 |
|
| 848 |
server.on('request', (req, res) => { |
| 849 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 850 |
res.end('hello world!') |
| 851 |
}) |
| 852 |
|
| 853 |
server.listen(0, '127.0.0.1') |
| 854 |
|
| 855 |
await once(server, 'listening') |
| 856 |
|
| 857 |
const client = new Agent().compose( |
| 858 |
dns({ |
| 859 |
dualStack: false, |
| 860 |
affinity: 4, |
| 861 |
maxTTL: 200, |
| 862 |
lookup: (origin, opts, cb) => { |
| 863 |
++lookupCounter |
| 864 |
cb(null, [ |
| 865 |
{ |
| 866 |
address: '127.0.0.1', |
| 867 |
family: 4, |
| 868 |
ttl: lookupCounter === 1 ? 50 : 500 |
| 869 |
} |
| 870 |
]) |
| 871 |
} |
| 872 |
}) |
| 873 |
) |
| 874 |
|
| 875 |
after(async () => { |
| 876 |
clock.uninstall() |
| 877 |
await client.close() |
| 878 |
server.close() |
| 879 |
|
| 880 |
await once(server, 'close') |
| 881 |
}) |
| 882 |
|
| 883 |
const response = await client.request({ |
| 884 |
...requestOptions, |
| 885 |
origin: `http://localhost:${server.address().port}` |
| 886 |
}) |
| 887 |
|
| 888 |
t.equal(response.statusCode, 200) |
| 889 |
t.equal(await response.body.text(), 'hello world!') |
| 890 |
|
| 891 |
clock.tick(100) |
| 892 |
|
| 893 |
// 100ms: lookup since ttl = Math.min(50, maxTTL: 200) |
| 894 |
const response2 = await client.request({ |
| 895 |
...requestOptions, |
| 896 |
origin: `http://localhost:${server.address().port}` |
| 897 |
}) |
| 898 |
|
| 899 |
t.equal(response2.statusCode, 200) |
| 900 |
t.equal(await response2.body.text(), 'hello world!') |
| 901 |
|
| 902 |
clock.tick(100) |
| 903 |
|
| 904 |
// 100ms: cached since ttl = Math.min(500, maxTTL: 200) |
| 905 |
const response3 = await client.request({ |
| 906 |
...requestOptions, |
| 907 |
origin: `http://localhost:${server.address().port}` |
| 908 |
}) |
| 909 |
|
| 910 |
t.equal(response3.statusCode, 200) |
| 911 |
t.equal(await response3.body.text(), 'hello world!') |
| 912 |
|
| 913 |
clock.tick(150) |
| 914 |
|
| 915 |
// 250ms: lookup since ttl = Math.min(500, maxTTL: 200) |
| 916 |
const response4 = await client.request({ |
| 917 |
...requestOptions, |
| 918 |
origin: `http://localhost:${server.address().port}` |
| 919 |
}) |
| 920 |
|
| 921 |
t.equal(response4.statusCode, 200) |
| 922 |
t.equal(await response4.body.text(), 'hello world!') |
| 923 |
|
| 924 |
t.equal(lookupCounter, 3) |
| 925 |
}) |
| 926 |
|
| 927 |
test('Should use all dns entries (dual stack)', async t => { |
| 928 |
t = tspl(t, { plan: 16 }) |
| 929 |
|
| 930 |
let counter = 0 |
| 931 |
let lookupCounter = 0 |
| 932 |
const server = createServer({ joinDuplicateHeaders: true }) |
| 933 |
const requestOptions = { |
| 934 |
method: 'GET', |
| 935 |
path: '/', |
| 936 |
headers: { |
| 937 |
'content-type': 'application/json' |
| 938 |
} |
| 939 |
} |
| 940 |
|
| 941 |
server.on('request', (req, res) => { |
| 942 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 943 |
res.end('hello world!') |
| 944 |
}) |
| 945 |
|
| 946 |
server.listen(0) |
| 947 |
|
| 948 |
await once(server, 'listening') |
| 949 |
|
| 950 |
const client = new Agent().compose([ |
| 951 |
dispatch => { |
| 952 |
return (opts, handler) => { |
| 953 |
++counter |
| 954 |
const url = new URL(opts.origin) |
| 955 |
switch (counter) { |
| 956 |
case 1: |
| 957 |
t.equal(url.hostname, '1.1.1.1') |
| 958 |
break |
| 959 |
|
| 960 |
case 2: |
| 961 |
t.equal(url.hostname, '[::1]') |
| 962 |
break |
| 963 |
|
| 964 |
case 3: |
| 965 |
t.equal(url.hostname, '2.2.2.2') |
| 966 |
break |
| 967 |
|
| 968 |
case 4: |
| 969 |
t.equal(url.hostname, '[::2]') |
| 970 |
break |
| 971 |
|
| 972 |
case 5: |
| 973 |
t.equal(url.hostname, '1.1.1.1') |
| 974 |
break |
| 975 |
default: |
| 976 |
t.fail('should not reach this point') |
| 977 |
} |
| 978 |
|
| 979 |
url.hostname = '127.0.0.1' |
| 980 |
opts.origin = url.toString() |
| 981 |
return dispatch(opts, handler) |
| 982 |
} |
| 983 |
}, |
| 984 |
dns({ |
| 985 |
lookup (origin, opts, cb) { |
| 986 |
lookupCounter++ |
| 987 |
cb(null, [ |
| 988 |
{ address: '::1', family: 6 }, |
| 989 |
{ address: '::2', family: 6 }, |
| 990 |
{ address: '1.1.1.1', family: 4 }, |
| 991 |
{ address: '2.2.2.2', family: 4 } |
| 992 |
]) |
| 993 |
} |
| 994 |
}) |
| 995 |
]) |
| 996 |
|
| 997 |
after(async () => { |
| 998 |
await client.close() |
| 999 |
server.close() |
| 1000 |
|
| 1001 |
await once(server, 'close') |
| 1002 |
}) |
| 1003 |
|
| 1004 |
for (let i = 0; i < 5; i++) { |
| 1005 |
const response = await client.request({ |
| 1006 |
...requestOptions, |
| 1007 |
origin: `http://localhost:${server.address().port}` |
| 1008 |
}) |
| 1009 |
|
| 1010 |
t.equal(response.statusCode, 200) |
| 1011 |
t.equal(await response.body.text(), 'hello world!') |
| 1012 |
} |
| 1013 |
|
| 1014 |
t.equal(lookupCounter, 1) |
| 1015 |
}) |
| 1016 |
|
| 1017 |
test('Should use all dns entries (dual stack disabled - 4)', async t => { |
| 1018 |
t = tspl(t, { plan: 10 }) |
| 1019 |
|
| 1020 |
let counter = 0 |
| 1021 |
let lookupCounter = 0 |
| 1022 |
const server = createServer({ joinDuplicateHeaders: true }) |
| 1023 |
const requestOptions = { |
| 1024 |
method: 'GET', |
| 1025 |
path: '/', |
| 1026 |
headers: { |
| 1027 |
'content-type': 'application/json' |
| 1028 |
} |
| 1029 |
} |
| 1030 |
|
| 1031 |
server.on('request', (req, res) => { |
| 1032 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 1033 |
res.end('hello world!') |
| 1034 |
}) |
| 1035 |
|
| 1036 |
server.listen(0) |
| 1037 |
|
| 1038 |
await once(server, 'listening') |
| 1039 |
|
| 1040 |
const client = new Agent().compose([ |
| 1041 |
dispatch => { |
| 1042 |
return (opts, handler) => { |
| 1043 |
++counter |
| 1044 |
const url = new URL(opts.origin) |
| 1045 |
|
| 1046 |
switch (counter) { |
| 1047 |
case 1: |
| 1048 |
t.equal(url.hostname, '1.1.1.1') |
| 1049 |
break |
| 1050 |
|
| 1051 |
case 2: |
| 1052 |
t.equal(url.hostname, '2.2.2.2') |
| 1053 |
break |
| 1054 |
|
| 1055 |
case 3: |
| 1056 |
t.equal(url.hostname, '1.1.1.1') |
| 1057 |
break |
| 1058 |
default: |
| 1059 |
t.fail('should not reach this point') |
| 1060 |
} |
| 1061 |
|
| 1062 |
url.hostname = '127.0.0.1' |
| 1063 |
opts.origin = url.toString() |
| 1064 |
return dispatch(opts, handler) |
| 1065 |
} |
| 1066 |
}, |
| 1067 |
dns({ |
| 1068 |
dualStack: false, |
| 1069 |
lookup (origin, opts, cb) { |
| 1070 |
lookupCounter++ |
| 1071 |
cb(null, [ |
| 1072 |
{ address: '1.1.1.1', family: 4 }, |
| 1073 |
{ address: '2.2.2.2', family: 4 } |
| 1074 |
]) |
| 1075 |
} |
| 1076 |
}) |
| 1077 |
]) |
| 1078 |
|
| 1079 |
after(async () => { |
| 1080 |
await client.close() |
| 1081 |
server.close() |
| 1082 |
|
| 1083 |
await once(server, 'close') |
| 1084 |
}) |
| 1085 |
|
| 1086 |
const response1 = await client.request({ |
| 1087 |
...requestOptions, |
| 1088 |
origin: `http://localhost:${server.address().port}` |
| 1089 |
}) |
| 1090 |
|
| 1091 |
t.equal(response1.statusCode, 200) |
| 1092 |
t.equal(await response1.body.text(), 'hello world!') |
| 1093 |
|
| 1094 |
const response2 = await client.request({ |
| 1095 |
...requestOptions, |
| 1096 |
origin: `http://localhost:${server.address().port}` |
| 1097 |
}) |
| 1098 |
|
| 1099 |
t.equal(response2.statusCode, 200) |
| 1100 |
t.equal(await response2.body.text(), 'hello world!') |
| 1101 |
|
| 1102 |
const response3 = await client.request({ |
| 1103 |
...requestOptions, |
| 1104 |
origin: `http://localhost:${server.address().port}` |
| 1105 |
}) |
| 1106 |
|
| 1107 |
t.equal(response3.statusCode, 200) |
| 1108 |
t.equal(await response3.body.text(), 'hello world!') |
| 1109 |
|
| 1110 |
t.equal(lookupCounter, 1) |
| 1111 |
}) |
| 1112 |
|
| 1113 |
test('Should use all dns entries (dual stack disabled - 6)', async t => { |
| 1114 |
t = tspl(t, { plan: 10 }) |
| 1115 |
|
| 1116 |
let counter = 0 |
| 1117 |
let lookupCounter = 0 |
| 1118 |
const server = createServer({ joinDuplicateHeaders: true }) |
| 1119 |
const requestOptions = { |
| 1120 |
method: 'GET', |
| 1121 |
path: '/', |
| 1122 |
headers: { |
| 1123 |
'content-type': 'application/json' |
| 1124 |
} |
| 1125 |
} |
| 1126 |
|
| 1127 |
server.on('request', (req, res) => { |
| 1128 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 1129 |
res.end('hello world!') |
| 1130 |
}) |
| 1131 |
|
| 1132 |
server.listen(0) |
| 1133 |
|
| 1134 |
await once(server, 'listening') |
| 1135 |
|
| 1136 |
const client = new Agent().compose([ |
| 1137 |
dispatch => { |
| 1138 |
return (opts, handler) => { |
| 1139 |
++counter |
| 1140 |
const url = new URL(opts.origin) |
| 1141 |
|
| 1142 |
switch (counter) { |
| 1143 |
case 1: |
| 1144 |
t.equal(url.hostname, '[::1]') |
| 1145 |
break |
| 1146 |
|
| 1147 |
case 2: |
| 1148 |
t.equal(url.hostname, '[::2]') |
| 1149 |
break |
| 1150 |
|
| 1151 |
case 3: |
| 1152 |
t.equal(url.hostname, '[::1]') |
| 1153 |
break |
| 1154 |
default: |
| 1155 |
t.fail('should not reach this point') |
| 1156 |
} |
| 1157 |
|
| 1158 |
url.hostname = '127.0.0.1' |
| 1159 |
opts.origin = url.toString() |
| 1160 |
return dispatch(opts, handler) |
| 1161 |
} |
| 1162 |
}, |
| 1163 |
dns({ |
| 1164 |
dualStack: false, |
| 1165 |
affinity: 6, |
| 1166 |
lookup (origin, opts, cb) { |
| 1167 |
lookupCounter++ |
| 1168 |
cb(null, [ |
| 1169 |
{ address: '::1', family: 6 }, |
| 1170 |
{ address: '::2', family: 6 } |
| 1171 |
]) |
| 1172 |
} |
| 1173 |
}) |
| 1174 |
]) |
| 1175 |
|
| 1176 |
after(async () => { |
| 1177 |
await client.close() |
| 1178 |
server.close() |
| 1179 |
|
| 1180 |
await once(server, 'close') |
| 1181 |
}) |
| 1182 |
|
| 1183 |
const response1 = await client.request({ |
| 1184 |
...requestOptions, |
| 1185 |
origin: `http://localhost:${server.address().port}` |
| 1186 |
}) |
| 1187 |
|
| 1188 |
t.equal(response1.statusCode, 200) |
| 1189 |
t.equal(await response1.body.text(), 'hello world!') |
| 1190 |
|
| 1191 |
const response2 = await client.request({ |
| 1192 |
...requestOptions, |
| 1193 |
origin: `http://localhost:${server.address().port}` |
| 1194 |
}) |
| 1195 |
|
| 1196 |
t.equal(response2.statusCode, 200) |
| 1197 |
t.equal(await response2.body.text(), 'hello world!') |
| 1198 |
|
| 1199 |
const response3 = await client.request({ |
| 1200 |
...requestOptions, |
| 1201 |
origin: `http://localhost:${server.address().port}` |
| 1202 |
}) |
| 1203 |
|
| 1204 |
t.equal(response3.statusCode, 200) |
| 1205 |
t.equal(await response3.body.text(), 'hello world!') |
| 1206 |
|
| 1207 |
t.equal(lookupCounter, 1) |
| 1208 |
}) |
| 1209 |
|
| 1210 |
test('Should handle single family resolved (dual stack)', async t => { |
| 1211 |
t = tspl(t, { plan: 7 }) |
| 1212 |
|
| 1213 |
const clock = FakeTimers.install() |
| 1214 |
let counter = 0 |
| 1215 |
let lookupCounter = 0 |
| 1216 |
const server = createServer({ joinDuplicateHeaders: true }) |
| 1217 |
const requestOptions = { |
| 1218 |
method: 'GET', |
| 1219 |
path: '/', |
| 1220 |
headers: { |
| 1221 |
'content-type': 'application/json' |
| 1222 |
} |
| 1223 |
} |
| 1224 |
|
| 1225 |
server.on('request', (req, res) => { |
| 1226 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 1227 |
res.end('hello world!') |
| 1228 |
}) |
| 1229 |
|
| 1230 |
server.listen(0) |
| 1231 |
|
| 1232 |
await once(server, 'listening') |
| 1233 |
|
| 1234 |
const client = new Agent().compose([ |
| 1235 |
dispatch => { |
| 1236 |
return (opts, handler) => { |
| 1237 |
++counter |
| 1238 |
const url = new URL(opts.origin) |
| 1239 |
|
| 1240 |
switch (counter) { |
| 1241 |
case 1: |
| 1242 |
t.equal(isIP(url.hostname), 4) |
| 1243 |
break |
| 1244 |
|
| 1245 |
case 2: |
| 1246 |
// [::1] -> ::1 |
| 1247 |
t.equal(isIP(url.hostname.slice(1, 4)), 6) |
| 1248 |
break |
| 1249 |
default: |
| 1250 |
t.fail('should not reach this point') |
| 1251 |
} |
| 1252 |
|
| 1253 |
return dispatch(opts, handler) |
| 1254 |
} |
| 1255 |
}, |
| 1256 |
dns({ |
| 1257 |
lookup (origin, opts, cb) { |
| 1258 |
lookupCounter++ |
| 1259 |
if (lookupCounter === 1) { |
| 1260 |
cb(null, [ |
| 1261 |
{ address: '127.0.0.1', family: 4, ttl: 50 } |
| 1262 |
]) |
| 1263 |
} else { |
| 1264 |
cb(null, [ |
| 1265 |
{ address: '::1', family: 6, ttl: 50 } |
| 1266 |
]) |
| 1267 |
} |
| 1268 |
} |
| 1269 |
}) |
| 1270 |
]) |
| 1271 |
|
| 1272 |
after(async () => { |
| 1273 |
clock.uninstall() |
| 1274 |
await client.close() |
| 1275 |
server.close() |
| 1276 |
|
| 1277 |
await once(server, 'close') |
| 1278 |
}) |
| 1279 |
|
| 1280 |
const response = await client.request({ |
| 1281 |
...requestOptions, |
| 1282 |
origin: `http://localhost:${server.address().port}` |
| 1283 |
}) |
| 1284 |
|
| 1285 |
t.equal(response.statusCode, 200) |
| 1286 |
t.equal(await response.body.text(), 'hello world!') |
| 1287 |
|
| 1288 |
clock.tick(100) |
| 1289 |
|
| 1290 |
const response2 = await client.request({ |
| 1291 |
...requestOptions, |
| 1292 |
origin: `http://localhost:${server.address().port}` |
| 1293 |
}) |
| 1294 |
|
| 1295 |
t.equal(response2.statusCode, 200) |
| 1296 |
t.equal(await response2.body.text(), 'hello world!') |
| 1297 |
|
| 1298 |
t.equal(lookupCounter, 2) |
| 1299 |
}) |
| 1300 |
|
| 1301 |
test('Should prefer affinity (dual stack - 4)', async t => { |
| 1302 |
t = tspl(t, { plan: 10 }) |
| 1303 |
|
| 1304 |
const clock = FakeTimers.install() |
| 1305 |
let counter = 0 |
| 1306 |
let lookupCounter = 0 |
| 1307 |
const server = createServer({ joinDuplicateHeaders: true }) |
| 1308 |
const requestOptions = { |
| 1309 |
method: 'GET', |
| 1310 |
path: '/', |
| 1311 |
headers: { |
| 1312 |
'content-type': 'application/json' |
| 1313 |
} |
| 1314 |
} |
| 1315 |
|
| 1316 |
server.on('request', (req, res) => { |
| 1317 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 1318 |
res.end('hello world!') |
| 1319 |
}) |
| 1320 |
|
| 1321 |
server.listen(0) |
| 1322 |
|
| 1323 |
await once(server, 'listening') |
| 1324 |
|
| 1325 |
const client = new Agent().compose([ |
| 1326 |
dispatch => { |
| 1327 |
return (opts, handler) => { |
| 1328 |
++counter |
| 1329 |
const url = new URL(opts.origin) |
| 1330 |
|
| 1331 |
switch (counter) { |
| 1332 |
case 1: |
| 1333 |
t.equal(url.hostname, '1.1.1.1') |
| 1334 |
break |
| 1335 |
|
| 1336 |
case 2: |
| 1337 |
t.equal(url.hostname, '2.2.2.2') |
| 1338 |
break |
| 1339 |
|
| 1340 |
case 3: |
| 1341 |
t.equal(url.hostname, '1.1.1.1') |
| 1342 |
break |
| 1343 |
default: |
| 1344 |
t.fail('should not reach this point') |
| 1345 |
} |
| 1346 |
|
| 1347 |
url.hostname = '127.0.0.1' |
| 1348 |
opts.origin = url.toString() |
| 1349 |
return dispatch(opts, handler) |
| 1350 |
} |
| 1351 |
}, |
| 1352 |
dns({ |
| 1353 |
affinity: 4, |
| 1354 |
lookup (origin, opts, cb) { |
| 1355 |
lookupCounter++ |
| 1356 |
cb(null, [ |
| 1357 |
{ address: '1.1.1.1', family: 4 }, |
| 1358 |
{ address: '2.2.2.2', family: 4 }, |
| 1359 |
{ address: '::1', family: 6 }, |
| 1360 |
{ address: '::2', family: 6 } |
| 1361 |
]) |
| 1362 |
} |
| 1363 |
}) |
| 1364 |
]) |
| 1365 |
|
| 1366 |
after(async () => { |
| 1367 |
clock.uninstall() |
| 1368 |
await client.close() |
| 1369 |
server.close() |
| 1370 |
|
| 1371 |
await once(server, 'close') |
| 1372 |
}) |
| 1373 |
|
| 1374 |
const response = await client.request({ |
| 1375 |
...requestOptions, |
| 1376 |
origin: `http://localhost:${server.address().port}` |
| 1377 |
}) |
| 1378 |
|
| 1379 |
t.equal(response.statusCode, 200) |
| 1380 |
t.equal(await response.body.text(), 'hello world!') |
| 1381 |
|
| 1382 |
clock.tick(100) |
| 1383 |
|
| 1384 |
const response2 = await client.request({ |
| 1385 |
...requestOptions, |
| 1386 |
origin: `http://localhost:${server.address().port}` |
| 1387 |
}) |
| 1388 |
|
| 1389 |
t.equal(response2.statusCode, 200) |
| 1390 |
t.equal(await response2.body.text(), 'hello world!') |
| 1391 |
|
| 1392 |
const response3 = await client.request({ |
| 1393 |
...requestOptions, |
| 1394 |
origin: `http://localhost:${server.address().port}` |
| 1395 |
}) |
| 1396 |
|
| 1397 |
t.equal(response3.statusCode, 200) |
| 1398 |
t.equal(await response3.body.text(), 'hello world!') |
| 1399 |
|
| 1400 |
t.equal(lookupCounter, 1) |
| 1401 |
}) |
| 1402 |
|
| 1403 |
test('Should prefer affinity (dual stack - 6)', async t => { |
| 1404 |
t = tspl(t, { plan: 10 }) |
| 1405 |
|
| 1406 |
const clock = FakeTimers.install() |
| 1407 |
let counter = 0 |
| 1408 |
let lookupCounter = 0 |
| 1409 |
const server = createServer({ joinDuplicateHeaders: true }) |
| 1410 |
const requestOptions = { |
| 1411 |
method: 'GET', |
| 1412 |
path: '/', |
| 1413 |
headers: { |
| 1414 |
'content-type': 'application/json' |
| 1415 |
} |
| 1416 |
} |
| 1417 |
|
| 1418 |
server.on('request', (req, res) => { |
| 1419 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 1420 |
res.end('hello world!') |
| 1421 |
}) |
| 1422 |
|
| 1423 |
server.listen(0) |
| 1424 |
|
| 1425 |
await once(server, 'listening') |
| 1426 |
|
| 1427 |
const client = new Agent().compose([ |
| 1428 |
dispatch => { |
| 1429 |
return (opts, handler) => { |
| 1430 |
++counter |
| 1431 |
const url = new URL(opts.origin) |
| 1432 |
|
| 1433 |
switch (counter) { |
| 1434 |
case 1: |
| 1435 |
t.equal(url.hostname, '[::1]') |
| 1436 |
break |
| 1437 |
|
| 1438 |
case 2: |
| 1439 |
t.equal(url.hostname, '[::2]') |
| 1440 |
break |
| 1441 |
|
| 1442 |
case 3: |
| 1443 |
t.equal(url.hostname, '[::1]') |
| 1444 |
break |
| 1445 |
default: |
| 1446 |
t.fail('should not reach this point') |
| 1447 |
} |
| 1448 |
|
| 1449 |
url.hostname = '127.0.0.1' |
| 1450 |
opts.origin = url.toString() |
| 1451 |
return dispatch(opts, handler) |
| 1452 |
} |
| 1453 |
}, |
| 1454 |
dns({ |
| 1455 |
affinity: 6, |
| 1456 |
lookup (origin, opts, cb) { |
| 1457 |
lookupCounter++ |
| 1458 |
cb(null, [ |
| 1459 |
{ address: '1.1.1.1', family: 4 }, |
| 1460 |
{ address: '2.2.2.2', family: 4 }, |
| 1461 |
{ address: '::1', family: 6 }, |
| 1462 |
{ address: '::2', family: 6 } |
| 1463 |
]) |
| 1464 |
} |
| 1465 |
}) |
| 1466 |
]) |
| 1467 |
|
| 1468 |
after(async () => { |
| 1469 |
clock.uninstall() |
| 1470 |
await client.close() |
| 1471 |
server.close() |
| 1472 |
|
| 1473 |
await once(server, 'close') |
| 1474 |
}) |
| 1475 |
|
| 1476 |
const response = await client.request({ |
| 1477 |
...requestOptions, |
| 1478 |
origin: `http://localhost:${server.address().port}` |
| 1479 |
}) |
| 1480 |
|
| 1481 |
t.equal(response.statusCode, 200) |
| 1482 |
t.equal(await response.body.text(), 'hello world!') |
| 1483 |
|
| 1484 |
clock.tick(100) |
| 1485 |
|
| 1486 |
const response2 = await client.request({ |
| 1487 |
...requestOptions, |
| 1488 |
origin: `http://localhost:${server.address().port}` |
| 1489 |
}) |
| 1490 |
|
| 1491 |
t.equal(response2.statusCode, 200) |
| 1492 |
t.equal(await response2.body.text(), 'hello world!') |
| 1493 |
|
| 1494 |
const response3 = await client.request({ |
| 1495 |
...requestOptions, |
| 1496 |
origin: `http://localhost:${server.address().port}` |
| 1497 |
}) |
| 1498 |
|
| 1499 |
t.equal(response3.statusCode, 200) |
| 1500 |
t.equal(await response3.body.text(), 'hello world!') |
| 1501 |
|
| 1502 |
t.equal(lookupCounter, 1) |
| 1503 |
}) |
| 1504 |
|
| 1505 |
test('Should use resolved ports (4)', async t => { |
| 1506 |
t = tspl(t, { plan: 5 }) |
| 1507 |
|
| 1508 |
let lookupCounter = 0 |
| 1509 |
const server1 = createServer({ joinDuplicateHeaders: true }) |
| 1510 |
const server2 = createServer({ joinDuplicateHeaders: true }) |
| 1511 |
const requestOptions = { |
| 1512 |
method: 'GET', |
| 1513 |
path: '/', |
| 1514 |
headers: { |
| 1515 |
'content-type': 'application/json' |
| 1516 |
} |
| 1517 |
} |
| 1518 |
|
| 1519 |
server1.on('request', (req, res) => { |
| 1520 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 1521 |
res.end('hello world!') |
| 1522 |
}) |
| 1523 |
|
| 1524 |
server1.listen(0) |
| 1525 |
|
| 1526 |
server2.on('request', (req, res) => { |
| 1527 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 1528 |
res.end('hello world! (x2)') |
| 1529 |
}) |
| 1530 |
server2.listen(0) |
| 1531 |
|
| 1532 |
await Promise.all([once(server1, 'listening'), once(server2, 'listening')]) |
| 1533 |
|
| 1534 |
const client = new Agent().compose([ |
| 1535 |
dns({ |
| 1536 |
lookup (origin, opts, cb) { |
| 1537 |
lookupCounter++ |
| 1538 |
cb(null, [ |
| 1539 |
{ address: '127.0.0.1', family: 4, port: server1.address().port }, |
| 1540 |
{ address: '127.0.0.1', family: 4, port: server2.address().port } |
| 1541 |
]) |
| 1542 |
} |
| 1543 |
}) |
| 1544 |
]) |
| 1545 |
|
| 1546 |
after(async () => { |
| 1547 |
await client.close() |
| 1548 |
server1.close() |
| 1549 |
server2.close() |
| 1550 |
|
| 1551 |
await Promise.all([once(server1, 'close'), once(server2, 'close')]) |
| 1552 |
}) |
| 1553 |
|
| 1554 |
const response = await client.request({ |
| 1555 |
...requestOptions, |
| 1556 |
origin: 'http://localhost' |
| 1557 |
}) |
| 1558 |
|
| 1559 |
t.equal(response.statusCode, 200) |
| 1560 |
t.equal(await response.body.text(), 'hello world!') |
| 1561 |
|
| 1562 |
const response2 = await client.request({ |
| 1563 |
...requestOptions, |
| 1564 |
origin: 'http://localhost' |
| 1565 |
}) |
| 1566 |
|
| 1567 |
t.equal(response2.statusCode, 200) |
| 1568 |
t.equal(await response2.body.text(), 'hello world! (x2)') |
| 1569 |
|
| 1570 |
t.equal(lookupCounter, 1) |
| 1571 |
}) |
| 1572 |
|
| 1573 |
test('Should use resolved ports (6)', async t => { |
| 1574 |
t = tspl(t, { plan: 5 }) |
| 1575 |
|
| 1576 |
let lookupCounter = 0 |
| 1577 |
const server1 = createServer({ joinDuplicateHeaders: true }) |
| 1578 |
const server2 = createServer({ joinDuplicateHeaders: true }) |
| 1579 |
const requestOptions = { |
| 1580 |
method: 'GET', |
| 1581 |
path: '/', |
| 1582 |
headers: { |
| 1583 |
'content-type': 'application/json' |
| 1584 |
} |
| 1585 |
} |
| 1586 |
|
| 1587 |
server1.on('request', (req, res) => { |
| 1588 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 1589 |
res.end('hello world!') |
| 1590 |
}) |
| 1591 |
|
| 1592 |
server1.listen(0, '::1') |
| 1593 |
|
| 1594 |
server2.on('request', (req, res) => { |
| 1595 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 1596 |
res.end('hello world! (x2)') |
| 1597 |
}) |
| 1598 |
server2.listen(0, '::1') |
| 1599 |
|
| 1600 |
await Promise.all([once(server1, 'listening'), once(server2, 'listening')]) |
| 1601 |
|
| 1602 |
const client = new Agent().compose([ |
| 1603 |
dns({ |
| 1604 |
lookup (origin, opts, cb) { |
| 1605 |
lookupCounter++ |
| 1606 |
cb(null, [ |
| 1607 |
{ address: '::1', family: 6, port: server1.address().port }, |
| 1608 |
{ address: '::1', family: 6, port: server2.address().port } |
| 1609 |
]) |
| 1610 |
} |
| 1611 |
}) |
| 1612 |
]) |
| 1613 |
|
| 1614 |
after(async () => { |
| 1615 |
await client.close() |
| 1616 |
server1.close() |
| 1617 |
server2.close() |
| 1618 |
|
| 1619 |
await Promise.all([once(server1, 'close'), once(server2, 'close')]) |
| 1620 |
}) |
| 1621 |
|
| 1622 |
const response = await client.request({ |
| 1623 |
...requestOptions, |
| 1624 |
origin: 'http://localhost' |
| 1625 |
}) |
| 1626 |
|
| 1627 |
t.equal(response.statusCode, 200) |
| 1628 |
t.equal(await response.body.text(), 'hello world!') |
| 1629 |
|
| 1630 |
const response2 = await client.request({ |
| 1631 |
...requestOptions, |
| 1632 |
origin: 'http://localhost' |
| 1633 |
}) |
| 1634 |
|
| 1635 |
t.equal(response2.statusCode, 200) |
| 1636 |
t.equal(await response2.body.text(), 'hello world! (x2)') |
| 1637 |
|
| 1638 |
t.equal(lookupCounter, 1) |
| 1639 |
}) |
| 1640 |
|
| 1641 |
test('Should handle max cached items', async t => { |
| 1642 |
t = tspl(t, { plan: 9 }) |
| 1643 |
|
| 1644 |
let counter = 0 |
| 1645 |
const server1 = createServer({ joinDuplicateHeaders: true }) |
| 1646 |
const server2 = createServer({ joinDuplicateHeaders: true }) |
| 1647 |
const requestOptions = { |
| 1648 |
method: 'GET', |
| 1649 |
path: '/', |
| 1650 |
headers: { |
| 1651 |
'content-type': 'application/json' |
| 1652 |
} |
| 1653 |
} |
| 1654 |
|
| 1655 |
server1.on('request', (req, res) => { |
| 1656 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 1657 |
res.end('hello world!') |
| 1658 |
}) |
| 1659 |
|
| 1660 |
server1.listen(0) |
| 1661 |
|
| 1662 |
server2.on('request', (req, res) => { |
| 1663 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 1664 |
res.end('hello world! (x2)') |
| 1665 |
}) |
| 1666 |
server2.listen(0) |
| 1667 |
|
| 1668 |
await Promise.all([once(server1, 'listening'), once(server2, 'listening')]) |
| 1669 |
|
| 1670 |
const client = new Agent().compose([ |
| 1671 |
dispatch => { |
| 1672 |
return (opts, handler) => { |
| 1673 |
++counter |
| 1674 |
const url = new URL(opts.origin) |
| 1675 |
|
| 1676 |
switch (counter) { |
| 1677 |
case 1: |
| 1678 |
t.equal(isIP(url.hostname), 4) |
| 1679 |
break |
| 1680 |
|
| 1681 |
case 2: |
| 1682 |
// [::1] -> ::1 |
| 1683 |
t.equal(isIP(url.hostname.slice(1, 4)), 6) |
| 1684 |
break |
| 1685 |
|
| 1686 |
case 3: |
| 1687 |
t.equal(url.hostname, 'developer.mozilla.org') |
| 1688 |
// Rewrite origin to avoid reaching internet |
| 1689 |
opts.origin = `http://127.0.0.1:${server2.address().port}` |
| 1690 |
break |
| 1691 |
default: |
| 1692 |
t.fails('should not reach this point') |
| 1693 |
} |
| 1694 |
|
| 1695 |
return dispatch(opts, handler) |
| 1696 |
} |
| 1697 |
}, |
| 1698 |
dns({ |
| 1699 |
maxItems: 1, |
| 1700 |
lookup: (_origin, _opts, cb) => { |
| 1701 |
cb(null, [ |
| 1702 |
{ |
| 1703 |
address: '::1', |
| 1704 |
family: 6 |
| 1705 |
}, |
| 1706 |
{ |
| 1707 |
address: '127.0.0.1', |
| 1708 |
family: 4 |
| 1709 |
} |
| 1710 |
]) |
| 1711 |
} |
| 1712 |
}) |
| 1713 |
]) |
| 1714 |
|
| 1715 |
after(async () => { |
| 1716 |
await client.close() |
| 1717 |
server1.close() |
| 1718 |
server2.close() |
| 1719 |
|
| 1720 |
await Promise.all([once(server1, 'close'), once(server2, 'close')]) |
| 1721 |
}) |
| 1722 |
|
| 1723 |
const response = await client.request({ |
| 1724 |
...requestOptions, |
| 1725 |
origin: `http://localhost:${server1.address().port}` |
| 1726 |
}) |
| 1727 |
|
| 1728 |
t.equal(response.statusCode, 200) |
| 1729 |
t.equal(await response.body.text(), 'hello world!') |
| 1730 |
|
| 1731 |
const response2 = await client.request({ |
| 1732 |
...requestOptions, |
| 1733 |
origin: `http://localhost:${server1.address().port}` |
| 1734 |
}) |
| 1735 |
|
| 1736 |
t.equal(response2.statusCode, 200) |
| 1737 |
t.equal(await response2.body.text(), 'hello world!') |
| 1738 |
|
| 1739 |
const response3 = await client.request({ |
| 1740 |
...requestOptions, |
| 1741 |
origin: 'https://developer.mozilla.org' |
| 1742 |
}) |
| 1743 |
|
| 1744 |
t.equal(response3.statusCode, 200) |
| 1745 |
t.equal(await response3.body.text(), 'hello world! (x2)') |
| 1746 |
}) |
| 1747 |
|
| 1748 |
test('Should support external storage', async t => { |
| 1749 |
t = tspl(t, { plan: 9 }) |
| 1750 |
|
| 1751 |
let counter = 0 |
| 1752 |
const server1 = createServer({ joinDuplicateHeaders: true }) |
| 1753 |
const server2 = createServer({ joinDuplicateHeaders: true }) |
| 1754 |
const requestOptions = { |
| 1755 |
method: 'GET', |
| 1756 |
path: '/', |
| 1757 |
headers: { |
| 1758 |
'content-type': 'application/json' |
| 1759 |
} |
| 1760 |
} |
| 1761 |
|
| 1762 |
server1.on('request', (req, res) => { |
| 1763 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 1764 |
res.end('hello world!') |
| 1765 |
}) |
| 1766 |
|
| 1767 |
server1.listen(0) |
| 1768 |
|
| 1769 |
server2.on('request', (req, res) => { |
| 1770 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 1771 |
res.end('hello world! (x2)') |
| 1772 |
}) |
| 1773 |
server2.listen(0) |
| 1774 |
|
| 1775 |
await Promise.all([once(server1, 'listening'), once(server2, 'listening')]) |
| 1776 |
|
| 1777 |
const cache = new Map() |
| 1778 |
const storage = { |
| 1779 |
get (origin) { |
| 1780 |
return cache.get(origin) |
| 1781 |
}, |
| 1782 |
set (origin, records) { |
| 1783 |
cache.set(origin, records) |
| 1784 |
}, |
| 1785 |
delete (origin) { |
| 1786 |
cache.delete(origin) |
| 1787 |
}, |
| 1788 |
// simulate internal DNSStorage behaviour with `maxItems: 1` parameter |
| 1789 |
full () { |
| 1790 |
return cache.size === 1 |
| 1791 |
} |
| 1792 |
} |
| 1793 |
|
| 1794 |
const client = new Agent().compose([ |
| 1795 |
dispatch => { |
| 1796 |
return (opts, handler) => { |
| 1797 |
++counter |
| 1798 |
const url = new URL(opts.origin) |
| 1799 |
|
| 1800 |
switch (counter) { |
| 1801 |
case 1: |
| 1802 |
t.equal(isIP(url.hostname), 4) |
| 1803 |
break |
| 1804 |
|
| 1805 |
case 2: |
| 1806 |
// [::1] -> ::1 |
| 1807 |
t.equal(isIP(url.hostname.slice(1, 4)), 6) |
| 1808 |
break |
| 1809 |
|
| 1810 |
case 3: |
| 1811 |
t.equal(url.hostname, 'developer.mozilla.org') |
| 1812 |
// Rewrite origin to avoid reaching internet |
| 1813 |
opts.origin = `http://127.0.0.1:${server2.address().port}` |
| 1814 |
break |
| 1815 |
default: |
| 1816 |
t.fails('should not reach this point') |
| 1817 |
} |
| 1818 |
|
| 1819 |
return dispatch(opts, handler) |
| 1820 |
} |
| 1821 |
}, |
| 1822 |
dns({ |
| 1823 |
storage, |
| 1824 |
lookup: (_origin, _opts, cb) => { |
| 1825 |
cb(null, [ |
| 1826 |
{ |
| 1827 |
address: '::1', |
| 1828 |
family: 6 |
| 1829 |
}, |
| 1830 |
{ |
| 1831 |
address: '127.0.0.1', |
| 1832 |
family: 4 |
| 1833 |
} |
| 1834 |
]) |
| 1835 |
} |
| 1836 |
}) |
| 1837 |
]) |
| 1838 |
|
| 1839 |
after(async () => { |
| 1840 |
await client.close() |
| 1841 |
server1.close() |
| 1842 |
server2.close() |
| 1843 |
|
| 1844 |
await Promise.all([once(server1, 'close'), once(server2, 'close')]) |
| 1845 |
}) |
| 1846 |
|
| 1847 |
const response = await client.request({ |
| 1848 |
...requestOptions, |
| 1849 |
origin: `http://localhost:${server1.address().port}` |
| 1850 |
}) |
| 1851 |
|
| 1852 |
t.equal(response.statusCode, 200) |
| 1853 |
t.equal(await response.body.text(), 'hello world!') |
| 1854 |
|
| 1855 |
const response2 = await client.request({ |
| 1856 |
...requestOptions, |
| 1857 |
origin: `http://localhost:${server1.address().port}` |
| 1858 |
}) |
| 1859 |
|
| 1860 |
t.equal(response2.statusCode, 200) |
| 1861 |
t.equal(await response2.body.text(), 'hello world!') |
| 1862 |
|
| 1863 |
const response3 = await client.request({ |
| 1864 |
...requestOptions, |
| 1865 |
origin: 'https://developer.mozilla.org' |
| 1866 |
}) |
| 1867 |
|
| 1868 |
t.equal(response3.statusCode, 200) |
| 1869 |
t.equal(await response3.body.text(), 'hello world! (x2)') |
| 1870 |
}) |
| 1871 |
|
| 1872 |
test('retry once with dual-stack', async t => { |
| 1873 |
t = tspl(t, { plan: 2 }) |
| 1874 |
|
| 1875 |
const requestOptions = { |
| 1876 |
method: 'GET', |
| 1877 |
path: '/', |
| 1878 |
headers: { |
| 1879 |
'content-type': 'application/json' |
| 1880 |
} |
| 1881 |
} |
| 1882 |
|
| 1883 |
let counter = 0 |
| 1884 |
const client = new Agent().compose([ |
| 1885 |
dispatch => { |
| 1886 |
return (opts, handler) => { |
| 1887 |
counter++ |
| 1888 |
return dispatch(opts, handler) |
| 1889 |
} |
| 1890 |
}, |
| 1891 |
dns({ |
| 1892 |
lookup: (_origin, _opts, cb) => { |
| 1893 |
cb(null, [ |
| 1894 |
{ |
| 1895 |
address: '127.0.0.1', |
| 1896 |
port: 3669, |
| 1897 |
family: 4, |
| 1898 |
ttl: 1000 |
| 1899 |
}, |
| 1900 |
{ |
| 1901 |
address: '::1', |
| 1902 |
port: 3669, |
| 1903 |
family: 6, |
| 1904 |
ttl: 1000 |
| 1905 |
} |
| 1906 |
]) |
| 1907 |
} |
| 1908 |
}) |
| 1909 |
]) |
| 1910 |
|
| 1911 |
after(async () => { |
| 1912 |
await client.close() |
| 1913 |
}) |
| 1914 |
|
| 1915 |
await t.rejects(client.request({ |
| 1916 |
...requestOptions, |
| 1917 |
origin: 'http://localhost' |
| 1918 |
}), 'ECONNREFUSED') |
| 1919 |
|
| 1920 |
t.equal(counter, 2) |
| 1921 |
}) |
| 1922 |
|
| 1923 |
test('Should handle ENOTFOUND response error', async t => { |
| 1924 |
t = tspl(t, { plan: 3 }) |
| 1925 |
let lookupCounter = 0 |
| 1926 |
|
| 1927 |
const requestOptions = { |
| 1928 |
method: 'GET', |
| 1929 |
path: '/', |
| 1930 |
origin: 'http://localhost' |
| 1931 |
} |
| 1932 |
|
| 1933 |
const client = new Agent().compose([ |
| 1934 |
dns({ |
| 1935 |
lookup (origin, opts, cb) { |
| 1936 |
lookupCounter++ |
| 1937 |
if (lookupCounter === 1) { |
| 1938 |
const err = new Error('test error') |
| 1939 |
err.code = 'ENOTFOUND' |
| 1940 |
cb(err) |
| 1941 |
} else { |
| 1942 |
// Causes InformationalError |
| 1943 |
cb(null, []) |
| 1944 |
} |
| 1945 |
} |
| 1946 |
}) |
| 1947 |
]) |
| 1948 |
|
| 1949 |
after(async () => { |
| 1950 |
await client.close() |
| 1951 |
}) |
| 1952 |
|
| 1953 |
let error1 |
| 1954 |
try { |
| 1955 |
await client.request(requestOptions) |
| 1956 |
} catch (err) { |
| 1957 |
error1 = err |
| 1958 |
} |
| 1959 |
t.equal(error1.code, 'ENOTFOUND') |
| 1960 |
|
| 1961 |
// Test that the records in the dns interceptor were deleted after the |
| 1962 |
// previous request |
| 1963 |
let error2 |
| 1964 |
try { |
| 1965 |
await client.request(requestOptions) |
| 1966 |
} catch (err) { |
| 1967 |
error2 = err |
| 1968 |
} |
| 1969 |
t.equal(error2.name, 'InformationalError') |
| 1970 |
|
| 1971 |
t.equal(lookupCounter, 2) |
| 1972 |
}) |
| 1973 |
|
| 1974 |
test('#3937 - Handle host correctly', async t => { |
| 1975 |
t = tspl(t, { plan: 10 }) |
| 1976 |
|
| 1977 |
const hostsnames = [] |
| 1978 |
const server = createServer({ joinDuplicateHeaders: true }) |
| 1979 |
const requestOptions = { |
| 1980 |
method: 'GET', |
| 1981 |
path: '/', |
| 1982 |
headers: { |
| 1983 |
'content-type': 'application/json' |
| 1984 |
} |
| 1985 |
} |
| 1986 |
|
| 1987 |
server.on('request', (req, res) => { |
| 1988 |
t.equal(req.headers.host, `localhost:${server.address().port}`) |
| 1989 |
|
| 1990 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 1991 |
res.end('hello world!') |
| 1992 |
}) |
| 1993 |
|
| 1994 |
server.listen(0) |
| 1995 |
|
| 1996 |
await once(server, 'listening') |
| 1997 |
|
| 1998 |
const client = new Agent().compose([ |
| 1999 |
dispatch => { |
| 2000 |
return (opts, handler) => { |
| 2001 |
const url = new URL(opts.origin) |
| 2002 |
|
| 2003 |
t.equal(hostsnames.includes(url.hostname), false) |
| 2004 |
|
| 2005 |
if (url.hostname[0] === '[') { |
| 2006 |
// [::1] -> ::1 |
| 2007 |
t.equal(isIP(url.hostname.slice(1, 4)), 6) |
| 2008 |
} else { |
| 2009 |
t.equal(isIP(url.hostname), 4) |
| 2010 |
} |
| 2011 |
|
| 2012 |
hostsnames.push(url.hostname) |
| 2013 |
|
| 2014 |
return dispatch(opts, handler) |
| 2015 |
} |
| 2016 |
}, |
| 2017 |
dns({ |
| 2018 |
lookup: (_origin, _opts, cb) => { |
| 2019 |
cb(null, [ |
| 2020 |
{ |
| 2021 |
address: '::1', |
| 2022 |
family: 6 |
| 2023 |
}, |
| 2024 |
{ |
| 2025 |
address: '127.0.0.1', |
| 2026 |
family: 4 |
| 2027 |
} |
| 2028 |
]) |
| 2029 |
} |
| 2030 |
}) |
| 2031 |
]) |
| 2032 |
|
| 2033 |
after(async () => { |
| 2034 |
await client.close() |
| 2035 |
server.close() |
| 2036 |
|
| 2037 |
await once(server, 'close') |
| 2038 |
}) |
| 2039 |
|
| 2040 |
const response = await client.request({ |
| 2041 |
...requestOptions, |
| 2042 |
origin: `http://localhost:${server.address().port}` |
| 2043 |
}) |
| 2044 |
|
| 2045 |
t.equal(response.statusCode, 200) |
| 2046 |
t.equal(await response.body.text(), 'hello world!') |
| 2047 |
|
| 2048 |
const response2 = await client.request({ |
| 2049 |
...requestOptions, |
| 2050 |
origin: `http://localhost:${server.address().port}` |
| 2051 |
}) |
| 2052 |
|
| 2053 |
t.equal(response2.statusCode, 200) |
| 2054 |
t.equal(await response2.body.text(), 'hello world!') |
| 2055 |
}) |
| 2056 |
|
| 2057 |
test('#4444 - Should preserve tuple-style headers', async t => { |
| 2058 |
t = tspl(t, { plan: 5 }) |
| 2059 |
|
| 2060 |
const server = createServer({ joinDuplicateHeaders: true }) |
| 2061 |
|
| 2062 |
server.on('request', (req, res) => { |
| 2063 |
t.equal(req.headers.host, `localhost:${server.address().port}`) |
| 2064 |
t.equal(req.headers.foo, 'bar') |
| 2065 |
t.equal(req.headers['0'], undefined) |
| 2066 |
|
| 2067 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 2068 |
res.end('hello world!') |
| 2069 |
}) |
| 2070 |
|
| 2071 |
server.listen(0) |
| 2072 |
|
| 2073 |
await once(server, 'listening') |
| 2074 |
|
| 2075 |
const client = new Agent().compose([ |
| 2076 |
dns({ |
| 2077 |
lookup: (_origin, _opts, cb) => { |
| 2078 |
cb(null, [ |
| 2079 |
{ |
| 2080 |
address: '::1', |
| 2081 |
family: 6 |
| 2082 |
}, |
| 2083 |
{ |
| 2084 |
address: '127.0.0.1', |
| 2085 |
family: 4 |
| 2086 |
} |
| 2087 |
]) |
| 2088 |
} |
| 2089 |
}) |
| 2090 |
]) |
| 2091 |
|
| 2092 |
after(async () => { |
| 2093 |
await client.close() |
| 2094 |
server.close() |
| 2095 |
|
| 2096 |
await once(server, 'close') |
| 2097 |
}) |
| 2098 |
|
| 2099 |
const response = await request(`http://localhost:${server.address().port}`, { |
| 2100 |
dispatcher: client, |
| 2101 |
headers: [['foo', 'bar']] |
| 2102 |
}) |
| 2103 |
|
| 2104 |
t.equal(response.statusCode, 200) |
| 2105 |
t.equal(await response.body.text(), 'hello world!') |
| 2106 |
}) |
| 2107 |
|
| 2108 |
test('#4444 - Should preserve iterable headers', async t => { |
| 2109 |
t = tspl(t, { plan: 5 }) |
| 2110 |
|
| 2111 |
const server = createServer({ joinDuplicateHeaders: true }) |
| 2112 |
|
| 2113 |
server.on('request', (req, res) => { |
| 2114 |
t.equal(req.headers.host, `localhost:${server.address().port}`) |
| 2115 |
t.equal(req.headers.foo, 'bar') |
| 2116 |
t.equal(req.headers['0'], undefined) |
| 2117 |
|
| 2118 |
res.writeHead(200, { 'content-type': 'text/plain' }) |
| 2119 |
res.end('hello world!') |
| 2120 |
}) |
| 2121 |
|
| 2122 |
server.listen(0) |
| 2123 |
|
| 2124 |
await once(server, 'listening') |
| 2125 |
|
| 2126 |
const client = new Agent().compose([ |
| 2127 |
dns({ |
| 2128 |
lookup: (_origin, _opts, cb) => { |
| 2129 |
cb(null, [ |
| 2130 |
{ |
| 2131 |
address: '::1', |
| 2132 |
family: 6 |
| 2133 |
}, |
| 2134 |
{ |
| 2135 |
address: '127.0.0.1', |
| 2136 |
family: 4 |
| 2137 |
} |
| 2138 |
]) |
| 2139 |
} |
| 2140 |
}) |
| 2141 |
]) |
| 2142 |
|
| 2143 |
after(async () => { |
| 2144 |
await client.close() |
| 2145 |
server.close() |
| 2146 |
|
| 2147 |
await once(server, 'close') |
| 2148 |
}) |
| 2149 |
|
| 2150 |
const response = await request(`http://localhost:${server.address().port}`, { |
| 2151 |
dispatcher: client, |
| 2152 |
headers: new Map([['foo', 'bar']]) |
| 2153 |
}) |
| 2154 |
|
| 2155 |
t.equal(response.statusCode, 200) |
| 2156 |
t.equal(await response.body.text(), 'hello world!') |
| 2157 |
}) |
| 2158 |
|
| 2159 |
test('#3951 - Should handle lookup errors correctly', async t => { |
| 2160 |
const suite = tspl(t, { plan: 1 }) |
| 2161 |
|
| 2162 |
const requestOptions = { |
| 2163 |
method: 'GET', |
| 2164 |
path: '/', |
| 2165 |
headers: { |
| 2166 |
'content-type': 'application/json' |
| 2167 |
} |
| 2168 |
} |
| 2169 |
|
| 2170 |
const client = new Agent().compose([ |
| 2171 |
dns({ |
| 2172 |
lookup: (_origin, _opts, cb) => { |
| 2173 |
cb(new Error('lookup error')) |
| 2174 |
} |
| 2175 |
}) |
| 2176 |
]) |
| 2177 |
|
| 2178 |
suite.rejects(client.request({ |
| 2179 |
...requestOptions, |
| 2180 |
origin: 'http://localhost' |
| 2181 |
}), new Error('lookup error')) |
| 2182 |
}) |
| 2183 |
|
| 2184 |
test('#4234 - Should pass Client requests without origin through', async t => { |
| 2185 |
t = tspl(t, { plan: 4 }) |
| 2186 |
|
| 2187 |
const server = createServer({ joinDuplicateHeaders: true }) |
| 2188 |
server.on('request', (req, res) => { |
| 2189 |
t.equal(req.headers.host, `localhost:${server.address().port}`) |
| 2190 |
res.end('hello world!') |
| 2191 |
}) |
| 2192 |
|
| 2193 |
server.listen(0) |
| 2194 |
await once(server, 'listening') |
| 2195 |
|
| 2196 |
let lookupCount = 0 |
| 2197 |
const client = new Client(`http://localhost:${server.address().port}`).compose(dns({ |
| 2198 |
lookup: (_origin, _opts, cb) => { |
| 2199 |
lookupCount++ |
| 2200 |
cb(new Error('lookup should not run')) |
| 2201 |
} |
| 2202 |
})) |
| 2203 |
|
| 2204 |
after(async () => { |
| 2205 |
await client.close() |
| 2206 |
server.close() |
| 2207 |
await once(server, 'close') |
| 2208 |
}) |
| 2209 |
|
| 2210 |
const response = await client.request({ |
| 2211 |
method: 'GET', |
| 2212 |
path: '/' |
| 2213 |
}) |
| 2214 |
|
| 2215 |
t.equal(response.statusCode, 200) |
| 2216 |
t.equal(await response.body.text(), 'hello world!') |
| 2217 |
t.equal(lookupCount, 0) |
| 2218 |
}) |
| 2219 |
|
| 2220 |
test('#4234 - Should pass Pool requests without origin through', async t => { |
| 2221 |
t = tspl(t, { plan: 4 }) |
| 2222 |
|
| 2223 |
const server = createServer({ joinDuplicateHeaders: true }) |
| 2224 |
server.on('request', (req, res) => { |
| 2225 |
t.equal(req.headers.host, `localhost:${server.address().port}`) |
| 2226 |
res.end('hello world!') |
| 2227 |
}) |
| 2228 |
|
| 2229 |
server.listen(0) |
| 2230 |
await once(server, 'listening') |
| 2231 |
|
| 2232 |
let lookupCount = 0 |
| 2233 |
const pool = new Pool(`http://localhost:${server.address().port}`).compose(dns({ |
| 2234 |
lookup: (_origin, _opts, cb) => { |
| 2235 |
lookupCount++ |
| 2236 |
cb(new Error('lookup should not run')) |
| 2237 |
} |
| 2238 |
})) |
| 2239 |
|
| 2240 |
after(async () => { |
| 2241 |
await pool.close() |
| 2242 |
server.close() |
| 2243 |
await once(server, 'close') |
| 2244 |
}) |
| 2245 |
|
| 2246 |
const response = await pool.request({ |
| 2247 |
method: 'GET', |
| 2248 |
path: '/' |
| 2249 |
}) |
| 2250 |
|
| 2251 |
t.equal(response.statusCode, 200) |
| 2252 |
t.equal(await response.body.text(), 'hello world!') |
| 2253 |
t.equal(lookupCount, 0) |
| 2254 |
}) |