test: add common.dns.errorLookupMock
PR-URL: https://github.com/nodejs/node/pull/17296 Refs: https://github.com/nodejs/help/issues/687 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
cbaf59c5b9
commit
59ed40a8a6
@ -419,7 +419,26 @@ called before the callback is invoked.
|
|||||||
|
|
||||||
## DNS Module
|
## DNS Module
|
||||||
|
|
||||||
The `DNS` module provides a naïve DNS parser/serializer.
|
The `DNS` module provides utilities related to the `dns` built-in module.
|
||||||
|
|
||||||
|
### errorLookupMock(code, syscall)
|
||||||
|
|
||||||
|
* `code` [<String>] Defaults to `dns.mockedErrorCode`.
|
||||||
|
* `syscall` [<String>] Defaults to `dns.mockedSysCall`.
|
||||||
|
* return [<Function>]
|
||||||
|
|
||||||
|
|
||||||
|
A mock for the `lookup` option of `net.connect()` that would result in an error
|
||||||
|
with the `code` and the `syscall` specified. Returns a function that has the
|
||||||
|
same signature as `dns.lookup()`.
|
||||||
|
|
||||||
|
### mockedErrorCode
|
||||||
|
|
||||||
|
The default `code` of errors generated by `errorLookupMock`.
|
||||||
|
|
||||||
|
### mockedSysCall
|
||||||
|
|
||||||
|
The default `syscall` of errors generated by `errorLookupMock`.
|
||||||
|
|
||||||
### readDomainFromPacket(buffer, offset)
|
### readDomainFromPacket(buffer, offset)
|
||||||
|
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
/* eslint-disable required-modules */
|
/* eslint-disable required-modules */
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// Naïve DNS parser/serializer.
|
|
||||||
|
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const os = require('os');
|
const os = require('os');
|
||||||
|
|
||||||
@ -22,6 +20,8 @@ const classes = {
|
|||||||
IN: 1
|
IN: 1
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Naïve DNS parser/serializer.
|
||||||
|
|
||||||
function readDomainFromPacket(buffer, offset) {
|
function readDomainFromPacket(buffer, offset) {
|
||||||
assert.ok(offset < buffer.length);
|
assert.ok(offset < buffer.length);
|
||||||
const length = buffer[offset];
|
const length = buffer[offset];
|
||||||
@ -287,6 +287,26 @@ function writeDNSPacket(parsed) {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
const mockedErrorCode = 'ENOTFOUND';
|
||||||
types, classes, writeDNSPacket, parseDNSPacket
|
const mockedSysCall = 'getaddrinfo';
|
||||||
|
|
||||||
|
function errorLookupMock(code = mockedErrorCode, syscall = mockedSysCall) {
|
||||||
|
return function lookupWithError(host, dnsopts, cb) {
|
||||||
|
const err = new Error(`${syscall} ${code} ${host}`);
|
||||||
|
err.code = code;
|
||||||
|
err.errno = code;
|
||||||
|
err.syscall = syscall;
|
||||||
|
err.hostname = host;
|
||||||
|
cb(err);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
types,
|
||||||
|
classes,
|
||||||
|
writeDNSPacket,
|
||||||
|
parseDNSPacket,
|
||||||
|
errorLookupMock,
|
||||||
|
mockedErrorCode,
|
||||||
|
mockedSysCall
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user