File tree Expand file tree Collapse file tree 2 files changed +43
-4
lines changed Expand file tree Collapse file tree 2 files changed +43
-4
lines changed Original file line number Diff line number Diff line change @@ -419,7 +419,26 @@ called before the callback is invoked.
419419
420420## DNS Module
421421
422- The ` DNS ` module provides a naïve DNS parser/serializer.
422+ The ` DNS ` module provides utilities related to the ` dns ` built-in module.
423+
424+ ### errorLookupMock(code, syscall)
425+
426+ * ` code ` [ < ; String>] Defaults to ` dns.mockedErrorCode ` .
427+ * ` syscall ` [ < ; String>] Defaults to ` dns.mockedSysCall ` .
428+ * return [ < ; Function>]
429+
430+
431+ A mock for the ` lookup ` option of ` net.connect() ` that would result in an error
432+ with the ` code ` and the ` syscall ` specified. Returns a function that has the
433+ same signature as ` dns.lookup() ` .
434+
435+ ### mockedErrorCode
436+
437+ The default ` code ` of errors generated by ` errorLookupMock ` .
438+
439+ ### mockedSysCall
440+
441+ The default ` syscall ` of errors generated by ` errorLookupMock ` .
423442
424443### readDomainFromPacket(buffer, offset)
425444
Original file line number Diff line number Diff line change 11/* eslint-disable required-modules */
22'use strict' ;
33
4- // Naïve DNS parser/serializer.
5-
64const assert = require ( 'assert' ) ;
75const os = require ( 'os' ) ;
86
@@ -22,6 +20,8 @@ const classes = {
2220 IN : 1
2321} ;
2422
23+ // Naïve DNS parser/serializer.
24+
2525function readDomainFromPacket ( buffer , offset ) {
2626 assert . ok ( offset < buffer . length ) ;
2727 const length = buffer [ offset ] ;
@@ -287,6 +287,26 @@ function writeDNSPacket(parsed) {
287287 } ) ) ;
288288}
289289
290+ const mockedErrorCode = 'ENOTFOUND' ;
291+ const mockedSysCall = 'getaddrinfo' ;
292+
293+ function errorLookupMock ( code = mockedErrorCode , syscall = mockedSysCall ) {
294+ return function lookupWithError ( host , dnsopts , cb ) {
295+ const err = new Error ( `${ syscall } ${ code } ${ host } ` ) ;
296+ err . code = code ;
297+ err . errno = code ;
298+ err . syscall = syscall ;
299+ err . hostname = host ;
300+ cb ( err ) ;
301+ } ;
302+ }
303+
290304module . exports = {
291- types, classes, writeDNSPacket, parseDNSPacket
305+ types,
306+ classes,
307+ writeDNSPacket,
308+ parseDNSPacket,
309+ errorLookupMock,
310+ mockedErrorCode,
311+ mockedSysCall
292312} ;
You can’t perform that action at this time.
0 commit comments