Skip to content

Commit

Permalink
[test] Updated tests to reflect finalized API of the RoutingProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Sep 10, 2011
1 parent f765f90 commit 734769f
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 55 deletions.
53 changes: 36 additions & 17 deletions test/helpers.js
Expand Up @@ -5,15 +5,16 @@
*
*/

var fs = require('fs'),
var assert = require('assert'),
fs = require('fs'),
http = require('http'),
https = require('https'),
path = require('path'),
vows = require('vows'),
assert = require('assert'),
argv = require('optimist').argv,
request = require('request'),
websocket = require('./../vendor/websocket'),
httpProxy = require('./../lib/node-http-proxy');
vows = require('vows'),
websocket = require('../vendor/websocket'),
httpProxy = require('../lib/node-http-proxy');

var loadHttps = exports.loadHttps = function () {
return {
Expand All @@ -22,16 +23,34 @@ var loadHttps = exports.loadHttps = function () {
};
};

var TestRunner = exports.TestRunner = function (source, target) {
this.source = { protocol: source },
this.target = { protocol: target };
var parseProtocol = exports.parseProtocol = function () {
function setupProtocol (secure) {
return {
secure: secure,
protocols: {
http: secure ? 'https' : 'http',
ws: secure ? 'wss' : 'ws'
}
}
}

return {
source: setupProtocol(argv.source === 'secure'),
target: setupProtocol(argv.target === 'secure')
};
}

var TestRunner = exports.TestRunner = function (options) {
options = options || {};
this.source = options.source || {};
this.target = options.target || {};
this.testServers = [];

if (source === 'https') {
if (this.source.secure) {
this.source.https = loadHttps();
}

if (target === 'https') {
if (this.target.secure) {
this.target.https = loadHttps();
}
};
Expand All @@ -48,7 +67,7 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx

options = {
method: 'GET',
uri: self.source.protocol + '://localhost:' + proxyPort,
uri: self.source.protocols.http + '://localhost:' + proxyPort,
headers: {
host: host
}
Expand Down Expand Up @@ -79,7 +98,7 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx

TestRunner.prototype.assertResponseCode = function (proxyPort, statusCode, createProxy) {
var assertion = "should receive " + statusCode + " responseCode",
protocol = this.source.protocol;
protocol = this.source.protocols.http;

var test = {
topic: function () {
Expand Down Expand Up @@ -240,21 +259,21 @@ TestRunner.prototype.startProxyServerWithTableAndLatency = function (port, laten
// Initialize the nodeProxy and start proxying the request
//
var that = this,
proxy = new httpProxy.HttpProxy(merge({}, options, this.getOptions())),
proxy = new httpProxy.RoutingProxy(merge({}, options, this.getOptions())),
proxyServer;

var handler = function (req, res) {
var buffer = proxy.buffer(req);
var buffer = httpProxy.buffer(req);
setTimeout(function () {
proxy.proxyRequest(req, res, {
buffer: buffer
});
}, latency);
};

proxyServer = that.options.https
? https.createServer(that.options.https, handler, that.options)
: http.createServer(handler, that.options);
proxyServer = this.source.https
? https.createServer(this.source.https, handler)
: http.createServer(handler);

proxyServer.listen(port, function () {
that.testServers.push(proxyServer);
Expand Down
9 changes: 4 additions & 5 deletions test/http/http-proxy-test.js
Expand Up @@ -26,7 +26,6 @@

var assert = require('assert'),
util = require('util'),
argv = require('optimist').argv,
request = require('request'),
vows = require('vows'),
helpers = require('../helpers');
Expand All @@ -45,11 +44,11 @@ var badForwardOptions = {
}
};

var protocol = argv.https ? 'https' : 'http',
target = argv.target ? argv.target : 'http',
runner = new helpers.TestRunner(protocol, target);
var options = helpers.parseProtocol(),
testName = [options.source.protocols.http, options.target.protocols.http].join('-to-'),
runner = new helpers.TestRunner(options);

vows.describe('node-http-proxy/' + protocol).addBatch({
vows.describe('node-http-proxy/http-proxy/' + testName).addBatch({
"When using server created by httpProxy.createServer()": {
"with no latency" : {
"and a valid target server": runner.assertProxied('localhost', 8080, 8081, function (callback) {
Expand Down
23 changes: 11 additions & 12 deletions test/http/routing-proxy-test.js
Expand Up @@ -14,8 +14,9 @@ var assert = require('assert'),
vows = require('vows'),
helpers = require('../helpers');

var protocol = argv.https ? 'https' : 'http',
runner = new helpers.TestRunner(protocol),
var options = helpers.parseProtocol(),
testName = [options.source.protocols.http, options.target.protocols.http].join('-to-'),
runner = new helpers.TestRunner(options),
routeFile = path.join(__dirname, 'config.json');

var fileOptions = {
Expand All @@ -40,7 +41,7 @@ var hostnameOptions = {
}
};

vows.describe('node-http-proxy/proxy-table/' + protocol).addBatch({
vows.describe('node-http-proxy/routing-proxy/' + testName).addBatch({
"When using server created by httpProxy.createServer()": {
"when passed a routing table": {
"and routing by RegExp": {
Expand Down Expand Up @@ -81,16 +82,14 @@ vows.describe('node-http-proxy/proxy-table/' + protocol).addBatch({
fs.writeFileSync(routeFile, JSON.stringify(config));

this.server.on('routes', function () {
var options = {
method: 'GET',
uri: protocol + '://localhost:8100',
headers: {
host: 'dynamic.com'
}
};

runner.startTargetServer(8103, 'hello dynamic.com', function () {
request(options, that.callback);
request({
method: 'GET',
uri: options.source.protocols.http + '://localhost:8100',
headers: {
host: 'dynamic.com'
}
}, that.callback);
});
});
},
Expand Down
24 changes: 12 additions & 12 deletions test/websocket/websocket-proxy-test.js
Expand Up @@ -43,11 +43,11 @@ catch (ex) {
process.exit(1);
}

var protocol = argv.https ? 'https' : 'http',
wsprotocol = argv.https ? 'wss' : 'ws',
runner = new helpers.TestRunner(protocol);
var options = helpers.parseProtocol(),
testName = [options.source.protocols.ws, options.target.protocols.ws].join('-to-'),
runner = new helpers.TestRunner(options);

vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
vows.describe('node-http-proxy/http-proxy/' + testName).addBatch({
"When using server created by httpProxy.createServer()": {
"with no latency" : {
"when an inbound message is sent from a WebSocket client": {
Expand All @@ -58,8 +58,8 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
runner.webSocketTest({
io: io,
host: 'localhost',
wsprotocol: wsprotocol,
protocol: protocol,
wsprotocol: options.source.protocols.ws,
protocol: options.source.protocols.http,
ports: {
target: 8130,
proxy: 8131
Expand All @@ -85,7 +85,7 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
},
"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.isTrue(headers.response['sec-websocket-location'].indexOf(options.source.protocols.ws) !== -1);
assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']);
}
},
Expand All @@ -97,8 +97,8 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
runner.webSocketTest({
io: io,
host: 'localhost',
wsprotocol: wsprotocol,
protocol: protocol,
wsprotocol: options.source.protocols.ws,
protocol: options.source.protocols.http,
ports: {
target: 8132,
proxy: 8133
Expand Down Expand Up @@ -126,8 +126,8 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
runner.webSocketTest({
io: io,
host: 'localhost',
wsprotocol: wsprotocol,
protocol: protocol,
wsprotocol: options.source.protocols.ws,
protocol: options.source.protocols.http,
ports: {
target: 8134,
proxy: 8135
Expand All @@ -154,7 +154,7 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
},
"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.isTrue(headers.response['sec-websocket-location'].indexOf(options.source.protocols.ws) !== -1);
assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']);
}
}
Expand Down
18 changes: 9 additions & 9 deletions test/websocket/websocket-routing-proxy-test.js
Expand Up @@ -30,7 +30,7 @@ var util = require('util'),
colors = require('colors'),
request = require('request'),
vows = require('vows'),
websocket = require('../vendor/websocket'),
websocket = require('../../vendor/websocket'),
helpers = require('../helpers');

try {
Expand All @@ -43,11 +43,11 @@ catch (ex) {
process.exit(1);
}

var protocol = argv.https ? 'https' : 'http',
wsprotocol = argv.https ? 'wss' : 'ws',
runner = new helpers.TestRunner(protocol);
var options = helpers.parseProtocol(),
testName = [options.source.protocols.ws, options.target.protocols.ws].join('-to-'),
runner = new helpers.TestRunner(options);

vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
vows.describe('node-http-proxy/routing-proxy/' + testName).addBatch({
"When using server created by httpProxy.createServer()": {
"using proxy table with no latency": {
"when an inbound message is sent from a WebSocket client": {
Expand All @@ -58,9 +58,9 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
runner.webSocketTestWithTable({
io: io,
host: 'localhost',
wsprotocol: wsprotocol,
protocol: protocol,
router: {'localhost':'localhost:8230'},
wsprotocol: options.source.protocols.ws,
protocol: options.source.protocols.http,
router: { 'localhost' : 'localhost:8230' },
ports: {
target: 8230,
proxy: 8231
Expand All @@ -86,7 +86,7 @@ vows.describe('node-http-proxy/websocket/' + wsprotocol).addBatch({
},
"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.isTrue(headers.response['sec-websocket-location'].indexOf(options.source.protocols.ws) !== -1);
assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']);
}
}
Expand Down

0 comments on commit 734769f

Please sign in to comment.