Skip to content

Commit 4e789a3

Browse files
joyeecheungMylesBorins
authored andcommitted
test: add common.dns.errorLookupMock
PR-URL: #17296 Refs: nodejs/help#687 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 9d65724 commit 4e789a3

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

test/common/README.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,26 @@ called before the callback is invoked.
419419

420420
## DNS Module
421421

422-
The `DNS` module provides a naïve DNS parser/serializer.
422+
The `DNS` module provides utilities related to the `dns` built-in module.
423+
424+
### errorLookupMock(code, syscall)
425+
426+
* `code` [&lt;String>] Defaults to `dns.mockedErrorCode`.
427+
* `syscall` [&lt;String>] Defaults to `dns.mockedSysCall`.
428+
* return [&lt;Function>]
429+
430+
431+
A mock for the `lookup` option of `net.connect()` that would result in an error
432+
with the `code` and the `syscall` specified. Returns a function that has the
433+
same signature as `dns.lookup()`.
434+
435+
### mockedErrorCode
436+
437+
The default `code` of errors generated by `errorLookupMock`.
438+
439+
### mockedSysCall
440+
441+
The default `syscall` of errors generated by `errorLookupMock`.
423442

424443
### readDomainFromPacket(buffer, offset)
425444

test/common/dns.js

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/* eslint-disable required-modules */
22
'use strict';
33

4-
// Naïve DNS parser/serializer.
5-
64
const assert = require('assert');
75
const os = require('os');
86

@@ -22,6 +20,8 @@ const classes = {
2220
IN: 1
2321
};
2422

23+
// Naïve DNS parser/serializer.
24+
2525
function readDomainFromPacket(buffer, offset) {
2626
assert.ok(offset < buffer.length);
2727
const length = buffer[offset];
@@ -287,6 +287,26 @@ function writeDNSPacket(parsed) {
287287
}));
288288
}
289289

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+
290304
module.exports = {
291-
types, classes, writeDNSPacket, parseDNSPacket
305+
types,
306+
classes,
307+
writeDNSPacket,
308+
parseDNSPacket,
309+
errorLookupMock,
310+
mockedErrorCode,
311+
mockedSysCall
292312
};

0 commit comments

Comments
 (0)