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
Next Next commit
test: add common.dns.errorLookupMock
  • Loading branch information
joyeecheung committed Nov 25, 2017
commit 452c4b8aa10ad18fef69ce34dac5c4b61eb5364c
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` [<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)

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
};