Skip to content

Commit

Permalink
[test fix] A few minor fixes to ensure basic WebSocket tests are work…
Browse files Browse the repository at this point in the history
…ing. Better scope tests by supported protocol
  • Loading branch information
indexzero committed Aug 28, 2011
1 parent be4562d commit daf9231
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 61 deletions.
6 changes: 3 additions & 3 deletions lib/node-http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ exports.createServer = function () {
case 'function': callback = arg; break;
};
});

if (!host && !port && !options) {
//
// If `host`, `port` and `options` are all not passed, then
Expand Down Expand Up @@ -285,12 +285,12 @@ exports._getProtocol = function _getProtocol (options) {
// containing the relevant `ca`, `key`, and `cert` properties.
//
exports._getBase = function _getBase (options) {
var result = {};
var result = function () {};

if (options.https && typeof options.https === 'object') {
['ca', 'cert', 'key'].forEach(function (key) {
if (options.https[key]) {
result[key] = options.https[key];
result.prototype[key] = options.https[key];
}
});
}
Expand Down
15 changes: 8 additions & 7 deletions lib/node-http-proxy/http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ var HttpProxy = exports.HttpProxy = function (options) {
// be provided or the operation will fail with an `origin mismatch`
// by definition.
//
this.source = options.source || { host: 'localhost', port: 8000 };
this.source = options.source || { host: 'localhost', port: 8000 };
this.source.https = this.source.https || options.https;
this.changeOrigin = options.changeOrigin || false;
};

Expand All @@ -114,7 +115,7 @@ util.inherits(HttpProxy, events.EventEmitter);
HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
var self = this,
errState = false,
outgoing = new Object(this.target.base),
outgoing = new(this.target.base),
reverseProxy;

//
Expand Down Expand Up @@ -286,7 +287,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
//
HttpProxy.prototype._forwardRequest = function (req) {
var self = this,
outgoing = new Object(this.forward.base),
outgoing = new(this.forward.base),
forwardProxy;

//
Expand Down Expand Up @@ -336,7 +337,7 @@ HttpProxy.prototype._forwardRequest = function (req) {
};

//
// ### function proxyWebSocketRequest (req, socket, head, options)
// ### function proxyWebSocketRequest (req, socket, head, buffer)
// #### @req {ServerRequest} Websocket request to proxy.
// #### @socket {net.Socket} Socket for the underlying HTTP request
// #### @head {string} Headers for the Websocket request.
Expand All @@ -346,7 +347,7 @@ HttpProxy.prototype._forwardRequest = function (req) {
//
HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer) {
var self = this,
outgoing = new Object(this.target.base);
outgoing = new(this.target.base);
listeners = {},
errState = false,
CRLF = '\r\n';
Expand Down Expand Up @@ -478,7 +479,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
var agent = this.target.agent,
protocolName = this.target.https ? 'https' : 'http',
portUri = getPort(this.source.port),
remoteHost = options.host + portUri;
remoteHost = this.target.host + portUri;

//
// Change headers (if requested).
Expand Down Expand Up @@ -567,7 +568,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
// Get the Non-Printable data
data = data.slice(Buffer.byteLength(sdata), data.length);

if (self.https && !self.target.https) {
if (self.source.https && !self.target.https) {
//
// If the proxy server is running HTTPS but the client is running
// HTTP then replace `ws` with `wss` in the data sent back to the client.
Expand Down
1 change: 0 additions & 1 deletion test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ TestRunner.prototype.assertResponseCode = function (proxyPort, statusCode, creat
//
// WebSocketTest
//

TestRunner.prototype.webSocketTest = function (options) {
var self = this;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ var assert = require('assert'),
argv = require('optimist').argv,
request = require('request'),
vows = require('vows'),
helpers = require('./helpers'),
TestRunner = helpers.TestRunner;
helpers = require('../helpers');

var protocol = argv.https ? 'https' : 'http',
runner = new TestRunner(protocol),
runner = new helpers.TestRunner(protocol),
routeFile = path.join(__dirname, 'config.json');

var fileOptions = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var assert = require('assert'),
argv = require('optimist').argv,
request = require('request'),
vows = require('vows'),
helpers = require('./helpers');
helpers = require('../helpers');

var forwardOptions = {
forward: {
Expand Down
104 changes: 104 additions & 0 deletions test/websocket/websocket-proxy-table-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
node-http-proxy-test.js: http proxy for node.js
Copyright (c) 2010 Charlie Robbins, Marak Squires and Fedor Indutny
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

var util = require('util'),
assert = require('assert'),
argv = require('optimist').argv,
colors = require('colors'),
request = require('request'),
vows = require('vows'),
websocket = require('../vendor/websocket'),
helpers = require('../helpers');

try {
var utils = require('socket.io/lib/socket.io/utils'),
io = require('socket.io');
}
catch (ex) {
console.error('Socket.io is required for this example:');
console.error('npm ' + 'install'.green + ' socket.io@0.6.18'.magenta);
process.exit(1);
}

var protocol = argv.https ? 'https' : 'http',
wsprotocol = argv.https ? 'wss' : 'ws',
runner = new helpers.TestRunner(protocol);

vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
"When using server created by httpProxy.createServer()": {
"using proxy table with no latency": {
"when an inbound message is sent from a WebSocket client": {
topic: function () {
var that = this
headers = {};

runner.webSocketTestWithTable({
io: io,
host: 'localhost',
wsprotocol: wsprotocol,
protocol: protocol,
router: {'localhost':'localhost:8230'},
ports: {
target: 8230,
proxy: 8231
},
onListen: function (socket) {
socket.on('connection', function (client) {
client.on('message', function (msg) {
that.callback(null, msg, headers);
});
});
},
onWsupgrade: function (req, res) {
headers.request = req;
headers.response = res.headers;
},
onOpen: function (ws) {
ws.send(utils.encode('from client'));
}
});
},
"the target server should receive the message": function (err, msg, headers) {
assert.equal(msg, 'from client');
},
"the origin and sec-websocket-origin headers should match": function (err, msg, headers) {
assert.isString(headers.response['sec-websocket-location']);
assert.isTrue(headers.response['sec-websocket-location'].indexOf(wsprotocol) !== -1);
assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']);
}
}
}
}
}).addBatch({
"When the tests are over": {
topic: function () {
return runner.closeServers();
},
"the servers should clean up": function () {
assert.isTrue(true);
}
}
}).export(module);
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ var util = require('util'),
colors = require('colors'),
request = require('request'),
vows = require('vows'),
websocket = require('./../vendor/websocket'),
helpers = require('./helpers');
websocket = require('../../vendor/websocket'),
helpers = require('../helpers');

try {
var utils = require('socket.io/lib/socket.io/utils'),
Expand All @@ -48,50 +48,6 @@ var protocol = argv.https ? 'https' : 'http',
runner = new helpers.TestRunner(protocol);

vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
"when using proxy table":{
"with no latency" : {
"when an inbound message is sent from a WebSocket client": {
topic: function () {
var that = this
headers = {};

runner.webSocketTestWithTable({
io: io,
host: 'localhost',
wsprotocol: wsprotocol,
protocol: protocol,
router: {'localhost':'localhost:8230'},
ports: {
target: 8230,
proxy: 8231
},
onListen: function (socket) {
socket.on('connection', function (client) {
client.on('message', function (msg) {
that.callback(null, msg, headers);
});
});
},
onWsupgrade: function (req, res) {
headers.request = req;
headers.response = res.headers;
},
onOpen: function (ws) {
ws.send(utils.encode('from client'));
}
});
},
"the target server should receive the message": function (err, msg, headers) {
assert.equal(msg, 'from client');
},
"the origin and sec-websocket-origin headers should match": function (err, msg, headers) {
assert.isString(headers.response['sec-websocket-location']);
assert.isTrue(headers.response['sec-websocket-location'].indexOf(wsprotocol) !== -1);
assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']);
}
}
}
},
"When using server created by httpProxy.createServer()": {
"with no latency" : {
"when an inbound message is sent from a WebSocket client": {
Expand Down

0 comments on commit daf9231

Please sign in to comment.