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