@@ -189,14 +189,15 @@ describe('http proxy', () => {
189
189
} )
190
190
} )
191
191
192
- describe ( 'https proxy' , ( ) => {
192
+ describe ( 'https proxy (self-signed) ' , ( ) => {
193
193
const protocol = 'https'
194
194
let proxyServer , apiServer
195
195
const portNotInUse = 3009
196
+ const selfSigned = true
196
197
197
198
describe ( 'no auth' , ( ) => {
198
199
beforeAll ( async ( ) => {
199
- proxyServer = await createHttpsProxy ( )
200
+ proxyServer = await createHttpsProxy ( { selfSigned } )
200
201
apiServer = await createApiServer ( { port : 3001 , useSsl : true } )
201
202
} )
202
203
@@ -212,29 +213,48 @@ describe('https proxy', () => {
212
213
const testUrl = `${ protocol } ://localhost:${ apiServerAddress . port } /mirror?${ queryString . stringify ( queryObject ) } `
213
214
214
215
const proxyUrl = proxyServer . url
215
- const proxyFetch = new ProxyFetch ( { proxyUrl, rejectUnauthorized : false } )
216
- const response = await proxyFetch . fetch ( testUrl )
216
+ // IGNORE self-signed cert errors
217
+ {
218
+ const proxyFetch = new ProxyFetch ( { proxyUrl, rejectUnauthorized : false } )
219
+ const response = await proxyFetch . fetch ( testUrl )
217
220
218
- const json = await response . json ( )
219
- expect ( json ) . toStrictEqual ( queryObject )
221
+ const json = await response . json ( )
222
+ expect ( json ) . toStrictEqual ( queryObject )
223
+ }
224
+ // DO NOT ignore self-signed cert errors
225
+ {
226
+ const proxyFetch = new ProxyFetch ( { proxyUrl, rejectUnauthorized : true } )
227
+ await expect ( async ( ) => {
228
+ await proxyFetch . fetch ( testUrl )
229
+ } ) . rejects . toThrow ( 'self-signed certificate in certificate chain' )
230
+ }
220
231
} )
221
232
222
- test ( 'failure' , async ( ) => {
233
+ test ( 'failure (non-existent port) ' , async ( ) => {
223
234
// connect to non-existent server port
224
235
const testUrl = `${ protocol } ://localhost:${ portNotInUse } /mirror/?foo=bar`
225
-
226
236
const proxyUrl = proxyServer . url
227
- const proxyFetch = new ProxyFetch ( { proxyUrl, rejectUnauthorized : false } )
228
237
229
- const response = await proxyFetch . fetch ( testUrl )
230
- expect ( response . ok ) . toEqual ( false )
231
- expect ( response . status ) . toEqual ( 502 )
238
+ // IGNORE self-signed cert errors
239
+ {
240
+ const proxyFetch = new ProxyFetch ( { proxyUrl, rejectUnauthorized : false } )
241
+ const response = await proxyFetch . fetch ( testUrl )
242
+ expect ( response . ok ) . toEqual ( false )
243
+ expect ( response . status ) . toEqual ( 502 )
244
+ }
245
+ // DO NOT ignore self-signed cert errors
246
+ {
247
+ const proxyFetch = new ProxyFetch ( { proxyUrl, rejectUnauthorized : true } )
248
+ await expect ( async ( ) => {
249
+ await proxyFetch . fetch ( testUrl )
250
+ } ) . rejects . toThrow ( 'self-signed certificate in certificate chain' )
251
+ }
232
252
} )
233
253
} )
234
254
235
255
describe ( 'basic auth' , ( ) => {
236
256
beforeAll ( async ( ) => {
237
- proxyServer = await createHttpsProxy ( { useBasicAuth : true } )
257
+ proxyServer = await createHttpsProxy ( { useBasicAuth : true , selfSigned } )
238
258
apiServer = await createApiServer ( { port : 3001 , useSsl : true } )
239
259
} )
240
260
@@ -253,17 +273,28 @@ describe('https proxy', () => {
253
273
'Proxy-Authorization' : 'Basic ' + Buffer . from ( `${ username } :${ password } ` ) . toString ( 'base64' )
254
274
}
255
275
const proxyUrl = proxyServer . url
256
- const proxyFetch = new ProxyFetch ( { proxyUrl, username, password, rejectUnauthorized : false } )
257
-
258
276
const testUrl = `${ protocol } ://localhost:${ apiServerPort } /mirror?${ queryString . stringify ( queryObject ) } `
259
- const response = await proxyFetch . fetch ( testUrl , { headers } )
260
- const spy = jest . spyOn ( proxyFetch , 'fetch' ) . mockImplementation ( ( ) => testUrl )
261
- const pattern = / \b ^ h t t p s \b /
262
- expect ( proxyFetch . fetch ( ) ) . toMatch ( new RegExp ( pattern ) )
263
- spy . mockRestore ( )
264
- expect ( response . ok ) . toEqual ( true )
265
- const json = await response . json ( )
266
- expect ( json ) . toStrictEqual ( queryObject )
277
+ // IGNORE self-signed cert errors
278
+ {
279
+ const proxyFetch = new ProxyFetch ( { proxyUrl, username, password, rejectUnauthorized : false } )
280
+ const response = await proxyFetch . fetch ( testUrl , { headers } )
281
+
282
+ const spy = jest . spyOn ( proxyFetch , 'fetch' ) . mockImplementation ( ( ) => testUrl )
283
+ const pattern = / \b ^ h t t p s \b /
284
+ expect ( proxyFetch . fetch ( ) ) . toMatch ( new RegExp ( pattern ) )
285
+ spy . mockRestore ( )
286
+
287
+ expect ( response . ok ) . toEqual ( true )
288
+ const json = await response . json ( )
289
+ expect ( json ) . toStrictEqual ( queryObject )
290
+ }
291
+ // DO NOT ignore self-signed cert errors
292
+ {
293
+ const proxyFetch = new ProxyFetch ( { proxyUrl, rejectUnauthorized : true } )
294
+ await expect ( async ( ) => {
295
+ await proxyFetch . fetch ( testUrl )
296
+ } ) . rejects . toThrow ( 'self-signed certificate in certificate chain' )
297
+ }
267
298
} )
268
299
269
300
test ( 'failure' , async ( ) => {
@@ -276,22 +307,33 @@ describe('https proxy', () => {
276
307
'Proxy-Authorization' : 'Basic ' + Buffer . from ( `${ username } :${ password } ` ) . toString ( 'base64' )
277
308
}
278
309
const proxyUrl = proxyServer . url
279
- const proxyFetch = new ProxyFetch ( { proxyUrl, username, password, rejectUnauthorized : false } )
280
-
281
310
const testUrl = `${ protocol } ://localhost:${ apiServerPort } /mirror?${ queryString . stringify ( queryObject ) } `
282
- const response = await proxyFetch . fetch ( testUrl , { headers } )
283
- const spy = jest . spyOn ( proxyFetch , 'fetch' ) . mockImplementation ( ( ) => testUrl )
284
- const pattern = / \b ^ h t t p \b /
285
- expect ( proxyFetch . fetch ( ) ) . not . toMatch ( new RegExp ( pattern ) )
286
- spy . mockRestore ( )
287
- expect ( response . ok ) . toEqual ( false )
288
- expect ( response . status ) . toEqual ( 403 )
311
+ // IGNORE self-signed cert errors
312
+ {
313
+ const proxyFetch = new ProxyFetch ( { proxyUrl, username, password, rejectUnauthorized : false } )
314
+ const response = await proxyFetch . fetch ( testUrl , { headers } )
315
+
316
+ const spy = jest . spyOn ( proxyFetch , 'fetch' ) . mockImplementation ( ( ) => testUrl )
317
+ const pattern = / \b ^ h t t p \b /
318
+ expect ( proxyFetch . fetch ( ) ) . not . toMatch ( new RegExp ( pattern ) )
319
+ spy . mockRestore ( )
320
+
321
+ expect ( response . ok ) . toEqual ( false )
322
+ expect ( response . status ) . toEqual ( 403 )
323
+ }
324
+ // DO NOT ignore self-signed cert errors
325
+ {
326
+ const proxyFetch = new ProxyFetch ( { proxyUrl, rejectUnauthorized : true } )
327
+ await expect ( async ( ) => {
328
+ await proxyFetch . fetch ( testUrl )
329
+ } ) . rejects . toThrow ( 'self-signed certificate in certificate chain' )
330
+ }
289
331
} )
290
332
} )
291
333
292
334
describe ( 'HttpExponentialBackoff' , ( ) => {
293
335
beforeAll ( async ( ) => {
294
- proxyServer = await createHttpsProxy ( )
336
+ proxyServer = await createHttpsProxy ( { selfSigned } )
295
337
apiServer = await createApiServer ( { port : 3001 , useSsl : true } )
296
338
} )
297
339
@@ -306,27 +348,45 @@ describe('https proxy', () => {
306
348
307
349
const testUrl = `${ protocol } ://localhost:${ apiServerPort } /mirror?${ queryString . stringify ( queryObject ) } `
308
350
const proxyUrl = proxyServer . url
309
-
310
351
const fetchRetry = new HttpExponentialBackoff ( )
311
- const response = await fetchRetry . exponentialBackoff ( testUrl , { method : 'GET' } , {
312
- proxy : { proxyUrl, rejectUnauthorized : false }
313
- } )
314
- const json = await response . json ( )
315
- expect ( json ) . toStrictEqual ( queryObject )
352
+
353
+ // IGNORE self-signed cert errors
354
+ {
355
+ const response = await fetchRetry . exponentialBackoff ( testUrl , { method : 'GET' } , {
356
+ proxy : { proxyUrl, rejectUnauthorized : false }
357
+ } )
358
+ const json = await response . json ( )
359
+ expect ( json ) . toStrictEqual ( queryObject )
360
+ }
361
+ // DO NOT ignore self-signed cert errors
362
+ await expect ( async ( ) => {
363
+ return fetchRetry . exponentialBackoff ( testUrl , { method : 'GET' } , {
364
+ proxy : { proxyUrl, rejectUnauthorized : true }
365
+ } )
366
+ } ) . rejects . toThrow ( 'self-signed certificate in certificate chain' )
316
367
} )
317
368
318
369
test ( 'failure' , async ( ) => {
319
370
// connect to non-existent server port
320
371
const testUrl = `${ protocol } ://localhost:3009/mirror/?foo=bar`
321
372
const proxyUrl = proxyServer . url
322
-
323
373
const fetchRetry = new HttpExponentialBackoff ( )
324
- const response = await fetchRetry . exponentialBackoff ( testUrl , { method : 'GET' } , {
325
- proxy : { proxyUrl, rejectUnauthorized : false } ,
326
- maxRetries : 2
327
- } , [ ] , 0 ) // retryDelay must be zero for test timings
328
- expect ( response . ok ) . toEqual ( false )
329
- expect ( response . status ) . toEqual ( 502 )
374
+
375
+ // IGNORE self-signed cert errors
376
+ {
377
+ const response = await fetchRetry . exponentialBackoff ( testUrl , { method : 'GET' } , {
378
+ proxy : { proxyUrl, rejectUnauthorized : false } ,
379
+ maxRetries : 2
380
+ } , [ ] , 0 ) // retryDelay must be zero for test timings
381
+ expect ( response . ok ) . toEqual ( false )
382
+ expect ( response . status ) . toEqual ( 502 )
383
+ }
384
+ // DO NOT ignore self-signed cert errors
385
+ await expect ( async ( ) => {
386
+ return fetchRetry . exponentialBackoff ( testUrl , { method : 'GET' } , {
387
+ proxy : { proxyUrl, rejectUnauthorized : true }
388
+ } )
389
+ } ) . rejects . toThrow ( 'self-signed certificate in certificate chain' )
330
390
} )
331
391
} )
332
392
} )
0 commit comments