[test fix] A few minor fixes to ensure basic WebSocket tests are working. Better scope tests by supported protocol

This commit is contained in:
indexzero 2011-08-28 05:06:14 -04:00
parent be4562da9f
commit daf9231a66
7 changed files with 120 additions and 61 deletions

View File

@ -285,12 +285,12 @@ exports._getProtocol = function _getProtocol (options) {
// containing the relevant `ca`, `key`, and `cert` properties. // containing the relevant `ca`, `key`, and `cert` properties.
// //
exports._getBase = function _getBase (options) { exports._getBase = function _getBase (options) {
var result = {}; var result = function () {};
if (options.https && typeof options.https === 'object') { if (options.https && typeof options.https === 'object') {
['ca', 'cert', 'key'].forEach(function (key) { ['ca', 'cert', 'key'].forEach(function (key) {
if (options.https[key]) { if (options.https[key]) {
result[key] = options.https[key]; result.prototype[key] = options.https[key];
} }
}); });
} }

View File

@ -98,7 +98,8 @@ var HttpProxy = exports.HttpProxy = function (options) {
// be provided or the operation will fail with an `origin mismatch` // be provided or the operation will fail with an `origin mismatch`
// by definition. // 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; this.changeOrigin = options.changeOrigin || false;
}; };
@ -114,7 +115,7 @@ util.inherits(HttpProxy, events.EventEmitter);
HttpProxy.prototype.proxyRequest = function (req, res, buffer) { HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
var self = this, var self = this,
errState = false, errState = false,
outgoing = new Object(this.target.base), outgoing = new(this.target.base),
reverseProxy; reverseProxy;
// //
@ -286,7 +287,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
// //
HttpProxy.prototype._forwardRequest = function (req) { HttpProxy.prototype._forwardRequest = function (req) {
var self = this, var self = this,
outgoing = new Object(this.forward.base), outgoing = new(this.forward.base),
forwardProxy; forwardProxy;
// //
@ -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. // #### @req {ServerRequest} Websocket request to proxy.
// #### @socket {net.Socket} Socket for the underlying HTTP request // #### @socket {net.Socket} Socket for the underlying HTTP request
// #### @head {string} Headers for the Websocket request. // #### @head {string} Headers for the Websocket request.
@ -346,7 +347,7 @@ HttpProxy.prototype._forwardRequest = function (req) {
// //
HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer) { HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer) {
var self = this, var self = this,
outgoing = new Object(this.target.base); outgoing = new(this.target.base);
listeners = {}, listeners = {},
errState = false, errState = false,
CRLF = '\r\n'; CRLF = '\r\n';
@ -478,7 +479,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
var agent = this.target.agent, var agent = this.target.agent,
protocolName = this.target.https ? 'https' : 'http', protocolName = this.target.https ? 'https' : 'http',
portUri = getPort(this.source.port), portUri = getPort(this.source.port),
remoteHost = options.host + portUri; remoteHost = this.target.host + portUri;
// //
// Change headers (if requested). // Change headers (if requested).
@ -567,7 +568,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
// Get the Non-Printable data // Get the Non-Printable data
data = data.slice(Buffer.byteLength(sdata), data.length); 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 // 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. // HTTP then replace `ws` with `wss` in the data sent back to the client.

View File

@ -112,7 +112,6 @@ TestRunner.prototype.assertResponseCode = function (proxyPort, statusCode, creat
// //
// WebSocketTest // WebSocketTest
// //
TestRunner.prototype.webSocketTest = function (options) { TestRunner.prototype.webSocketTest = function (options) {
var self = this; var self = this;

View File

@ -12,11 +12,10 @@ var assert = require('assert'),
argv = require('optimist').argv, argv = require('optimist').argv,
request = require('request'), request = require('request'),
vows = require('vows'), vows = require('vows'),
helpers = require('./helpers'), helpers = require('../helpers');
TestRunner = helpers.TestRunner;
var protocol = argv.https ? 'https' : 'http', var protocol = argv.https ? 'https' : 'http',
runner = new TestRunner(protocol), runner = new helpers.TestRunner(protocol),
routeFile = path.join(__dirname, 'config.json'); routeFile = path.join(__dirname, 'config.json');
var fileOptions = { var fileOptions = {

View File

@ -29,7 +29,7 @@ var assert = require('assert'),
argv = require('optimist').argv, argv = require('optimist').argv,
request = require('request'), request = require('request'),
vows = require('vows'), vows = require('vows'),
helpers = require('./helpers'); helpers = require('../helpers');
var forwardOptions = { var forwardOptions = {
forward: { forward: {

View File

@ -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);

View File

@ -30,8 +30,8 @@ var util = require('util'),
colors = require('colors'), colors = require('colors'),
request = require('request'), request = require('request'),
vows = require('vows'), vows = require('vows'),
websocket = require('./../vendor/websocket'), websocket = require('../../vendor/websocket'),
helpers = require('./helpers'); helpers = require('../helpers');
try { try {
var utils = require('socket.io/lib/socket.io/utils'), var utils = require('socket.io/lib/socket.io/utils'),
@ -48,50 +48,6 @@ var protocol = argv.https ? 'https' : 'http',
runner = new helpers.TestRunner(protocol); runner = new helpers.TestRunner(protocol);
vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({ 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()": { "When using server created by httpProxy.createServer()": {
"with no latency" : { "with no latency" : {
"when an inbound message is sent from a WebSocket client": { "when an inbound message is sent from a WebSocket client": {