GeezerVison is showing github.com — test: add common.dns.errorLookupMock · nodejs/node@34af494
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: test/common/README.md +20-1Lines changed: 20 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -404,7 +404,26 @@ called before the callback is invoked. |
404 | 404 |
|
405 | 405 | ## DNS Module |
406 | 406 |
|
407 | | -The `DNS` module provides a naïve DNS parser/serializer. |
| 407 | +The `DNS` module provides utilities related to the `dns` built-in module. |
| 408 | + |
| 409 | +### errorLookupMock(code, syscall) |
| 410 | + |
| 411 | +* `code` [<String>] Defaults to `dns.mockedErrorCode`. |
| 412 | +* `syscall` [<String>] Defaults to `dns.mockedSysCall`. |
| 413 | +* return [<Function>] |
| 414 | + |
| 415 | + |
| 416 | +A mock for the `lookup` option of `net.connect()` that would result in an error |
| 417 | +with the `code` and the `syscall` specified. Returns a function that has the |
| 418 | +same signature as `dns.lookup()`. |
| 419 | + |
| 420 | +### mockedErrorCode |
| 421 | + |
| 422 | +The default `code` of errors generated by `errorLookupMock`. |
| 423 | + |
| 424 | +### mockedSysCall |
| 425 | + |
| 426 | +The default `syscall` of errors generated by `errorLookupMock`. |
408 | 427 |
|
409 | 428 | ### readDomainFromPacket(buffer, offset) |
410 | 429 |
|
|
Collapse file
Copy file name to clipboardExpand all lines: test/common/dns.js +23-3Lines changed: 23 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
1 | 1 | /* eslint-disable required-modules */ |
2 | 2 | 'use strict'; |
3 | 3 |
|
4 | | -// Naïve DNS parser/serializer. |
5 | | - |
6 | 4 | const assert = require('assert'); |
7 | 5 | const os = require('os'); |
8 | 6 |
|
@@ -22,6 +20,8 @@ const classes = { |
22 | 20 | IN: 1 |
23 | 21 | }; |
24 | 22 |
|
| 23 | +// Naïve DNS parser/serializer. |
| 24 | + |
25 | 25 | function readDomainFromPacket(buffer, offset) { |
26 | 26 | assert.ok(offset < buffer.length); |
27 | 27 | const length = buffer[offset]; |
@@ -287,6 +287,26 @@ function writeDNSPacket(parsed) { |
287 | 287 | })); |
288 | 288 | } |
289 | 289 |
|
| 290 | +const mockedErrorCode = 'ENOTFOUND'; |
| 291 | +const mockedSysCall = 'getaddrinfo'; |
| 292 | + |
| 293 | +function errorLookupMock(code = mockedErrorCode, syscall = mockedSysCall) { |
| 294 | + return function lookupWithError(host, dnsopts, cb) { |
| 295 | + const err = new Error(`${syscall} ${code} ${host}`); |
| 296 | + err.code = code; |
| 297 | + err.errno = code; |
| 298 | + err.syscall = syscall; |
| 299 | + err.hostname = host; |
| 300 | + cb(err); |
| 301 | + }; |
| 302 | +} |
| 303 | + |
290 | 304 | module.exports = { |
291 | | - types, classes, writeDNSPacket, parseDNSPacket |
| 305 | + types, |
| 306 | + classes, |
| 307 | + writeDNSPacket, |
| 308 | + parseDNSPacket, |
| 309 | + errorLookupMock, |
| 310 | + mockedErrorCode, |
| 311 | + mockedSysCall |
292 | 312 | }; |