Skip to content

Commit

Permalink
[test] Fix tests in https mode
Browse files Browse the repository at this point in the history
Tests can be run in https mode by adding the --https flag
vows test/*-test.js --spec --https
  • Loading branch information
olauzon committed May 17, 2011
1 parent ff82946 commit 1ee6bef
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 57 deletions.
84 changes: 47 additions & 37 deletions test/helpers.js
Expand Up @@ -37,7 +37,7 @@ var TestRunner = exports.TestRunner = function (protocol) {
this.options = {};
this.protocol = protocol;
this.testServers = [];

if (protocol === 'https') {
this.options.https = loadHttps();
}
Expand All @@ -47,17 +47,17 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx
var self = this,
assertion = "should receive 'hello " + host + "'",
output = 'hello ' + host;

var test = {
topic: function () {
var that = this, options = {
method: 'GET',
method: 'GET',
uri: self.protocol + '://localhost:' + proxyPort,
headers: {
host: host
}
};

function startTest () {
if (port) {
return self.startTargetServer(port, output, function () {
Expand All @@ -67,74 +67,77 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx

request(options, this.callback);
}

return createProxy ? createProxy(startTest) : startTest();
}
};
test[assertion] = function (err, res, body) {;

test[assertion] = function (err, res, body) {
assert.isNull(err);
assert.equal(body, output);
};

return test;
};

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

var assertion = "should receive " + statusCode + " responseCode",
protocol = this.protocol;

var test = {
topic: function () {
var that = this, options = {
method: 'GET',
uri: 'http://localhost:' + proxyPort,
method: 'GET',
uri: protocol + '://localhost:' + proxyPort,
headers: {
host: 'unknown.com'
}
};

if (createProxy) {
return createProxy(function () {
request(options, that.callback);
request(options, that.callback);
});
}

request(options, this.callback);
}
};

test[assertion] = function (err, res, body) {
assert.isNull(err);
assert.equal(res.statusCode, statusCode);
};

return test;
};

//
// Creates the reverse proxy server
//
TestRunner.prototype.startProxyServer = function (port, targetPort, host, callback) {
var that = this, proxyServer = httpProxy.createServer(targetPort, host);

var that = this,
options = that.options,
proxyServer = httpProxy.createServer(targetPort, host, options);

proxyServer.listen(port, function () {
that.testServers.push(proxyServer);
callback(null, proxyServer);
});
});
};

//
//
// Creates the reverse proxy server with a specified latency
//
TestRunner.prototype.startLatentProxyServer = function (port, targetPort, host, latency, callback) {
// Initialize the nodeProxy and start proxying the request
var that = this, proxyServer = httpProxy.createServer(function (req, res, proxy) {
var buffer = proxy.buffer(req);

setTimeout(function () {
proxy.proxyRequest(req, res, {
port: targetPort,
host: host,
port: targetPort,
host: host,
buffer: buffer
});
}, latency);
Expand All @@ -150,12 +153,12 @@ TestRunner.prototype.startLatentProxyServer = function (port, targetPort, host,
// Creates the reverse proxy server with a ProxyTable
//
TestRunner.prototype.startProxyServerWithTable = function (port, options, callback) {
var that = this, proxyServer = httpProxy.createServer(merge({}, options, this.options));
var that = this, proxyServer = httpProxy.createServer(merge({}, options, this.options));
proxyServer.listen(port, function () {
that.testServers.push(proxyServer);
callback();
});

return proxyServer;
};

Expand All @@ -164,29 +167,36 @@ TestRunner.prototype.startProxyServerWithTable = function (port, options, callba
//
TestRunner.prototype.startProxyServerWithTableAndLatency = function (port, latency, options, callback) {
// Initialize the nodeProxy and start proxying the request
var proxyServer, that = this, proxy = new httpProxy.HttpProxy(merge({}, options, this.options));
proxyServer = http.createServer(function (req, res) {
var proxyServer,
that = this,
proxy = new httpProxy.HttpProxy(merge({}, options, that.options));

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

};

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

proxyServer.listen(port, function () {
that.testServers.push(proxyServer);
callback();
});

return proxyServer;
};

//
// Creates proxy server forwarding to the specified options
//
TestRunner.prototype.startProxyServerWithForwarding = function (port, targetPort, host, options, callback) {
var that = this, proxyServer = httpProxy.createServer(targetPort, host, merge({}, options, this.options));
var that = this, proxyServer = httpProxy.createServer(targetPort, host, merge({}, options, this.options));
proxyServer.listen(port, function () {
that.testServers.push(proxyServer);
callback(null, proxyServer);
Expand All @@ -200,13 +210,13 @@ TestRunner.prototype.startTargetServer = function (port, output, callback) {
var that = this, targetServer, handler = function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write(output);
res.end();
res.end();
};
targetServer = this.options.https

targetServer = this.options.https
? https.createServer(this.options.https, handler)
: http.createServer(handler);

targetServer.listen(port, function () {
that.testServers.push(targetServer);
callback(null, targetServer);
Expand All @@ -222,4 +232,4 @@ TestRunner.prototype.closeServers = function () {
});

return this.testServers;
};
};
2 changes: 1 addition & 1 deletion test/node-http-proxy-test.js
Expand Up @@ -89,4 +89,4 @@ vows.describe('node-http-proxy/' + protocol).addBatch({
assert.isTrue(true);
}
}
}).export(module);
}).export(module);
12 changes: 6 additions & 6 deletions test/proxy-table-test.js
Expand Up @@ -14,7 +14,7 @@ var fs = require('fs'),
helpers = require('./helpers'),
argv = require('optimist').argv,
TestRunner = helpers.TestRunner;

var protocol = argv.https ? 'https' : 'http',
runner = new TestRunner(protocol),
routeFile = path.join(__dirname, 'config.json');
Expand Down Expand Up @@ -78,13 +78,13 @@ vows.describe('node-http-proxy/proxy-table/' + protocol).addBatch({
data = fs.readFileSync(routeFile),
config = JSON.parse(data);

config.router['dynamic.com'] = "127.0.0.1:8103"
config.router['dynamic.com'] = "127.0.0.1:8103";
fs.writeFileSync(routeFile, JSON.stringify(config));

this.server.on('routes', function () {
var options = {
method: 'GET',
uri: 'http://localhost:8100',
method: 'GET',
uri: protocol + '://localhost:8100',
headers: {
host: 'dynamic.com'
}
Expand Down Expand Up @@ -126,4 +126,4 @@ vows.describe('node-http-proxy/proxy-table/' + protocol).addBatch({
assert.isTrue(true);
}
}
}).export(module);
}).export(module);
26 changes: 13 additions & 13 deletions test/web-socket-proxy-test.js
Expand Up @@ -23,7 +23,7 @@
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

var vows = require('vows'),
util = require('util'),
colors = require('colors'),
Expand All @@ -34,7 +34,7 @@ var vows = require('vows'),

try {
var utils = require('socket.io/lib/socket.io/utils'),
io = require('socket.io');
io = require('socket.io');
}
catch (ex) {
console.error('Socket.io is required for this test:');
Expand All @@ -50,16 +50,16 @@ vows.describe('node-http-proxy/websocket').addBatch({
"when an inbound message is sent from a WebSocket client": {
topic: function () {
var that = this;

runner.startTargetServer(8130, 'hello websocket', function (err, target) {
var socket = io.listen(target);

socket.on('connection', function (client) {
client.on('message', function (msg) {
that.callback(null, msg);
});
});

runner.startProxyServer(8131, 8130, 'localhost', function (err, proxy) {
//
// Setup the web socket against our proxy
Expand All @@ -70,23 +70,23 @@ vows.describe('node-http-proxy/websocket').addBatch({
ws.send(utils.encode('from client'));
});
});
})
});
},
"the target server should receive the message": function (err, msg) {
assert.equal(msg, 'from client');
}
}
},
"when an outbound message is sent from the target server": {
topic: function () {
var that = this;

runner.startTargetServer(8132, 'hello websocket', function (err, target) {
var socket = io.listen(target);

socket.on('connection', function (client) {
socket.broadcast('from server');
});

runner.startProxyServer(8133, 8132, 'localhost', function (err, proxy) {
//
// Setup the web socket against our proxy
Expand All @@ -100,11 +100,11 @@ vows.describe('node-http-proxy/websocket').addBatch({
}
});
});
})
});
},
"the client should receive the message": function (err, msg) {
assert.equal(msg, 'from server');
}
}
}
}
}
Expand All @@ -117,4 +117,4 @@ vows.describe('node-http-proxy/websocket').addBatch({
assert.isTrue(true);
}
}
}).export(module);
}).export(module);

0 comments on commit 1ee6bef

Please sign in to comment.