1
+ var httpProxy = require ( '../lib/http-proxy' ) ,
2
+ expect = require ( 'expect.js' ) ,
3
+ http = require ( 'http' ) ;
4
+
5
+
6
+ describe ( 'lib/http-proxy.js' , function ( ) {
7
+
8
+ describe ( '#createProxyServer with target options for integration into existing server' , function ( ) {
9
+ it ( 'should proxy the request using the web proxy handler' , function ( done ) {
10
+ var proxy = httpProxy . createProxyServer ( {
11
+ target : 'http://127.0.0.1:8080'
12
+ } ) ;
13
+
14
+ function requestHandler ( req , res ) {
15
+ proxy . web ( req , res ) ;
16
+ }
17
+
18
+ var proxyServer = http . createServer ( requestHandler ) ;
19
+
20
+ var source = http . createServer ( function ( req , res ) {
21
+ source . close ( ) ;
22
+ proxyServer . close ( ) ;
23
+ expect ( req . method ) . to . eql ( 'GET' ) ;
24
+ expect ( req . headers . host . split ( ':' ) [ 1 ] ) . to . eql ( '8082' ) ;
25
+ done ( ) ;
26
+ } ) ;
27
+
28
+ proxyServer . listen ( '8082' ) ;
29
+ source . listen ( '8080' ) ;
30
+
31
+ http . request ( 'http://127.0.0.1:8082' , function ( ) { } ) . end ( ) ;
32
+ } ) ;
33
+ } ) ;
34
+
35
+ describe ( '#createProxyServer() for integration into existing server with error response' , function ( ) {
36
+ it ( 'should proxy the request and handle error via callback' , function ( done ) {
37
+ var proxy = httpProxy . createProxyServer ( {
38
+ target : 'http://127.0.0.1:8080'
39
+ } ) ;
40
+
41
+ var proxyServer = http . createServer ( requestHandler ) ;
42
+
43
+ function requestHandler ( req , res ) {
44
+ proxy . web ( req , res , function ( err ) {
45
+ proxyServer . close ( ) ;
46
+ expect ( err ) . to . be . an ( Error ) ;
47
+ expect ( err . code ) . to . be ( 'ECONNREFUSED' ) ;
48
+ done ( ) ;
49
+ } ) ;
50
+ }
51
+
52
+ proxyServer . listen ( '8082' ) ;
53
+
54
+ http . request ( {
55
+ hostname : '127.0.0.1' ,
56
+ port : '8082' ,
57
+ method : 'GET' ,
58
+ } , function ( ) { } ) . end ( ) ;
59
+ } ) ;
60
+
61
+ it ( 'should proxy the request and handle error event listener' , function ( done ) {
62
+ var proxy = httpProxy . createProxyServer ( {
63
+ target : 'http://127.0.0.1:8080'
64
+ } ) ;
65
+
66
+ var proxyServer = http . createServer ( requestHandler ) ;
67
+
68
+ function requestHandler ( req , res ) {
69
+ proxy . once ( 'error' , function ( err , errReq , errRes ) {
70
+ proxyServer . close ( ) ;
71
+ expect ( err ) . to . be . an ( Error ) ;
72
+ expect ( errReq ) . to . be . equal ( req ) ;
73
+ expect ( errRes ) . to . be . equal ( res ) ;
74
+ expect ( err . code ) . to . be ( 'ECONNREFUSED' ) ;
75
+ done ( ) ;
76
+ } ) ;
77
+
78
+ proxy . web ( req , res ) ;
79
+ }
80
+
81
+ proxyServer . listen ( '8082' ) ;
82
+
83
+ http . request ( {
84
+ hostname : '127.0.0.1' ,
85
+ port : '8082' ,
86
+ method : 'GET' ,
87
+ } , function ( ) { } ) . end ( ) ;
88
+ } ) ;
89
+
90
+ } ) ;
91
+
92
+ } ) ;
0 commit comments