Skip to content

Commit 626cba7

Browse files
author
Martinez, Andrew
committed
resolves http-party#882
Adds a proxyWsReq event that can be used to modify the outgoing websocket handshake.
1 parent 30e3b37 commit 626cba7

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

lib/http-proxy/passes/ws-incoming.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ var passes = exports;
140140
server.emit('proxySocket', proxySocket); //DEPRECATED.
141141
});
142142

143+
server.emit('proxyWsReq', proxyReq, req, socket);
143144
return proxyReq.end(); // XXX: CHECK IF THIS IS THIS CORRECT
144145

145146
function onOutgoingError(err) {

test/lib-http-proxy-passes-ws-incoming-test.js

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
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');
35

46
describe('lib/http-proxy/passes/ws-incoming.js', function () {
57
describe('#checkMethodAndHeader', function () {
@@ -9,7 +11,7 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () {
911
method: 'DELETE',
1012
headers: {}
1113
},
12-
stubSocket = {
14+
stubSocket = {
1315
destroy: function () {
1416
// Simulate Socket.destroy() method when call
1517
destroyCalled = true;
@@ -74,6 +76,62 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () {
7476
expect(returnValue).to.be(undefined);
7577
expect(destroyCalled).to.be(false);
7678
})
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+
});
77135
});
78136

79137
describe('#XHeaders', function () {

0 commit comments

Comments
 (0)