[test] Fix tests in https mode

Tests can be run in https mode by adding the --https flag
vows test/*-test.js --spec --https
This commit is contained in:
Olivier Lauzon 2011-05-17 08:51:24 +08:00
parent ff829467d3
commit 1ee6beff6a
4 changed files with 67 additions and 57 deletions

View File

@ -72,7 +72,7 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx
} }
}; };
test[assertion] = function (err, res, body) {; test[assertion] = function (err, res, body) {
assert.isNull(err); assert.isNull(err);
assert.equal(body, output); assert.equal(body, output);
}; };
@ -81,13 +81,14 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx
}; };
TestRunner.prototype.assertResponseCode = function (proxyPort, statusCode, createProxy) { TestRunner.prototype.assertResponseCode = function (proxyPort, statusCode, createProxy) {
var assertion = "should receive " + statusCode + " responseCode"; var assertion = "should receive " + statusCode + " responseCode",
protocol = this.protocol;
var test = { var test = {
topic: function () { topic: function () {
var that = this, options = { var that = this, options = {
method: 'GET', method: 'GET',
uri: 'http://localhost:' + proxyPort, uri: protocol + '://localhost:' + proxyPort,
headers: { headers: {
host: 'unknown.com' host: 'unknown.com'
} }
@ -115,7 +116,9 @@ TestRunner.prototype.assertResponseCode = function (proxyPort, statusCode, creat
// Creates the reverse proxy server // Creates the reverse proxy server
// //
TestRunner.prototype.startProxyServer = function (port, targetPort, host, callback) { 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 () { proxyServer.listen(port, function () {
that.testServers.push(proxyServer); that.testServers.push(proxyServer);
@ -164,15 +167,22 @@ TestRunner.prototype.startProxyServerWithTable = function (port, options, callba
// //
TestRunner.prototype.startProxyServerWithTableAndLatency = function (port, latency, options, callback) { TestRunner.prototype.startProxyServerWithTableAndLatency = function (port, latency, options, callback) {
// Initialize the nodeProxy and start proxying the request // Initialize the nodeProxy and start proxying the request
var proxyServer, that = this, proxy = new httpProxy.HttpProxy(merge({}, options, this.options)); var proxyServer,
proxyServer = http.createServer(function (req, res) { that = this,
proxy = new httpProxy.HttpProxy(merge({}, options, that.options));
var handler = function (req, res) {
var buffer = proxy.buffer(req); var buffer = proxy.buffer(req);
setTimeout(function () { setTimeout(function () {
proxy.proxyRequest(req, res, { proxy.proxyRequest(req, res, {
buffer: buffer buffer: buffer
}); });
}, latency); }, latency);
}, this.options); };
proxyServer = that.options.https
? https.createServer(that.options.https, handler, that.options)
: http.createServer(handler, that.options);
proxyServer.listen(port, function () { proxyServer.listen(port, function () {
that.testServers.push(proxyServer); that.testServers.push(proxyServer);
@ -200,7 +210,7 @@ TestRunner.prototype.startTargetServer = function (port, output, callback) {
var that = this, targetServer, handler = function (req, res) { var that = this, targetServer, handler = function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' }); res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write(output); res.write(output);
res.end(); res.end();
}; };
targetServer = this.options.https targetServer = this.options.https

View File

@ -78,13 +78,13 @@ vows.describe('node-http-proxy/proxy-table/' + protocol).addBatch({
data = fs.readFileSync(routeFile), data = fs.readFileSync(routeFile),
config = JSON.parse(data); 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)); fs.writeFileSync(routeFile, JSON.stringify(config));
this.server.on('routes', function () { this.server.on('routes', function () {
var options = { var options = {
method: 'GET', method: 'GET',
uri: 'http://localhost:8100', uri: protocol + '://localhost:8100',
headers: { headers: {
host: 'dynamic.com' host: 'dynamic.com'
} }

View File

@ -70,7 +70,7 @@ vows.describe('node-http-proxy/websocket').addBatch({
ws.send(utils.encode('from client')); ws.send(utils.encode('from client'));
}); });
}); });
}) });
}, },
"the target server should receive the message": function (err, msg) { "the target server should receive the message": function (err, msg) {
assert.equal(msg, 'from client'); assert.equal(msg, 'from client');
@ -100,7 +100,7 @@ vows.describe('node-http-proxy/websocket').addBatch({
} }
}); });
}); });
}) });
}, },
"the client should receive the message": function (err, msg) { "the client should receive the message": function (err, msg) {
assert.equal(msg, 'from server'); assert.equal(msg, 'from server');