1
1
var httpProxy = require ( '../lib/http-proxy/passes/ws-incoming' ) ,
2
- expect = require ( 'expect.js' ) ;
2
+ expect = require ( 'expect.js' ) ,
3
+ http = require ( 'http' ) ,
4
+ httpProxyLib = require ( '../lib/http-proxy' ) ;
3
5
4
6
describe ( 'lib/http-proxy/passes/ws-incoming.js' , function ( ) {
5
7
describe ( '#checkMethodAndHeader' , function ( ) {
@@ -9,7 +11,7 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () {
9
11
method : 'DELETE' ,
10
12
headers : { }
11
13
} ,
12
- stubSocket = {
14
+ stubSocket = {
13
15
destroy : function ( ) {
14
16
// Simulate Socket.destroy() method when call
15
17
destroyCalled = true ;
@@ -74,6 +76,62 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () {
74
76
expect ( returnValue ) . to . be ( undefined ) ;
75
77
expect ( destroyCalled ) . to . be ( false ) ;
76
78
} )
79
+
80
+
81
+ it ( 'should detect a proxyWsReq event and modify headers' , function ( done ) {
82
+ var proxy = httpProxyLib . createProxyServer ( {
83
+ target : 'http://127.0.0.1:8080' ,
84
+ ws : true
85
+ } ) ;
86
+
87
+ proxy . on ( 'proxyWsReq' , function ( proxyWsReq , req , res , options ) {
88
+ proxyWsReq . setHeader ( 'X-Special-Proxy-Header' , 'foobar' ) ;
89
+ } ) ;
90
+
91
+ function requestHandler ( req , res ) {
92
+ proxy . web ( req , res ) ;
93
+ }
94
+
95
+ var proxyServer = http . createServer ( requestHandler ) ;
96
+
97
+ proxyServer . on ( 'upgrade' , function ( req , res , head ) {
98
+ proxy . ws ( req , res , head ) ;
99
+ } ) ;
100
+
101
+ var source = http . createServer ( function ( req , res ) {
102
+ res . end ( ) ;
103
+ } ) ;
104
+
105
+ source . on ( 'upgrade' , function ( req , res , head ) {
106
+ expect ( req . headers [ 'x-special-proxy-header' ] ) . to . eql ( 'foobar' ) ;
107
+ source . close ( ) ;
108
+ proxyServer . close ( ) ;
109
+ done ( ) ;
110
+ } ) ;
111
+
112
+ source . on ( 'error' , function ( ) { } ) ;
113
+ proxy . on ( 'error' , function ( ) { } ) ;
114
+ proxyServer . on ( 'error' , function ( ) { } ) ;
115
+
116
+ proxyServer . listen ( '8081' ) ;
117
+ source . listen ( '8080' ) ;
118
+
119
+ var request = http . request ( {
120
+ host : '127.0.0.1' ,
121
+ port : '8081' ,
122
+ method : 'GET' ,
123
+ path : '/' ,
124
+ headers : {
125
+ 'upgrade' : 'websocket' ,
126
+ 'connection' : 'Upgrade' ,
127
+ 'sec-websocket-key' : 'dGhlIHNhbXBsZSBub25jZQ==' ,
128
+ 'sec-websocket-protocol' : 'chat, superchat' ,
129
+ 'sec-websocket-version' : 13
130
+ }
131
+ } , function ( ) { } ) ;
132
+ request . on ( 'error' , function ( ) { } ) ;
133
+ request . end ( ) ;
134
+ } ) ;
77
135
} ) ;
78
136
79
137
describe ( '#XHeaders' , function ( ) {
0 commit comments