@@ -102,22 +102,25 @@ describe('utils', () => {
102
102
103
103
describe ( 'proxy configuration' , ( ) => {
104
104
test ( 'should have a ProxyAgent by default' , async ( ) => {
105
- jest . spyOn ( axios , 'create' ) . mockImplementation ( ( ( ) => ( args : AxiosRequestConfig ) => args . httpsAgent ) as any )
106
- const requestOptions = {
107
- apiKey : 'apiKey' ,
108
- appKey : 'applicationKey' ,
109
- baseUrl : 'http://fake-base.url/' ,
110
- }
111
- const request = ciUtils . getRequestBuilder ( requestOptions )
112
- const fakeEndpoint = fakeEndpointBuilder ( request )
113
- const httpsAgent = await fakeEndpoint ( )
105
+ const httpsAgent = await getHttpAgentForProxyOptions ( )
114
106
expect ( httpsAgent ) . toBeDefined ( )
115
107
expect ( httpsAgent ) . toBeInstanceOf ( ProxyAgent )
116
108
} )
117
109
118
110
test ( 'should add proxy configuration when explicitly defined' , async ( ) => {
111
+ const httpsAgent = await getHttpAgentForProxyOptions ( { protocol : 'http' , host : '1.2.3.4' , port : 1234 } )
112
+ expect ( httpsAgent ) . toBeDefined ( )
113
+ expect ( ( httpsAgent as any ) . getProxyForUrl ( ) ) . toBe ( 'http://1.2.3.4:1234' )
114
+ } )
115
+
116
+ test ( 'should re-use the same proxy agent for the same proxy options' , async ( ) => {
117
+ const httpsAgent1 = await getHttpAgentForProxyOptions ( { protocol : 'http' , host : '1.2.3.4' , port : 1234 } )
118
+ const httpsAgent2 = await getHttpAgentForProxyOptions ( { protocol : 'http' , host : '1.2.3.4' , port : 1234 } )
119
+ expect ( httpsAgent1 ) . toBe ( httpsAgent2 )
120
+ } )
121
+
122
+ const getHttpAgentForProxyOptions = async ( proxyOpts ?: ciUtils . ProxyConfiguration ) => {
119
123
jest . spyOn ( axios , 'create' ) . mockImplementation ( ( ( ) => ( args : AxiosRequestConfig ) => args . httpsAgent ) as any )
120
- const proxyOpts : ciUtils . ProxyConfiguration = { protocol : 'http' , host : '1.2.3.4' , port : 1234 }
121
124
const requestOptions = {
122
125
apiKey : 'apiKey' ,
123
126
appKey : 'applicationKey' ,
@@ -126,10 +129,9 @@ describe('utils', () => {
126
129
}
127
130
const request = ciUtils . getRequestBuilder ( requestOptions )
128
131
const fakeEndpoint = fakeEndpointBuilder ( request )
129
- const httpsAgent = await fakeEndpoint ( )
130
- expect ( httpsAgent ) . toBeDefined ( )
131
- expect ( ( httpsAgent as any ) . getProxyForUrl ( ) ) . toBe ( 'http://1.2.3.4:1234' )
132
- } )
132
+
133
+ return fakeEndpoint ( )
134
+ }
133
135
} )
134
136
135
137
test ( 'should accept overrideUrl' , async ( ) => {
0 commit comments