Commit 095e86aa authored by indexzero's avatar indexzero

[test] Updated tests to include support for latent requests

parent ead7567d
...@@ -43,6 +43,7 @@ exports.NodeProxy.prototype = { ...@@ -43,6 +43,7 @@ exports.NodeProxy.prototype = {
init: function (req, res) { init: function (req, res) {
this.events = []; this.events = [];
var self = this;
this.onData = function () { this.onData = function () {
self.events.push(['data'].concat(self.toArray(arguments))); self.events.push(['data'].concat(self.toArray(arguments)));
......
...@@ -15,6 +15,7 @@ var vows = require('vows'), ...@@ -15,6 +15,7 @@ var vows = require('vows'),
require.paths.unshift(require('path').join(__dirname, '../lib/')); require.paths.unshift(require('path').join(__dirname, '../lib/'));
var NodeProxy = require('node-proxy').NodeProxy; var NodeProxy = require('node-proxy').NodeProxy;
var testServers = {};
// //
// Simple 'hello world' response for test purposes // Simple 'hello world' response for test purposes
...@@ -28,39 +29,69 @@ var helloWorld = function(req, res) { ...@@ -28,39 +29,69 @@ var helloWorld = function(req, res) {
// //
// Creates the reverse proxy server // Creates the reverse proxy server
// //
var startProxy = function (server, port, proxy) { var startProxyServer = function (server, port, proxy) {
http.createServer(function (req, res){ var proxyServer = http.createServer(function (req, res){
// Initialize the nodeProxy and start proxying the request // Initialize the nodeProxy and start proxying the request
proxy.init(req, res); proxy.init(req, res);
proxy.proxyRequest(server, port, req, res); proxy.proxyRequest(server, port, req, res);
}).listen(8080); });
proxyServer.listen(8080);
return proxyServer;
};
//
// Creates the reverse proxy server with a specified latency
//
var startLatentProxyServer = function (server, port, proxy, latency) {
var proxyServer = http.createServer(function (req, res){
// Initialize the nodeProxy and start proxying the request
proxy.init(req, res);
setTimeout(function () {
proxy.proxyRequest(server, port, req, res);
}, latency);
});
proxyServer.listen(8081);
return proxyServer;
}; };
// //
// Creates the 'hellonode' server // Creates the 'hellonode' server
// //
var startProxyTarget = function () { var startTargetServer = function (port) {
http.createServer(function (req, res) { var targetServer = http.createServer(function (req, res) {
helloWorld(req, res); helloWorld(req, res);
}).listen(8081); })
targetServer.listen(port);
return targetServer;
}; };
// //
// The default test bootstrapper // The default test bootstrapper with no latency
// //
var startProxyTest = function () { var startTest = function (proxy, port) {
var proxy = new (NodeProxy); testServers.noLatency = [];
startProxy('127.0.0.1', 8081, proxy); testServers.noLatency.push(startProxyServer('127.0.0.1', port, proxy));
startProxyTarget(); testServers.noLatency.push(startTargetServer(port));
return proxy;
}; };
//
// The test bootstrapper with some latency
//
var startTestWithLatency = function (proxy, port) {
testServers.latency = [];
testServers.latency.push(startLatentProxyServer('127.0.0.1', port, proxy, 2000));
testServers.latency.push(startTargetServer(port));
};
vows.describe('node-proxy').addBatch({ vows.describe('node-proxy').addBatch({
"When an incoming request is proxied to the helloNode server" : { "When an incoming request is proxied to the helloNode server" : {
"with no latency" : {
topic: function () { topic: function () {
// Create the proxy and start listening var proxy = new (NodeProxy);
var proxy = startProxyTest(); startTest(proxy, 8082);
proxy.emitter.addListener('end', this.callback); proxy.emitter.addListener('end', this.callback);
var client = http.createClient(8080, '127.0.0.1'); var client = http.createClient(8080, '127.0.0.1');
...@@ -69,6 +100,27 @@ vows.describe('node-proxy').addBatch({ ...@@ -69,6 +100,27 @@ vows.describe('node-proxy').addBatch({
}, },
"it should received 'hello world'": function (err, body) { "it should received 'hello world'": function (err, body) {
assert.equal(body, 'hello world'); assert.equal(body, 'hello world');
testServers.noLatency.forEach(function (server) {
server.close();
})
}
},
"with latency": {
topic: function () {
var proxy = new (NodeProxy);
startTestWithLatency(proxy, 8083);
proxy.emitter.addListener('end', this.callback);
var client = http.createClient(8081, '127.0.0.1');
var request = client.request('GET', '/');
request.end();
},
"it should receive 'hello world'": function (err, body) {
assert.equal(body, 'hello world');
testServers.latency.forEach(function (server) {
server.close();
})
}
} }
} }
}).export(module); }).export(module);
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment