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:
Joyee Cheung 2017-11-25 23:58:05 +09:00
parent cbaf59c5b9
commit 59ed40a8a6
No known key found for this signature in database
GPG Key ID: F586868AAD831D0C
2 changed files with 43 additions and 4 deletions

View File

@ -419,7 +419,26 @@ called before the callback is invoked.
## 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` [&lt;String>] Defaults to `dns.mockedErrorCode`.
* `syscall` [&lt;String>] Defaults to `dns.mockedSysCall`.
* return [&lt;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)

View File

@ -1,8 +1,6 @@
/* eslint-disable required-modules */
'use strict';
// Naïve DNS parser/serializer.
const assert = require('assert');
const os = require('os');
@ -22,6 +20,8 @@ const classes = {
IN: 1
};
// Naïve DNS parser/serializer.
function readDomainFromPacket(buffer, offset) {
assert.ok(offset < buffer.length);
const length = buffer[offset];
@ -287,6 +287,26 @@ function writeDNSPacket(parsed) {
}));
}
const mockedErrorCode = 'ENOTFOUND';
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
types,
classes,
writeDNSPacket,
parseDNSPacket,
errorLookupMock,
mockedErrorCode,
mockedSysCall
};