@@ -2,7 +2,7 @@ import _ = require("lodash");
2
2
import * as fs from 'fs/promises' ;
3
3
import request = require( "request-promise-native" ) ;
4
4
5
- import { getLocal , Mockttp , MockedEndpoint } from "../../.." ;
5
+ import { getLocal , Mockttp , MockedEndpoint , getAdminServer , getRemote } from "../../.." ;
6
6
import {
7
7
expect ,
8
8
nodeOnly
@@ -352,8 +352,107 @@ nodeOnly(() => {
352
352
// And it went via the intermediate proxy
353
353
expect ( ( await proxyEndpoint . getSeenRequests ( ) ) . length ) . to . equal ( 1 ) ;
354
354
} ) ;
355
+ } ) ;
356
+
357
+ describe ( "with a remote client" , ( ) => {
358
+
359
+ const adminServer = getAdminServer ( ) ;
360
+
361
+ before ( ( ) => adminServer . start ( ) ) ;
362
+ after ( ( ) => adminServer . stop ( ) ) ;
363
+
364
+ beforeEach ( async ( ) => {
365
+ server = getRemote ( ) ;
366
+ await server . start ( ) ;
367
+
368
+ // Configure Request to use the *first* server as a proxy
369
+ process . env = _ . merge ( { } , process . env , server . proxyEnv ) ;
370
+ } ) ;
371
+
372
+ describe ( "to an HTTPS proxy" , ( ) => {
373
+
374
+ const intermediateProxy = getLocal ( {
375
+ https : {
376
+ keyPath : './test/fixtures/untrusted-ca.key' ,
377
+ certPath : './test/fixtures/untrusted-ca.pem'
378
+ }
379
+ } ) ;
380
+ // HTTPS proxy - note that the remote server is plain HTTP.
381
+
382
+ let proxyEndpoint : MockedEndpoint ;
383
+
384
+ beforeEach ( async ( ) => {
385
+ await intermediateProxy . start ( ) ;
386
+ proxyEndpoint = await intermediateProxy . forAnyRequest ( ) . thenPassThrough ( ) ; // Totally neutral proxy
387
+ } ) ;
388
+
389
+ afterEach ( ( ) => intermediateProxy . stop ( ) ) ;
390
+
391
+ it ( "should not trust unknown proxy CAs by default" , async ( ) => {
392
+ // Remote server sends fixed response on this one URL:
393
+ await remoteServer . forGet ( '/test-url' ) . thenReply ( 200 , "Remote server says hi!" ) ;
394
+
395
+ // Mockttp forwards requests via our intermediate proxy
396
+ await server . forAnyRequest ( ) . thenPassThrough ( {
397
+ proxyConfig : {
398
+ proxyUrl : intermediateProxy . url
399
+ }
400
+ } ) ;
401
+
402
+ const result = await request . get ( remoteServer . urlFor ( "/test-url" ) ) . catch ( e => e ) ;
403
+
404
+ expect ( result ) . to . be . instanceOf ( Error ) ;
405
+ expect ( result . message ) . to . match ( / s e l f ( - | ) s i g n e d c e r t i f i c a t e / ) ; // Dash varies by Node version
406
+ } ) ;
407
+
408
+ it ( "should trust the remote proxy's CA if explicitly specified" , async ( ) => {
409
+ // Remote server sends fixed response on this one URL:
410
+ await remoteServer . forGet ( '/test-url' ) . thenReply ( 200 , "Remote server says hi!" ) ;
411
+
412
+ // Mockttp forwards requests via our intermediate proxy
413
+ await server . forAnyRequest ( ) . thenPassThrough ( {
414
+ proxyConfig : {
415
+ proxyUrl : intermediateProxy . url ,
416
+ trustedCAs : [
417
+ { cert : await fs . readFile ( './test/fixtures/untrusted-ca.pem' ) }
418
+ ]
419
+ }
420
+ } ) ;
421
+
422
+ const response = await request . get ( remoteServer . urlFor ( "/test-url" ) ) ;
423
+
424
+ // We get a successful response
425
+ expect ( response ) . to . equal ( "Remote server says hi!" ) ;
426
+ // And it went via the intermediate proxy
427
+ expect ( ( await proxyEndpoint . getSeenRequests ( ) ) . length ) . to . equal ( 1 ) ;
428
+ } ) ;
429
+
430
+ it ( "should trust the remote proxy's CA if explicitly specified as additional" , async ( ) => {
431
+ // Remote server sends fixed response on this one URL:
432
+ await remoteServer . forGet ( '/test-url' ) . thenReply ( 200 , "Remote server says hi!" ) ;
433
+
434
+ // Mockttp forwards requests via our intermediate proxy
435
+ await server . forAnyRequest ( ) . thenPassThrough ( {
436
+ proxyConfig : {
437
+ proxyUrl : intermediateProxy . url ,
438
+ additionalTrustedCAs : [
439
+ { cert : ( await fs . readFile ( './test/fixtures/untrusted-ca.pem' ) ) . toString ( ) }
440
+ ]
441
+ }
442
+ } ) ;
443
+
444
+ const response = await request . get ( remoteServer . urlFor ( "/test-url" ) ) ;
445
+
446
+ // We get a successful response
447
+ expect ( response ) . to . equal ( "Remote server says hi!" ) ;
448
+ // And it went via the intermediate proxy
449
+ expect ( ( await proxyEndpoint . getSeenRequests ( ) ) . length ) . to . equal ( 1 ) ;
450
+ } ) ;
451
+
452
+ } ) ;
355
453
356
454
} ) ;
357
455
358
456
} ) ;
457
+
359
458
} ) ;
0 commit comments