11import { it , expect , vi } from 'vitest' ;
22import http from 'http' ;
3+ import https from 'https' ;
4+ import { HttpsProxyAgent } from 'https-proxy-agent' ;
35import { axiosGet } from './utils' ;
46
57vi . mock ( 'axios' , ( ) => ( {
@@ -8,22 +10,146 @@ vi.mock('axios', () => ({
810 } ,
911} ) ) ;
1012
13+ vi . mock ( 'https-proxy-agent' , ( ) => ( {
14+ HttpsProxyAgent : vi . fn ( ) ,
15+ } ) ) ;
16+
1117it ( 'axiosGet should use agents with family set to 4' , async ( ) => {
1218 const httpSpy = vi . spyOn ( http , 'Agent' ) ;
19+ const httpsSpy = vi . spyOn ( https , 'Agent' ) ;
1320
1421 await axiosGet ( 'http://localhost' ) ;
1522
1623 expect ( httpSpy ) . toHaveBeenCalledWith ( { family : 4 } ) ;
24+ expect ( httpsSpy ) . toHaveBeenCalledWith ( { family : 4 } ) ;
1725
1826 httpSpy . mockRestore ( ) ;
27+ httpsSpy . mockRestore ( ) ;
1928} ) ;
2029
2130it ( 'axiosGet should allow to use agents with family set to 6' , async ( ) => {
2231 const httpSpy = vi . spyOn ( http , 'Agent' ) ;
32+ const httpsSpy = vi . spyOn ( https , 'Agent' ) ;
2333
2434 await axiosGet ( 'http://localhost' , { family : 6 } ) ;
2535
2636 expect ( httpSpy ) . toHaveBeenCalledWith ( { family : 6 } ) ;
37+ expect ( httpsSpy ) . toHaveBeenCalledWith ( { family : 6 } ) ;
38+
39+ httpSpy . mockRestore ( ) ;
40+ httpsSpy . mockRestore ( ) ;
41+ } ) ;
42+
43+ it ( 'axiosGet should use HttpsProxyAgent when HTTP_PROXY is set' , async ( ) => {
44+ const originalHttpProxy = process . env . HTTP_PROXY ;
45+ process . env . HTTP_PROXY = 'http://proxy.company.com:8080' ;
46+
47+ const httpSpy = vi . spyOn ( http , 'Agent' ) ;
48+ const httpsProxyAgentSpy = vi . mocked ( HttpsProxyAgent ) ;
49+
50+ await axiosGet ( 'https://example.com' ) ;
51+
52+ expect ( httpSpy ) . toHaveBeenCalledWith ( { family : 4 } ) ;
53+ expect ( httpsProxyAgentSpy ) . toHaveBeenCalledWith ( 'http://proxy.company.com:8080' , { family : 4 } ) ;
54+
55+ // Restore environment variable
56+ if ( originalHttpProxy !== undefined ) {
57+ process . env . HTTP_PROXY = originalHttpProxy ;
58+ } else {
59+ delete process . env . HTTP_PROXY ;
60+ }
61+
62+ httpSpy . mockRestore ( ) ;
63+ } ) ;
64+
65+ it ( 'axiosGet should use HttpsProxyAgent when HTTPS_PROXY is set' , async ( ) => {
66+ const originalHttpsProxy = process . env . HTTPS_PROXY ;
67+ process . env . HTTPS_PROXY = 'http://proxy.company.com:8080' ;
68+
69+ const httpSpy = vi . spyOn ( http , 'Agent' ) ;
70+ const httpsProxyAgentSpy = vi . mocked ( HttpsProxyAgent ) ;
71+
72+ await axiosGet ( 'https://example.com' ) ;
73+
74+ expect ( httpSpy ) . toHaveBeenCalledWith ( { family : 4 } ) ;
75+ expect ( httpsProxyAgentSpy ) . toHaveBeenCalledWith ( 'http://proxy.company.com:8080' , { family : 4 } ) ;
76+
77+ // Restore environment variable
78+ if ( originalHttpsProxy !== undefined ) {
79+ process . env . HTTPS_PROXY = originalHttpsProxy ;
80+ } else {
81+ delete process . env . HTTPS_PROXY ;
82+ }
83+
84+ httpSpy . mockRestore ( ) ;
85+ } ) ;
86+
87+ it ( 'axiosGet should prefer HTTPS_PROXY over HTTP_PROXY when both are set' , async ( ) => {
88+ const originalHttpProxy = process . env . HTTP_PROXY ;
89+ const originalHttpsProxy = process . env . HTTPS_PROXY ;
90+
91+ process . env . HTTP_PROXY = 'http://proxy1.company.com:8080' ;
92+ process . env . HTTPS_PROXY = 'http://proxy2.company.com:8080' ;
93+
94+ const httpSpy = vi . spyOn ( http , 'Agent' ) ;
95+ const httpsProxyAgentSpy = vi . mocked ( HttpsProxyAgent ) ;
96+
97+ await axiosGet ( 'https://example.com' ) ;
98+
99+ expect ( httpSpy ) . toHaveBeenCalledWith ( { family : 4 } ) ;
100+ expect ( httpsProxyAgentSpy ) . toHaveBeenCalledWith ( 'http://proxy2.company.com:8080' , { family : 4 } ) ;
101+
102+ // Restore environment variables
103+ if ( originalHttpProxy !== undefined ) {
104+ process . env . HTTP_PROXY = originalHttpProxy ;
105+ } else {
106+ delete process . env . HTTP_PROXY ;
107+ }
108+ if ( originalHttpsProxy !== undefined ) {
109+ process . env . HTTPS_PROXY = originalHttpsProxy ;
110+ } else {
111+ delete process . env . HTTPS_PROXY ;
112+ }
113+
114+ httpSpy . mockRestore ( ) ;
115+ } ) ;
116+
117+ it ( 'axiosGet should use standard https.Agent when no proxy is configured' , async ( ) => {
118+ const originalHttpProxy = process . env . HTTP_PROXY ;
119+ const originalHttpsProxy = process . env . HTTPS_PROXY ;
120+ const originalHttpProxyLower = process . env . http_proxy ;
121+ const originalHttpsProxyLower = process . env . https_proxy ;
122+
123+ // Clear all proxy environment variables
124+ delete process . env . HTTP_PROXY ;
125+ delete process . env . HTTPS_PROXY ;
126+ delete process . env . http_proxy ;
127+ delete process . env . https_proxy ;
128+
129+ const httpSpy = vi . spyOn ( http , 'Agent' ) ;
130+ const httpsSpy = vi . spyOn ( https , 'Agent' ) ;
131+ const httpsProxyAgentSpy = vi . mocked ( HttpsProxyAgent ) ;
132+
133+ await axiosGet ( 'https://example.com' ) ;
134+
135+ expect ( httpSpy ) . toHaveBeenCalledWith ( { family : 4 } ) ;
136+ expect ( httpsSpy ) . toHaveBeenCalledWith ( { family : 4 } ) ;
137+ expect ( httpsProxyAgentSpy ) . not . toHaveBeenCalled ( ) ;
138+
139+ // Restore environment variables
140+ if ( originalHttpProxy !== undefined ) {
141+ process . env . HTTP_PROXY = originalHttpProxy ;
142+ }
143+ if ( originalHttpsProxy !== undefined ) {
144+ process . env . HTTPS_PROXY = originalHttpsProxy ;
145+ }
146+ if ( originalHttpProxyLower !== undefined ) {
147+ process . env . http_proxy = originalHttpProxyLower ;
148+ }
149+ if ( originalHttpsProxyLower !== undefined ) {
150+ process . env . https_proxy = originalHttpsProxyLower ;
151+ }
27152
28153 httpSpy . mockRestore ( ) ;
154+ httpsSpy . mockRestore ( ) ;
29155} ) ;
0 commit comments