Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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>
  • Loading branch information
joyeecheung committed Mar 30, 2018
commit fde95fff26531145331c597b8f8584b9c0d66d19
21 changes: 20 additions & 1 deletion test/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,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
};