mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
[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:
parent
ff829467d3
commit
1ee6beff6a
@ -37,7 +37,7 @@ var TestRunner = exports.TestRunner = function (protocol) {
|
|||||||
this.options = {};
|
this.options = {};
|
||||||
this.protocol = protocol;
|
this.protocol = protocol;
|
||||||
this.testServers = [];
|
this.testServers = [];
|
||||||
|
|
||||||
if (protocol === 'https') {
|
if (protocol === 'https') {
|
||||||
this.options.https = loadHttps();
|
this.options.https = loadHttps();
|
||||||
}
|
}
|
||||||
@ -47,17 +47,17 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx
|
|||||||
var self = this,
|
var self = this,
|
||||||
assertion = "should receive 'hello " + host + "'",
|
assertion = "should receive 'hello " + host + "'",
|
||||||
output = 'hello ' + host;
|
output = 'hello ' + host;
|
||||||
|
|
||||||
var test = {
|
var test = {
|
||||||
topic: function () {
|
topic: function () {
|
||||||
var that = this, options = {
|
var that = this, options = {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
uri: self.protocol + '://localhost:' + proxyPort,
|
uri: self.protocol + '://localhost:' + proxyPort,
|
||||||
headers: {
|
headers: {
|
||||||
host: host
|
host: host
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function startTest () {
|
function startTest () {
|
||||||
if (port) {
|
if (port) {
|
||||||
return self.startTargetServer(port, output, function () {
|
return self.startTargetServer(port, output, function () {
|
||||||
@ -67,47 +67,48 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx
|
|||||||
|
|
||||||
request(options, this.callback);
|
request(options, this.callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
return createProxy ? createProxy(startTest) : startTest();
|
return createProxy ? createProxy(startTest) : startTest();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
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);
|
||||||
};
|
};
|
||||||
|
|
||||||
return test;
|
return test;
|
||||||
};
|
};
|
||||||
|
|
||||||
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'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (createProxy) {
|
if (createProxy) {
|
||||||
return createProxy(function () {
|
return createProxy(function () {
|
||||||
request(options, that.callback);
|
request(options, that.callback);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
request(options, this.callback);
|
request(options, this.callback);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
test[assertion] = function (err, res, body) {
|
test[assertion] = function (err, res, body) {
|
||||||
assert.isNull(err);
|
assert.isNull(err);
|
||||||
assert.equal(res.statusCode, statusCode);
|
assert.equal(res.statusCode, statusCode);
|
||||||
};
|
};
|
||||||
|
|
||||||
return test;
|
return test;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -115,26 +116,28 @@ 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);
|
||||||
callback(null, proxyServer);
|
callback(null, proxyServer);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
// Creates the reverse proxy server with a specified latency
|
// Creates the reverse proxy server with a specified latency
|
||||||
//
|
//
|
||||||
TestRunner.prototype.startLatentProxyServer = function (port, targetPort, host, latency, callback) {
|
TestRunner.prototype.startLatentProxyServer = function (port, targetPort, host, latency, callback) {
|
||||||
// Initialize the nodeProxy and start proxying the request
|
// Initialize the nodeProxy and start proxying the request
|
||||||
var that = this, proxyServer = httpProxy.createServer(function (req, res, proxy) {
|
var that = this, proxyServer = httpProxy.createServer(function (req, res, proxy) {
|
||||||
var buffer = proxy.buffer(req);
|
var buffer = proxy.buffer(req);
|
||||||
|
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
proxy.proxyRequest(req, res, {
|
proxy.proxyRequest(req, res, {
|
||||||
port: targetPort,
|
port: targetPort,
|
||||||
host: host,
|
host: host,
|
||||||
buffer: buffer
|
buffer: buffer
|
||||||
});
|
});
|
||||||
}, latency);
|
}, latency);
|
||||||
@ -150,12 +153,12 @@ TestRunner.prototype.startLatentProxyServer = function (port, targetPort, host,
|
|||||||
// Creates the reverse proxy server with a ProxyTable
|
// Creates the reverse proxy server with a ProxyTable
|
||||||
//
|
//
|
||||||
TestRunner.prototype.startProxyServerWithTable = function (port, options, callback) {
|
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 () {
|
proxyServer.listen(port, function () {
|
||||||
that.testServers.push(proxyServer);
|
that.testServers.push(proxyServer);
|
||||||
callback();
|
callback();
|
||||||
});
|
});
|
||||||
|
|
||||||
return proxyServer;
|
return proxyServer;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -164,21 +167,28 @@ 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);
|
||||||
callback();
|
callback();
|
||||||
});
|
});
|
||||||
|
|
||||||
return proxyServer;
|
return proxyServer;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -186,7 +196,7 @@ TestRunner.prototype.startProxyServerWithTableAndLatency = function (port, laten
|
|||||||
// Creates proxy server forwarding to the specified options
|
// Creates proxy server forwarding to the specified options
|
||||||
//
|
//
|
||||||
TestRunner.prototype.startProxyServerWithForwarding = function (port, targetPort, host, options, callback) {
|
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 () {
|
proxyServer.listen(port, function () {
|
||||||
that.testServers.push(proxyServer);
|
that.testServers.push(proxyServer);
|
||||||
callback(null, proxyServer);
|
callback(null, proxyServer);
|
||||||
@ -200,13 +210,13 @@ 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
|
||||||
? https.createServer(this.options.https, handler)
|
? https.createServer(this.options.https, handler)
|
||||||
: http.createServer(handler);
|
: http.createServer(handler);
|
||||||
|
|
||||||
targetServer.listen(port, function () {
|
targetServer.listen(port, function () {
|
||||||
that.testServers.push(targetServer);
|
that.testServers.push(targetServer);
|
||||||
callback(null, targetServer);
|
callback(null, targetServer);
|
||||||
@ -222,4 +232,4 @@ TestRunner.prototype.closeServers = function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return this.testServers;
|
return this.testServers;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -89,4 +89,4 @@ vows.describe('node-http-proxy/' + protocol).addBatch({
|
|||||||
assert.isTrue(true);
|
assert.isTrue(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).export(module);
|
}).export(module);
|
||||||
|
|||||||
@ -14,7 +14,7 @@ var fs = require('fs'),
|
|||||||
helpers = require('./helpers'),
|
helpers = require('./helpers'),
|
||||||
argv = require('optimist').argv,
|
argv = require('optimist').argv,
|
||||||
TestRunner = helpers.TestRunner;
|
TestRunner = helpers.TestRunner;
|
||||||
|
|
||||||
var protocol = argv.https ? 'https' : 'http',
|
var protocol = argv.https ? 'https' : 'http',
|
||||||
runner = new TestRunner(protocol),
|
runner = new TestRunner(protocol),
|
||||||
routeFile = path.join(__dirname, 'config.json');
|
routeFile = path.join(__dirname, 'config.json');
|
||||||
@ -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'
|
||||||
}
|
}
|
||||||
@ -126,4 +126,4 @@ vows.describe('node-http-proxy/proxy-table/' + protocol).addBatch({
|
|||||||
assert.isTrue(true);
|
assert.isTrue(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).export(module);
|
}).export(module);
|
||||||
|
|||||||
@ -23,7 +23,7 @@
|
|||||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var vows = require('vows'),
|
var vows = require('vows'),
|
||||||
util = require('util'),
|
util = require('util'),
|
||||||
colors = require('colors'),
|
colors = require('colors'),
|
||||||
@ -34,7 +34,7 @@ var vows = require('vows'),
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
var utils = require('socket.io/lib/socket.io/utils'),
|
var utils = require('socket.io/lib/socket.io/utils'),
|
||||||
io = require('socket.io');
|
io = require('socket.io');
|
||||||
}
|
}
|
||||||
catch (ex) {
|
catch (ex) {
|
||||||
console.error('Socket.io is required for this test:');
|
console.error('Socket.io is required for this test:');
|
||||||
@ -50,16 +50,16 @@ vows.describe('node-http-proxy/websocket').addBatch({
|
|||||||
"when an inbound message is sent from a WebSocket client": {
|
"when an inbound message is sent from a WebSocket client": {
|
||||||
topic: function () {
|
topic: function () {
|
||||||
var that = this;
|
var that = this;
|
||||||
|
|
||||||
runner.startTargetServer(8130, 'hello websocket', function (err, target) {
|
runner.startTargetServer(8130, 'hello websocket', function (err, target) {
|
||||||
var socket = io.listen(target);
|
var socket = io.listen(target);
|
||||||
|
|
||||||
socket.on('connection', function (client) {
|
socket.on('connection', function (client) {
|
||||||
client.on('message', function (msg) {
|
client.on('message', function (msg) {
|
||||||
that.callback(null, msg);
|
that.callback(null, msg);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
runner.startProxyServer(8131, 8130, 'localhost', function (err, proxy) {
|
runner.startProxyServer(8131, 8130, 'localhost', function (err, proxy) {
|
||||||
//
|
//
|
||||||
// Setup the web socket against our proxy
|
// Setup the web socket against our proxy
|
||||||
@ -70,23 +70,23 @@ 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');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"when an outbound message is sent from the target server": {
|
"when an outbound message is sent from the target server": {
|
||||||
topic: function () {
|
topic: function () {
|
||||||
var that = this;
|
var that = this;
|
||||||
|
|
||||||
runner.startTargetServer(8132, 'hello websocket', function (err, target) {
|
runner.startTargetServer(8132, 'hello websocket', function (err, target) {
|
||||||
var socket = io.listen(target);
|
var socket = io.listen(target);
|
||||||
|
|
||||||
socket.on('connection', function (client) {
|
socket.on('connection', function (client) {
|
||||||
socket.broadcast('from server');
|
socket.broadcast('from server');
|
||||||
});
|
});
|
||||||
|
|
||||||
runner.startProxyServer(8133, 8132, 'localhost', function (err, proxy) {
|
runner.startProxyServer(8133, 8132, 'localhost', function (err, proxy) {
|
||||||
//
|
//
|
||||||
// Setup the web socket against our proxy
|
// Setup the web socket against our proxy
|
||||||
@ -100,11 +100,11 @@ 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');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -117,4 +117,4 @@ vows.describe('node-http-proxy/websocket').addBatch({
|
|||||||
assert.isTrue(true);
|
assert.isTrue(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).export(module);
|
}).export(module);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user