Skip to content

Commit

Permalink
[test] Updated tests to include support for latent requests
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Jul 24, 2010
1 parent ead7567 commit 095e86a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 21 deletions.
1 change: 1 addition & 0 deletions lib/node-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ exports.NodeProxy.prototype = {

init: function (req, res) {
this.events = [];
var self = this;

this.onData = function () {
self.events.push(['data'].concat(self.toArray(arguments)));
Expand Down
94 changes: 73 additions & 21 deletions test/node-proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var vows = require('vows'),
require.paths.unshift(require('path').join(__dirname, '../lib/'));

var NodeProxy = require('node-proxy').NodeProxy;
var testServers = {};

//
// Simple 'hello world' response for test purposes
Expand All @@ -28,47 +29,98 @@ var helloWorld = function(req, res) {
//
// Creates the reverse proxy server
//
var startProxy = function (server, port, proxy) {
http.createServer(function (req, res){
var startProxyServer = function (server, port, proxy) {
var proxyServer = http.createServer(function (req, res){
// Initialize the nodeProxy and start proxying the request
proxy.init(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
//
var startProxyTarget = function () {
http.createServer(function (req, res) {
var startTargetServer = function (port) {
var targetServer = http.createServer(function (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 proxy = new (NodeProxy);
startProxy('127.0.0.1', 8081, proxy);
startProxyTarget();
return proxy;
var startTest = function (proxy, port) {
testServers.noLatency = [];
testServers.noLatency.push(startProxyServer('127.0.0.1', port, proxy));
testServers.noLatency.push(startTargetServer(port));
};

//
// 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({
"When an incoming request is proxied to the helloNode server" : {
topic: function () {
// Create the proxy and start listening
var proxy = startProxyTest();
proxy.emitter.addListener('end', this.callback);
"with no latency" : {
topic: function () {
var proxy = new (NodeProxy);
startTest(proxy, 8082);
proxy.emitter.addListener('end', this.callback);

var client = http.createClient(8080, '127.0.0.1');
var request = client.request('GET', '/');
request.end();
var client = http.createClient(8080, '127.0.0.1');
var request = client.request('GET', '/');
request.end();
},
"it should received 'hello world'": function (err, body) {
assert.equal(body, 'hello world');
testServers.noLatency.forEach(function (server) {
server.close();
})
}
},
"it should received 'hello world'": function (err, body) {
assert.equal(body, 'hello world');
"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);

0 comments on commit 095e86a

Please sign in to comment.