Skip to content

Commit

Permalink
test: add common.dns.errorLookupMock
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
joyeecheung authored and MylesBorins committed Dec 12, 2017
1 parent 7e247c1 commit b7578bf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
21 changes: 20 additions & 1 deletion test/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
26 changes: 23 additions & 3 deletions test/common/dns.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* eslint-disable required-modules */
'use strict';

// Naïve DNS parser/serializer.

const assert = require('assert');
const os = require('os');

Expand All @@ -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];
Expand Down Expand Up @@ -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
};

0 comments on commit b7578bf

Please sign in to comment.