@@ -19,6 +19,23 @@ import test, {
19
19
} from 'ava' ;
20
20
import createGlobalProxyAgent from '../../../src/factories/createGlobalProxyAgent' ;
21
21
22
+ const anyproxyDefaultRules = {
23
+ beforeDealHttpsRequest : async ( ) => {
24
+ return true ;
25
+ } ,
26
+ beforeSendRequest : ( ) => {
27
+ return {
28
+ response : {
29
+ body : 'OK' ,
30
+ header : {
31
+ 'content-type' : 'text/plain' ,
32
+ } ,
33
+ statusCode : 200 ,
34
+ } ,
35
+ } ;
36
+ } ,
37
+ } ;
38
+
22
39
const defaultHttpAgent = http . globalAgent ;
23
40
const defaultHttpsAgent = https . globalAgent ;
24
41
@@ -87,31 +104,14 @@ const createHttpResponseResolver = (resolve) => {
87
104
} ;
88
105
} ;
89
106
90
- const createProxyServer = async ( maybeBeforeSendRequest ) => {
107
+ const createProxyServer = async ( anyproxyRules ) => {
91
108
const port = await getNextPort ( ) ;
92
109
93
- let beforeSendRequest = ( ) => {
94
- return {
95
- response : {
96
- body : 'OK' ,
97
- header : {
98
- 'content-type' : 'text/plain' ,
99
- } ,
100
- statusCode : 200 ,
101
- } ,
102
- } ;
103
- } ;
104
-
105
- if ( maybeBeforeSendRequest ) {
106
- beforeSendRequest = maybeBeforeSendRequest ;
107
- }
108
-
109
110
const localProxyServer = await new Promise ( ( resolve ) => {
110
111
const proxyServer = new ProxyServer ( {
111
- forceProxyHttps : true ,
112
112
port,
113
113
rule : {
114
- beforeSendRequest ,
114
+ ... anyproxyRules ? anyproxyRules : anyproxyDefaultRules ,
115
115
} ,
116
116
} ) ;
117
117
@@ -173,19 +173,11 @@ test('proxies HTTP request', async (t) => {
173
173
test ( 'proxies HTTP request with proxy-authorization header' , async ( t ) => {
174
174
const globalProxyAgent = createGlobalProxyAgent ( ) ;
175
175
176
- const beforeSendRequest = sinon . stub ( ) . callsFake ( ( ) => {
177
- return {
178
- response : {
179
- body : 'OK' ,
180
- header : {
181
- 'content-type' : 'text/plain' ,
182
- } ,
183
- statusCode : 200 ,
184
- } ,
185
- } ;
186
- } ) ;
176
+ const beforeSendRequest = sinon . stub ( ) . callsFake ( anyproxyDefaultRules . beforeSendRequest ) ;
187
177
188
- const proxyServer = await createProxyServer ( beforeSendRequest ) ;
178
+ const proxyServer = await createProxyServer ( {
179
+ beforeSendRequest,
180
+ } ) ;
189
181
190
182
globalProxyAgent . HTTP_PROXY = 'http://foo@127.0.0.1:' + proxyServer . port ;
191
183
@@ -212,6 +204,29 @@ test('proxies HTTPS request', async (t) => {
212
204
t . assert ( response . body === 'OK' ) ;
213
205
} ) ;
214
206
207
+ test ( 'proxies HTTPS request with proxy-authorization header' , async ( t ) => {
208
+ const globalProxyAgent = createGlobalProxyAgent ( ) ;
209
+
210
+ const beforeDealHttpsRequest = sinon . stub ( ) . callsFake ( async ( ) => {
211
+ return true ;
212
+ } ) ;
213
+
214
+ const proxyServer = await createProxyServer ( {
215
+ beforeDealHttpsRequest,
216
+ beforeSendRequest : anyproxyDefaultRules . beforeSendRequest ,
217
+ } ) ;
218
+
219
+ globalProxyAgent . HTTP_PROXY = 'http://foo@127.0.0.1:' + proxyServer . port ;
220
+
221
+ const response = await new Promise ( ( resolve ) => {
222
+ https . get ( 'https://127.0.0.1' , createHttpResponseResolver ( resolve ) ) ;
223
+ } ) ;
224
+
225
+ t . assert ( response . body === 'OK' ) ;
226
+
227
+ t . is ( beforeDealHttpsRequest . firstCall . args [ 0 ] . _req . headers [ 'proxy-authorization' ] , 'Basic Zm9v' ) ;
228
+ } ) ;
229
+
215
230
test ( 'does not produce unhandled rejection when cannot connect to proxy' , async ( t ) => {
216
231
const globalProxyAgent = createGlobalProxyAgent ( ) ;
217
232
0 commit comments