9
9
} from "../../dist/lib/http-proxy/passes/web-incoming" ;
10
10
import * as httpProxy from "../.." ;
11
11
import * as http from "http" ;
12
- // import * as concat from "concat-stream";
13
- // import * as async from "async";
12
+ import concat from "concat-stream" ;
13
+ import * as async from "async" ;
14
14
import getPort from "../get-port" ;
15
15
16
16
describe ( "#deleteLength" , ( ) => {
@@ -81,11 +81,15 @@ describe("#XHeaders", () => {
81
81
} ) ;
82
82
83
83
const ports : { [ port : string ] : number } = { } ;
84
- function address ( port : number ) {
85
- if ( ports [ port ] == null ) {
86
- throw Error ( `invalid port ${ port } ` ) ;
84
+ function address ( p : number | string ) {
85
+ return `http://127.0.0.1:${ port ( p ) } ` ;
86
+ }
87
+ function port ( p : number | string ) {
88
+ const x = ports [ p ] ;
89
+ if ( x == null ) {
90
+ throw Error ( `invalid port ${ p } ` ) ;
87
91
}
88
- return `http://127.0.0.1: ${ ports [ port ] } ` ;
92
+ return x ;
89
93
}
90
94
91
95
describe ( "#createProxyServer.web() using own http server" , ( ) => {
@@ -383,17 +387,17 @@ describe("#createProxyServer.web() using own http server", () => {
383
387
} ) ;
384
388
req . end ( ) ;
385
389
} ) ;
386
- /*
387
390
388
391
it ( "should proxy the request and provide a proxyRes event with the request and response parameters" , ( done ) => {
389
392
const proxy = httpProxy . createProxyServer ( {
390
393
target : address ( 8080 ) ,
391
394
} ) ;
392
395
393
396
function requestHandler ( req , res ) {
394
- proxy.once("proxyRes", (_proxyRes , pReq, pRes) => {
397
+ proxy . once ( "proxyRes" , ( proxyRes , pReq , pRes ) => {
395
398
source . close ( ) ;
396
399
proxyServer . close ( ) ;
400
+ expect ( proxyRes != null ) . toBe ( true ) ;
397
401
expect ( pReq ) . toEqual ( req ) ;
398
402
expect ( pRes ) . toEqual ( res ) ;
399
403
done ( ) ;
@@ -408,9 +412,9 @@ describe("#createProxyServer.web() using own http server", () => {
408
412
res . end ( "Response" ) ;
409
413
} ) ;
410
414
411
- proxyServer.listen(" 8086" );
412
- source.listen(ports[' 8080'] );
413
- http.request("http://127.0.0.1: 8086" , () => {}).end();
415
+ proxyServer . listen ( port ( 8086 ) ) ;
416
+ source . listen ( port ( 8080 ) ) ;
417
+ http . request ( address ( 8086 ) , ( ) => { } ) . end ( ) ;
414
418
} ) ;
415
419
416
420
it ( "should proxy the request and provide and respond to manual user response when using modifyResponse" , ( done ) => {
@@ -440,12 +444,12 @@ describe("#createProxyServer.web() using own http server", () => {
440
444
441
445
async . parallel (
442
446
[
443
- (next) => proxyServer.listen(8086, next),
444
- (next) => source.listen(8080, next),
447
+ ( next ) => proxyServer . listen ( port ( 8086 ) , next ) ,
448
+ ( next ) => source . listen ( port ( 8080 ) , next ) ,
445
449
] ,
446
450
( _err ) => {
447
451
http
448
- .get("http://127.0.0.1: 8086" , (res) => {
452
+ . get ( address ( 8086 ) , ( res ) => {
449
453
res . pipe (
450
454
concat ( ( body ) => {
451
455
expect ( body . toString ( "utf8" ) ) . toEqual ( "my-custom-response" ) ;
@@ -455,50 +459,47 @@ describe("#createProxyServer.web() using own http server", () => {
455
459
} ) ,
456
460
) ;
457
461
} )
458
- .once("error", done);
462
+ . once ( "error" , ( err ) => {
463
+ source . close ( ) ;
464
+ proxyServer . close ( ) ;
465
+ done ( err ) ;
466
+ } ) ;
459
467
} ,
460
468
) ;
461
469
} ) ;
462
470
463
471
it ( "should proxy the request and handle changeOrigin option" , ( done ) => {
464
- const proxy = httpProxy.createProxyServer({
465
- target: address(8080),
466
- changeOrigin: true,
467
- });
468
-
469
- function requestHandler(req, res) {
470
- proxy.web(req, res);
471
- }
472
-
473
- const proxyServer = http.createServer(requestHandler);
474
-
475
- const source = http.createServer((req, _res) => {
476
- source.close();
477
- proxyServer.close();
478
- expect(req.method).toEqual("GET");
479
- expect(req.headers.host?.split(":")[1]).toEqual("8080");
480
- done();
481
- });
472
+ const proxy = httpProxy
473
+ . createProxyServer ( {
474
+ target : address ( 8080 ) ,
475
+ changeOrigin : true ,
476
+ } )
477
+ . listen ( port ( 8081 ) ) ;
482
478
483
- proxyServer.listen(ports['8081']);
484
- source.listen(ports['8080']);
479
+ const source = http
480
+ . createServer ( ( req , res ) => {
481
+ source . close ( ) ;
482
+ proxy . close ( ) ;
483
+ expect ( req . method ) . toEqual ( "GET" ) ;
484
+ expect ( req . headers . host ?. split ( ":" ) [ 1 ] ) . toEqual ( `${ port ( 8080 ) } ` ) ;
485
+ res . end ( ) ;
486
+ done ( ) ;
487
+ } )
488
+ . listen ( port ( 8080 ) ) ;
485
489
486
- http.request(address(8081), () => {}).end();
490
+ const client = http . request ( address ( 8081 ) , ( ) => { } ) ;
491
+ client . on ( "error" , ( ) => { } ) ;
492
+ client . end ( ) ;
487
493
} ) ;
488
494
489
495
it ( "should proxy the request with the Authorization header set" , ( done ) => {
490
496
const proxy = httpProxy . createProxyServer ( {
491
497
target : address ( 8080 ) ,
492
498
auth : "user:pass" ,
493
499
} ) ;
500
+ const proxyServer = http . createServer ( proxy . web ) ;
494
501
495
- function requestHandler(req, res) {
496
- proxy.web(req, res);
497
- }
498
-
499
- const proxyServer = http.createServer(requestHandler);
500
-
501
- const source = http.createServer((req, _res) => {
502
+ const source = http . createServer ( ( req , res ) => {
502
503
source . close ( ) ;
503
504
proxyServer . close ( ) ;
504
505
const auth = new Buffer (
@@ -507,11 +508,12 @@ describe("#createProxyServer.web() using own http server", () => {
507
508
) ;
508
509
expect ( req . method ) . toEqual ( "GET" ) ;
509
510
expect ( auth . toString ( ) ) . toEqual ( "user:pass" ) ;
511
+ res . end ( ) ;
510
512
done ( ) ;
511
513
} ) ;
512
514
513
- proxyServer.listen(ports[' 8081'] );
514
- source.listen(ports[' 8080'] );
515
+ proxyServer . listen ( port ( 8081 ) ) ;
516
+ source . listen ( port ( 8080 ) ) ;
515
517
516
518
http . request ( address ( 8081 ) , ( ) => { } ) . end ( ) ;
517
519
} ) ;
@@ -530,37 +532,38 @@ describe("#createProxyServer.web() using own http server", () => {
530
532
} ) ;
531
533
} else {
532
534
proxy . web ( req , res , {
533
- target: "http://127.0.0.1: 8082" ,
535
+ target : address ( 8082 ) ,
534
536
} ) ;
535
537
}
536
538
}
537
539
538
540
const proxyServer = http . createServer ( requestHandler ) ;
539
541
540
- const source1 = http.createServer((req, _res ) => {
542
+ const source1 = http . createServer ( ( req , res ) => {
541
543
expect ( req . method ) . toEqual ( "GET" ) ;
542
- expect(req.headers.host?.split(":")[1]).toEqual(" 8080" );
544
+ expect ( req . headers . host ?. split ( ":" ) [ 1 ] ) . toEqual ( ` ${ port ( 8080 ) } ` ) ;
543
545
expect ( req . url ) . toEqual ( "/test1" ) ;
546
+ res . end ( ) ;
544
547
} ) ;
545
548
546
- const source2 = http.createServer((req, _res ) => {
549
+ const source2 = http . createServer ( ( req , res ) => {
547
550
source1 . close ( ) ;
548
551
source2 . close ( ) ;
549
552
proxyServer . close ( ) ;
550
553
expect ( req . method ) . toEqual ( "GET" ) ;
551
- expect(req.headers.host?.split(":")[1]).toEqual(" 8080" );
554
+ expect ( req . headers . host ?. split ( ":" ) [ 1 ] ) . toEqual ( ` ${ port ( 8080 ) } ` ) ;
552
555
expect ( req . url ) . toEqual ( "/test2" ) ;
556
+ res . end ( ) ;
553
557
done ( ) ;
554
558
} ) ;
555
559
556
- proxyServer.listen(ports[' 8080'] );
557
- source1.listen(ports[' 8081'] );
558
- source2.listen(" 8082" );
560
+ proxyServer . listen ( port ( 8080 ) ) ;
561
+ source1 . listen ( port ( 8081 ) ) ;
562
+ source2 . listen ( port ( 8082 ) ) ;
559
563
560
- http.request("http://127.0.0.1: 8080/s1/test1" , () => {}).end();
561
- http.request("http://127.0.0.1: 8080/test2" , () => {}).end();
564
+ http . request ( ` ${ address ( 8080 ) } /s1/test1` , ( ) => { } ) . end ( ) ;
565
+ http . request ( ` ${ address ( 8080 ) } /test2` , ( ) => { } ) . end ( ) ;
562
566
} ) ;
563
- */
564
567
} ) ;
565
568
566
569
/*
0 commit comments