11import { beforeEach , describe , expect , it , vi } from "vitest" ;
22import { EventEmitter } from "node:events" ;
33import { PassThrough } from "node:stream" ;
4- const mocks = vi . hoisted ( ( ) => ( { lookup : vi . fn ( ) , request : vi . fn ( ) } ) ) ;
5- vi . mock ( "node:dns/promises" , ( ) => ( { lookup : mocks . lookup } ) ) ;
4+ const mocks = vi . hoisted ( ( ) => ( { resolve4 : vi . fn ( ) , request : vi . fn ( ) } ) ) ;
5+ vi . mock ( "node:dns" , ( ) => ( {
6+ promises : {
7+ Resolver : class {
8+ resolve4 = mocks . resolve4 ;
9+ cancel ( ) { }
10+ } ,
11+ } ,
12+ } ) ) ;
613vi . mock ( "node:http" , ( ) => ( { request : mocks . request } ) ) ;
714vi . mock ( "node:https" , ( ) => ( { request : mocks . request } ) ) ;
815import { isPublicIPv4 , publicRequest , resolvePublicAddress } from "./publicNetwork.js" ;
@@ -30,7 +37,7 @@ describe("public address policy", () => {
3037 ] ) ( "rejects %s" , ( ip ) => expect ( isPublicIPv4 ( ip ) ) . toBe ( false ) ) ;
3138 it ( "accepts public literals without DNS" , async ( ) => {
3239 await expect ( resolvePublicAddress ( "8.8.8.8" ) ) . resolves . toBe ( "8.8.8.8" ) ;
33- expect ( mocks . lookup ) . not . toHaveBeenCalled ( ) ;
40+ expect ( mocks . resolve4 ) . not . toHaveBeenCalled ( ) ;
3441 } ) ;
3542 it . each ( [
3643 "localhost" ,
@@ -41,25 +48,22 @@ describe("public address policy", () => {
4148 "[::1]" ,
4249 ] ) ( "blocks alias/literal %s before DNS" , async ( host ) => {
4350 await expect ( resolvePublicAddress ( host ) ) . rejects . toThrow ( ) ;
44- expect ( mocks . lookup ) . not . toHaveBeenCalled ( ) ;
51+ expect ( mocks . resolve4 ) . not . toHaveBeenCalled ( ) ;
4552 } ) ;
4653 it ( "rejects mixed DNS before opening a socket" , async ( ) => {
47- mocks . lookup . mockResolvedValue ( [
48- { address : "8.8.8.8" , family : 4 } ,
49- { address : "127.0.0.1" , family : 4 } ,
50- ] ) ;
54+ mocks . resolve4 . mockResolvedValue ( [ "8.8.8.8" , "127.0.0.1" ] ) ;
5155 await expect ( publicRequest ( "http://example.com/" ) ) . rejects . toThrow ( ) ;
5256 expect ( mocks . request ) . not . toHaveBeenCalled ( ) ;
5357 } ) ;
54- it . each ( [ { answers : [ ] } , { answers : [ { address : " 8.8.8.8", family : 6 } ] } ] ) (
58+ it . each ( [ { answers : [ ] } , { answers : [ "::ffff: 8.8.8.8"] } ] ) (
5559 "rejects empty or malformed DNS" ,
5660 async ( { answers } ) => {
57- mocks . lookup . mockResolvedValue ( answers ) ;
61+ mocks . resolve4 . mockResolvedValue ( answers ) ;
5862 await expect ( resolvePublicAddress ( "example.com" ) ) . rejects . toThrow ( ) ;
5963 } ,
6064 ) ;
6165 it ( "propagates DNS errors" , async ( ) => {
62- mocks . lookup . mockRejectedValue ( new Error ( "DNS failed" ) ) ;
66+ mocks . resolve4 . mockRejectedValue ( new Error ( "DNS failed" ) ) ;
6367 await expect ( resolvePublicAddress ( "example.com" ) ) . rejects . toThrow ( "DNS failed" ) ;
6468 } ) ;
6569} ) ;
@@ -86,14 +90,12 @@ function fakeRequest() {
8690}
8791describe ( "pinned request" , ( ) => {
8892 it ( "dials the checked numeric IP only, preserving TLS identity and manual redirects" , async ( ) => {
89- mocks . lookup
90- . mockResolvedValueOnce ( [ { address : "8.8.8.8" , family : 4 } ] )
91- . mockResolvedValue ( [ { address : "127.0.0.1" , family : 4 } ] ) ;
93+ mocks . resolve4 . mockResolvedValueOnce ( [ "8.8.8.8" ] ) . mockResolvedValue ( [ "127.0.0.1" ] ) ;
9294 fakeRequest ( ) ;
9395 const response = await publicRequest ( "https://example.com:8443/path?q=1" , {
9496 headers : { host : "evil.test" , "accept-encoding" : "gzip" } ,
9597 } ) ;
96- expect ( mocks . lookup ) . toHaveBeenCalledTimes ( 1 ) ;
98+ expect ( mocks . resolve4 ) . toHaveBeenCalledTimes ( 1 ) ;
9799 expect ( mocks . request ) . toHaveBeenCalledTimes ( 1 ) ;
98100 expect ( mocks . request . mock . calls [ 0 ] ?. [ 0 ] ) . toMatchObject ( {
99101 hostname : "8.8.8.8" ,
@@ -116,9 +118,9 @@ describe("pinned request", () => {
116118 ) ;
117119 it ( "does not dial after abort during DNS" , async ( ) => {
118120 const controller = new AbortController ( ) ;
119- mocks . lookup . mockImplementation ( async ( ) => {
121+ mocks . resolve4 . mockImplementation ( async ( ) => {
120122 controller . abort ( ) ;
121- return [ { address : "8.8.8.8" , family : 4 } ] ;
123+ return [ "8.8.8.8" ] ;
122124 } ) ;
123125 await expect (
124126 publicRequest ( "https://example.com" , { signal : controller . signal } ) ,
@@ -128,20 +130,8 @@ describe("pinned request", () => {
128130} ) ;
129131
130132describe ( "transport lifecycle" , ( ) => {
131- it ( "bounds stalled DNS without dialing" , async ( ) => {
132- vi . useFakeTimers ( ) ;
133- try {
134- mocks . lookup . mockImplementation ( ( ) => new Promise ( ( ) => { } ) ) ;
135- const result = expect ( publicRequest ( "https://example.com" ) ) . rejects . toThrow ( "DNS timed out" ) ;
136- await vi . advanceTimersByTimeAsync ( 10_000 ) ;
137- await result ;
138- expect ( mocks . request ) . not . toHaveBeenCalled ( ) ;
139- } finally {
140- vi . useRealTimers ( ) ;
141- }
142- } ) ;
143133 it ( "cancels the upstream stream and socket when the body is cancelled" , async ( ) => {
144- mocks . lookup . mockResolvedValue ( [ { address : "8.8.8.8" , family : 4 } ] ) ;
134+ mocks . resolve4 . mockResolvedValue ( [ "8.8.8.8" ] ) ;
145135 const incoming = Object . assign ( new PassThrough ( ) , { statusCode : 200 , headers : { } } ) ;
146136 const req = Object . assign ( new EventEmitter ( ) , { end : vi . fn ( ) , destroy : vi . fn ( ) } ) ;
147137 mocks . request . mockImplementation ( ( _options , callback ) => {
@@ -161,7 +151,7 @@ describe("transport lifecycle", () => {
161151 } ) . not . toThrow ( ) ;
162152 } ) ;
163153 it ( "fails unsupported compressed responses instead of returning corrupted asset bytes" , async ( ) => {
164- mocks . lookup . mockResolvedValue ( [ { address : "8.8.8.8" , family : 4 } ] ) ;
154+ mocks . resolve4 . mockResolvedValue ( [ "8.8.8.8" ] ) ;
165155 const incoming = Object . assign ( new PassThrough ( ) , {
166156 statusCode : 200 ,
167157 headers : { "content-encoding" : "gzip" } ,
0 commit comments