@@ -57,8 +57,14 @@ vi.mock("node:net", () => ({
5757 getDefaultAutoSelectFamily,
5858} ) ) ;
5959
60+ vi . mock ( "./proxy-env.js" , ( ) => ( {
61+ hasEnvHttpProxyConfigured : vi . fn ( ( ) => false ) ,
62+ } ) ) ;
63+
64+ import { hasEnvHttpProxyConfigured } from "./proxy-env.js" ;
6065import {
6166 DEFAULT_UNDICI_STREAM_TIMEOUT_MS ,
67+ ensureGlobalUndiciEnvProxyDispatcher ,
6268 ensureGlobalUndiciStreamTimeouts ,
6369 resetGlobalUndiciStreamTimeoutsForTests ,
6470} from "./undici-global-dispatcher.js" ;
@@ -69,6 +75,7 @@ describe("ensureGlobalUndiciStreamTimeouts", () => {
6975 resetGlobalUndiciStreamTimeoutsForTests ( ) ;
7076 setCurrentDispatcher ( new Agent ( ) ) ;
7177 getDefaultAutoSelectFamily . mockReturnValue ( undefined ) ;
78+ vi . mocked ( hasEnvHttpProxyConfigured ) . mockReturnValue ( false ) ;
7279 } ) ;
7380
7481 it ( "replaces default Agent dispatcher with extended stream timeouts" , ( ) => {
@@ -136,3 +143,66 @@ describe("ensureGlobalUndiciStreamTimeouts", () => {
136143 } ) ;
137144 } ) ;
138145} ) ;
146+
147+ describe ( "ensureGlobalUndiciEnvProxyDispatcher" , ( ) => {
148+ beforeEach ( ( ) => {
149+ vi . clearAllMocks ( ) ;
150+ resetGlobalUndiciStreamTimeoutsForTests ( ) ;
151+ setCurrentDispatcher ( new Agent ( ) ) ;
152+ vi . mocked ( hasEnvHttpProxyConfigured ) . mockReturnValue ( false ) ;
153+ } ) ;
154+
155+ it ( "installs EnvHttpProxyAgent when env HTTP proxy is configured on a default Agent" , ( ) => {
156+ vi . mocked ( hasEnvHttpProxyConfigured ) . mockReturnValue ( true ) ;
157+
158+ ensureGlobalUndiciEnvProxyDispatcher ( ) ;
159+
160+ expect ( setGlobalDispatcher ) . toHaveBeenCalledTimes ( 1 ) ;
161+ expect ( getCurrentDispatcher ( ) ) . toBeInstanceOf ( EnvHttpProxyAgent ) ;
162+ } ) ;
163+
164+ it ( "does not override unsupported custom proxy dispatcher types" , ( ) => {
165+ vi . mocked ( hasEnvHttpProxyConfigured ) . mockReturnValue ( true ) ;
166+ setCurrentDispatcher ( new ProxyAgent ( "http://proxy.test:8080" ) ) ;
167+
168+ ensureGlobalUndiciEnvProxyDispatcher ( ) ;
169+
170+ expect ( setGlobalDispatcher ) . not . toHaveBeenCalled ( ) ;
171+ } ) ;
172+
173+ it ( "retries proxy bootstrap after an unsupported dispatcher later becomes a default Agent" , ( ) => {
174+ vi . mocked ( hasEnvHttpProxyConfigured ) . mockReturnValue ( true ) ;
175+ setCurrentDispatcher ( new ProxyAgent ( "http://proxy.test:8080" ) ) ;
176+
177+ ensureGlobalUndiciEnvProxyDispatcher ( ) ;
178+ expect ( setGlobalDispatcher ) . not . toHaveBeenCalled ( ) ;
179+
180+ setCurrentDispatcher ( new Agent ( ) ) ;
181+ ensureGlobalUndiciEnvProxyDispatcher ( ) ;
182+
183+ expect ( setGlobalDispatcher ) . toHaveBeenCalledTimes ( 1 ) ;
184+ expect ( getCurrentDispatcher ( ) ) . toBeInstanceOf ( EnvHttpProxyAgent ) ;
185+ } ) ;
186+
187+ it ( "is idempotent after proxy bootstrap succeeds" , ( ) => {
188+ vi . mocked ( hasEnvHttpProxyConfigured ) . mockReturnValue ( true ) ;
189+
190+ ensureGlobalUndiciEnvProxyDispatcher ( ) ;
191+ ensureGlobalUndiciEnvProxyDispatcher ( ) ;
192+
193+ expect ( setGlobalDispatcher ) . toHaveBeenCalledTimes ( 1 ) ;
194+ } ) ;
195+
196+ it ( "reinstalls env proxy if an external change later reverts the dispatcher to Agent" , ( ) => {
197+ vi . mocked ( hasEnvHttpProxyConfigured ) . mockReturnValue ( true ) ;
198+
199+ ensureGlobalUndiciEnvProxyDispatcher ( ) ;
200+ expect ( setGlobalDispatcher ) . toHaveBeenCalledTimes ( 1 ) ;
201+
202+ setCurrentDispatcher ( new Agent ( ) ) ;
203+ ensureGlobalUndiciEnvProxyDispatcher ( ) ;
204+
205+ expect ( setGlobalDispatcher ) . toHaveBeenCalledTimes ( 2 ) ;
206+ expect ( getCurrentDispatcher ( ) ) . toBeInstanceOf ( EnvHttpProxyAgent ) ;
207+ } ) ;
208+ } ) ;
0 commit comments