Skip to content

resolves #882 #883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/http-proxy/passes/ws-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ var passes = exports;
server.emit('proxySocket', proxySocket); //DEPRECATED.
});

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

function onOutgoingError(err) {
Expand Down
79 changes: 68 additions & 11 deletions test/lib-http-proxy-passes-ws-incoming-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var httpProxy = require('../lib/http-proxy/passes/ws-incoming'),
expect = require('expect.js');
var wsPasses = require('../lib/http-proxy/passes/ws-incoming'),
expect = require('expect.js'),
http = require('http'),
httpProxy = require('../lib/http-proxy');

describe('lib/http-proxy/passes/ws-incoming.js', function () {
describe('#checkMethodAndHeader', function () {
Expand All @@ -9,13 +11,13 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () {
method: 'DELETE',
headers: {}
},
stubSocket = {
stubSocket = {
destroy: function () {
// Simulate Socket.destroy() method when call
destroyCalled = true;
}
}
returnValue = httpProxy.checkMethodAndHeader(stubRequest, stubSocket);
returnValue = wsPasses.checkMethodAndHeader(stubRequest, stubSocket);
expect(returnValue).to.be(true);
expect(destroyCalled).to.be(true);
})
Expand All @@ -32,7 +34,7 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () {
destroyCalled = true;
}
}
returnValue = httpProxy.checkMethodAndHeader(stubRequest, stubSocket);
returnValue = wsPasses.checkMethodAndHeader(stubRequest, stubSocket);
expect(returnValue).to.be(true);
expect(destroyCalled).to.be(true);
})
Expand All @@ -51,7 +53,7 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () {
destroyCalled = true;
}
}
returnValue = httpProxy.checkMethodAndHeader(stubRequest, stubSocket);
returnValue = wsPasses.checkMethodAndHeader(stubRequest, stubSocket);
expect(returnValue).to.be(true);
expect(destroyCalled).to.be(true);
})
Expand All @@ -70,15 +72,70 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () {
destroyCalled = true;
}
}
returnValue = httpProxy.checkMethodAndHeader(stubRequest, stubSocket);
returnValue = wsPasses.checkMethodAndHeader(stubRequest, stubSocket);
expect(returnValue).to.be(undefined);
expect(destroyCalled).to.be(false);
})

it('should detect a proxyWsReq event and modify headers', function (done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080',
ws: true
});

proxy.on('proxyWsReq', function(proxyWsReq, req, res, options) {
proxyWsReq.setHeader('X-Special-Proxy-Header', 'foobar');
});

function requestHandler(req, res) {
proxy.web(req, res);
}

var proxyServer = http.createServer(requestHandler);

proxyServer.on('upgrade', function(req, res, head){
proxy.ws(req, res, head);
});

var source = http.createServer(function(req, res) {
res.end();
});

source.on('upgrade', function(req, res, head){
expect(req.headers['x-special-proxy-header']).to.eql('foobar');
source.close();
proxyServer.close();
done();
});

source.on('error', function(){});
proxy.on('error', function(){});
proxyServer.on('error', function(){});

proxyServer.listen('8081');
source.listen('8080');

var request = http.request({
host: '127.0.0.1',
port: '8081',
method: 'GET',
path: '/',
headers: {
'upgrade': 'websocket',
'connection': 'Upgrade',
'sec-websocket-key': 'dGhlIHNhbXBsZSBub25jZQ==',
'sec-websocket-protocol': 'chat, superchat',
'sec-websocket-version': 13
}
}, function() {});
request.on('error', function(){});
request.end();
});
});

describe('#XHeaders', function () {
it('return if no forward request', function () {
var returnValue = httpProxy.XHeaders({}, {}, {});
var returnValue = wsPasses.XHeaders({}, {}, {});
expect(returnValue).to.be(undefined);
});

Expand All @@ -91,8 +148,8 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () {
headers: {
host: '192.168.1.2:8080'
}
}
httpProxy.XHeaders(stubRequest, {}, { xfwd: true });
};
wsPasses.XHeaders(stubRequest, {}, { xfwd: true });
expect(stubRequest.headers['x-forwarded-for']).to.be('192.168.1.2');
expect(stubRequest.headers['x-forwarded-port']).to.be('8080');
expect(stubRequest.headers['x-forwarded-proto']).to.be('ws');
Expand All @@ -111,7 +168,7 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () {
host: '192.168.1.3:8181'
}
};
httpProxy.XHeaders(stubRequest, {}, { xfwd: true });
wsPasses.XHeaders(stubRequest, {}, { xfwd: true });
expect(stubRequest.headers['x-forwarded-for']).to.be('192.168.1.3');
expect(stubRequest.headers['x-forwarded-port']).to.be('8181');
expect(stubRequest.headers['x-forwarded-proto']).to.be('wss');
Expand Down