1
- var httpProxy = require ( '../lib/http-proxy/passes/web-incoming' ) ,
2
- expect = require ( 'expect.js' ) ;
1
+ var webPasses = require ( '../lib/http-proxy/passes/web-incoming' ) ,
2
+ httpProxy = require ( '../lib/http-proxy' ) ,
3
+ expect = require ( 'expect.js' ) ,
4
+ http = require ( 'http' ) ;
3
5
4
6
describe ( 'lib/http-proxy/passes/web.js' , function ( ) {
5
7
describe ( '#deleteLength' , function ( ) {
@@ -8,7 +10,7 @@ describe('lib/http-proxy/passes/web.js', function() {
8
10
method : 'DELETE' ,
9
11
headers : { }
10
12
} ;
11
- httpProxy . deleteLength ( stubRequest , { } , { } ) ;
13
+ webPasses . deleteLength ( stubRequest , { } , { } ) ;
12
14
expect ( stubRequest . headers [ 'content-length' ] ) . to . eql ( '0' ) ;
13
15
} )
14
16
} ) ;
@@ -21,7 +23,7 @@ describe('lib/http-proxy/passes/web.js', function() {
21
23
}
22
24
}
23
25
24
- httpProxy . timeout ( stubRequest , { } , { timeout : 5000 } ) ;
26
+ webPasses . timeout ( stubRequest , { } , { timeout : 5000 } ) ;
25
27
expect ( done ) . to . eql ( 5000 ) ;
26
28
} ) ;
27
29
} ) ;
@@ -36,10 +38,91 @@ describe('lib/http-proxy/passes/web.js', function() {
36
38
}
37
39
38
40
it ( 'set the correct x-forwarded-* headers' , function ( ) {
39
- httpProxy . XHeaders ( stubRequest , { } , { xfwd : true } ) ;
41
+ webPasses . XHeaders ( stubRequest , { } , { xfwd : true } ) ;
40
42
expect ( stubRequest . headers [ 'x-forwarded-for' ] ) . to . be ( '192.168.1.2' ) ;
41
43
expect ( stubRequest . headers [ 'x-forwarded-port' ] ) . to . be ( '8080' ) ;
42
44
expect ( stubRequest . headers [ 'x-forwarded-proto' ] ) . to . be ( 'http' ) ;
43
45
} ) ;
44
46
} ) ;
45
47
} ) ;
48
+
49
+ describe ( '#createProxyServer.web() using own http server' , function ( ) {
50
+ it ( 'should proxy the request using the web proxy handler' , function ( done ) {
51
+ var proxy = httpProxy . createProxyServer ( {
52
+ target : 'http://127.0.0.1:8080'
53
+ } ) ;
54
+
55
+ function requestHandler ( req , res ) {
56
+ proxy . web ( req , res ) ;
57
+ }
58
+
59
+ var proxyServer = http . createServer ( requestHandler ) ;
60
+
61
+ var source = http . createServer ( function ( req , res ) {
62
+ source . close ( ) ;
63
+ proxyServer . close ( ) ;
64
+ expect ( req . method ) . to . eql ( 'GET' ) ;
65
+ expect ( req . headers . host . split ( ':' ) [ 1 ] ) . to . eql ( '8081' ) ;
66
+ done ( ) ;
67
+ } ) ;
68
+
69
+ proxyServer . listen ( '8081' ) ;
70
+ source . listen ( '8080' ) ;
71
+
72
+ http . request ( 'http://127.0.0.1:8081' , function ( ) { } ) . end ( ) ;
73
+ } ) ;
74
+
75
+ it ( 'should proxy the request and handle error via callback' , function ( done ) {
76
+ var proxy = httpProxy . createProxyServer ( {
77
+ target : 'http://127.0.0.1:8080'
78
+ } ) ;
79
+
80
+ var proxyServer = http . createServer ( requestHandler ) ;
81
+
82
+ function requestHandler ( req , res ) {
83
+ proxy . web ( req , res , function ( err ) {
84
+ proxyServer . close ( ) ;
85
+ expect ( err ) . to . be . an ( Error ) ;
86
+ expect ( err . code ) . to . be ( 'ECONNREFUSED' ) ;
87
+ done ( ) ;
88
+ } ) ;
89
+ }
90
+
91
+ proxyServer . listen ( '8081' ) ;
92
+
93
+ http . request ( {
94
+ hostname : '127.0.0.1' ,
95
+ port : '8081' ,
96
+ method : 'GET' ,
97
+ } , function ( ) { } ) . end ( ) ;
98
+ } ) ;
99
+
100
+ it ( 'should proxy the request and handle error via event listener' , function ( done ) {
101
+ var proxy = httpProxy . createProxyServer ( {
102
+ target : 'http://127.0.0.1:8080'
103
+ } ) ;
104
+
105
+ var proxyServer = http . createServer ( requestHandler ) ;
106
+
107
+ function requestHandler ( req , res ) {
108
+ proxy . once ( 'error' , function ( err , errReq , errRes ) {
109
+ proxyServer . close ( ) ;
110
+ expect ( err ) . to . be . an ( Error ) ;
111
+ expect ( errReq ) . to . be . equal ( req ) ;
112
+ expect ( errRes ) . to . be . equal ( res ) ;
113
+ expect ( err . code ) . to . be ( 'ECONNREFUSED' ) ;
114
+ done ( ) ;
115
+ } ) ;
116
+
117
+ proxy . web ( req , res ) ;
118
+ }
119
+
120
+ proxyServer . listen ( '8081' ) ;
121
+
122
+ http . request ( {
123
+ hostname : '127.0.0.1' ,
124
+ port : '8081' ,
125
+ method : 'GET' ,
126
+ } , function ( ) { } ) . end ( ) ;
127
+ } ) ;
128
+ } ) ;
0 commit comments