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,97 @@ 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
+ }
494
+ } ) ;
495
+
496
+ // make request
497
+ await request . get ( 'https://example.com/' ) ;
498
+ } ) ;
499
+
500
+ 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 } "; }` ;
502
+ await remoteServer . forGet ( '/proxy-bypass' ) . thenReply ( 200 , pacFile ) ;
503
+
504
+ await server . forAnyRequest ( ) . thenPassThrough ( {
505
+ ignoreHostHttpsErrors : true ,
506
+ proxyConfig : {
507
+ proxyUrl : `pac+${ remoteServer . url } /proxy-bypass`
508
+ }
509
+ } ) ;
510
+
511
+ await intermediateProxy . forAnyRequest ( ) . thenPassThrough ( {
512
+ ignoreHostHttpsErrors : true ,
513
+ beforeRequest : ( req ) => {
514
+ expect ( req . url ) . to . not . equal ( 'https://example.org/' ) ;
515
+ }
516
+ } ) ;
517
+
518
+ // make a request that hits the proxy based on PAC file
519
+ await request . get ( 'https://example.com/' ) ;
520
+
521
+ // make a request that bypasses proxy based on PAC file
522
+ await request . get ( 'https://example.org/' ) ;
523
+ } ) ;
524
+
525
+ it ( "should fallback to intermediateProxy using PAC file" , async ( ) => {
526
+ const pacFile = `function FindProxyForURL(url, host) { return "PROXY invalid-proxy:8080; PROXY ${ url . parse ( intermediateProxy . url ) . host } ;"; }` ;
527
+ await remoteServer . forGet ( '/proxy-fallback' ) . thenReply ( 200 , pacFile ) ;
528
+
529
+ await server . forAnyRequest ( ) . thenPassThrough ( {
530
+ ignoreHostHttpsErrors : true ,
531
+ proxyConfig : {
532
+ proxyUrl : `pac+${ remoteServer . url } /proxy-fallback`
533
+ }
534
+ } ) ;
535
+
536
+ await intermediateProxy . forAnyRequest ( ) . thenPassThrough ( {
537
+ ignoreHostHttpsErrors : true ,
538
+ beforeRequest : ( req ) => {
539
+ expect ( req . url ) . to . equal ( 'https://example.com/' ) ;
540
+ }
541
+ } ) ;
542
+
543
+ // make a request
544
+ await request . get ( 'https://example.com/' ) ;
545
+ } ) ;
546
+ } ) ;
547
+
456
548
} ) ;
457
549
458
550
} ) ;
0 commit comments