@@ -459,22 +459,22 @@ nodeOnly(() => {
459
459
keyPath : './test/fixtures/test-ca.key' ,
460
460
certPath : './test/fixtures/test-ca.pem'
461
461
} ;
462
-
462
+
463
463
const intermediateProxy = getLocal ( { https } ) ;
464
-
464
+
465
465
beforeEach ( async ( ) => {
466
466
server = getLocal ( { https } ) ;
467
467
await server . start ( ) ;
468
468
469
469
await intermediateProxy . start ( ) ;
470
-
470
+
471
471
process . env = _ . merge ( { } , process . env , server . proxyEnv ) ;
472
472
} ) ;
473
-
473
+
474
474
afterEach ( async ( ) => {
475
475
await intermediateProxy . stop ( ) ;
476
476
} ) ;
477
-
477
+
478
478
it ( "should forward traffic to intermediateProxy using PAC file" , async ( ) => {
479
479
const pacFile = `function FindProxyForURL(url, host) { return "PROXY ${ url . parse ( intermediateProxy . url ) . host } "; }` ;
480
480
await remoteServer . forGet ( '/proxy-all' ) . thenReply ( 200 , pacFile ) ;
@@ -485,43 +485,56 @@ nodeOnly(() => {
485
485
proxyUrl : `pac+${ remoteServer . url } /proxy-all`
486
486
}
487
487
} ) ;
488
-
488
+
489
489
await intermediateProxy . forAnyRequest ( ) . thenPassThrough ( {
490
490
ignoreHostHttpsErrors : true ,
491
491
beforeRequest : ( req ) => {
492
492
expect ( req . url ) . to . equal ( 'https://example.com/' ) ;
493
+ return {
494
+ response : {
495
+ statusCode : 200 ,
496
+ body : 'Proxied'
497
+ }
498
+ } ;
493
499
}
494
500
} ) ;
495
-
496
- // make request
497
- await request . get ( 'https://example.com/' ) ;
501
+
502
+ // make a request that hits the proxy based on PAC file
503
+ expect ( await request . get ( 'https://example.com/' ) ) . to . equal ( 'Proxied ') ;
498
504
} ) ;
499
-
505
+
500
506
it ( "should bypass intermediateProxy using PAC file" , async ( ) => {
501
- const pacFile = `function FindProxyForURL(url, host) { if (host.endsWith(".org ")) return "DIRECT"; return "PROXY ${ url . parse ( intermediateProxy . url ) . host } "; }` ;
507
+ const pacFile = `function FindProxyForURL(url, host) { if (host.endsWith(".com ")) { return "PROXY ${ url . parse ( intermediateProxy . url ) . host } "; } else { return "DIRECT"; } }` ;
502
508
await remoteServer . forGet ( '/proxy-bypass' ) . thenReply ( 200 , pacFile ) ;
509
+ await remoteServer . forGet ( '/remote-response' ) . thenReply ( 200 , 'Remote response' ) ;
503
510
504
511
await server . forAnyRequest ( ) . thenPassThrough ( {
505
512
ignoreHostHttpsErrors : true ,
506
513
proxyConfig : {
507
514
proxyUrl : `pac+${ remoteServer . url } /proxy-bypass`
508
515
}
509
516
} ) ;
510
-
517
+
511
518
await intermediateProxy . forAnyRequest ( ) . thenPassThrough ( {
512
519
ignoreHostHttpsErrors : true ,
513
520
beforeRequest : ( req ) => {
514
521
expect ( req . url ) . to . not . equal ( 'https://example.org/' ) ;
522
+ return {
523
+ response : {
524
+ statusCode : 200 ,
525
+ body : 'Proxied'
526
+ }
527
+ } ;
515
528
}
516
529
} ) ;
517
-
530
+
518
531
// make a request that hits the proxy based on PAC file
519
- await request . get ( 'https://example.com/' ) ;
532
+ expect ( await request . get ( 'https://example.com/' ) ) . to . equal ( 'Proxied ') ;
520
533
521
534
// make a request that bypasses proxy based on PAC file
522
- await request . get ( 'https://example.org/ ') ;
535
+ expect ( await request . get ( remoteServer . urlFor ( '/remote-response' ) ) ) . to . equal ( 'Remote response ') ;
523
536
} ) ;
524
-
537
+
525
538
it ( "should fallback to intermediateProxy using PAC file" , async ( ) => {
526
539
const pacFile = `function FindProxyForURL(url, host) { return "PROXY invalid-proxy:8080; PROXY ${ url . parse ( intermediateProxy . url ) . host } ;"; }` ;
527
540
await remoteServer . forGet ( '/proxy-fallback' ) . thenReply ( 200 , pacFile ) ;
@@ -532,16 +545,22 @@ nodeOnly(() => {
532
545
proxyUrl : `pac+${ remoteServer . url } /proxy-fallback`
533
546
}
534
547
} ) ;
535
-
548
+
536
549
await intermediateProxy . forAnyRequest ( ) . thenPassThrough ( {
537
550
ignoreHostHttpsErrors : true ,
538
551
beforeRequest : ( req ) => {
539
552
expect ( req . url ) . to . equal ( 'https://example.com/' ) ;
553
+ return {
554
+ response : {
555
+ statusCode : 200 ,
556
+ body : 'Proxied'
557
+ }
558
+ } ;
540
559
}
541
560
} ) ;
542
-
543
- // make a request
544
- await request . get ( 'https://example.com/' ) ;
561
+
562
+ // make a request that hits the proxy based on PAC file
563
+ expect ( await request . get ( 'https://example.com/' ) ) . to . equal ( 'Proxied ') ;
545
564
} ) ;
546
565
} ) ;
547
566
0 commit comments