From 734769fa9b2c3054d45e33c3e552af80ce3f4740 Mon Sep 17 00:00:00 2001 From: indexzero Date: Sat, 10 Sep 2011 03:55:35 -0400 Subject: [PATCH] [test] Updated tests to reflect finalized API of the RoutingProxy --- test/helpers.js | 53 +++++++++++++------ test/http/http-proxy-test.js | 9 ++-- test/http/routing-proxy-test.js | 23 ++++---- test/websocket/websocket-proxy-test.js | 24 ++++----- .../websocket/websocket-routing-proxy-test.js | 18 +++---- 5 files changed, 72 insertions(+), 55 deletions(-) diff --git a/test/helpers.js b/test/helpers.js index 1bc9915..b1bbd22 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -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 { @@ -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(); } }; @@ -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 } @@ -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 () { @@ -240,11 +259,11 @@ 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 @@ -252,9 +271,9 @@ TestRunner.prototype.startProxyServerWithTableAndLatency = function (port, laten }, 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); diff --git a/test/http/http-proxy-test.js b/test/http/http-proxy-test.js index 5437261..c202f97 100644 --- a/test/http/http-proxy-test.js +++ b/test/http/http-proxy-test.js @@ -26,7 +26,6 @@ var assert = require('assert'), util = require('util'), - argv = require('optimist').argv, request = require('request'), vows = require('vows'), helpers = require('../helpers'); @@ -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) { diff --git a/test/http/routing-proxy-test.js b/test/http/routing-proxy-test.js index 87cf6a8..1180fb7 100644 --- a/test/http/routing-proxy-test.js +++ b/test/http/routing-proxy-test.js @@ -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 = { @@ -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": { @@ -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); }); }); }, diff --git a/test/websocket/websocket-proxy-test.js b/test/websocket/websocket-proxy-test.js index bfc63e6..7d833f5 100644 --- a/test/websocket/websocket-proxy-test.js +++ b/test/websocket/websocket-proxy-test.js @@ -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": { @@ -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 @@ -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']); } }, @@ -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 @@ -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 @@ -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']); } } diff --git a/test/websocket/websocket-routing-proxy-test.js b/test/websocket/websocket-routing-proxy-test.js index a3edf05..2e5addf 100644 --- a/test/websocket/websocket-routing-proxy-test.js +++ b/test/websocket/websocket-routing-proxy-test.js @@ -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 { @@ -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": { @@ -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 @@ -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']); } }