Skip to content

Commit 095e86a

Browse files
committed
[test] Updated tests to include support for latent requests
1 parent ead7567 commit 095e86a

File tree

2 files changed

+74
-21
lines changed

2 files changed

+74
-21
lines changed

lib/node-proxy.js

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ exports.NodeProxy.prototype = {
4343

4444
init: function (req, res) {
4545
this.events = [];
46+
var self = this;
4647

4748
this.onData = function () {
4849
self.events.push(['data'].concat(self.toArray(arguments)));

test/node-proxy-test.js

+73-21
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var vows = require('vows'),
1515
require.paths.unshift(require('path').join(__dirname, '../lib/'));
1616

1717
var NodeProxy = require('node-proxy').NodeProxy;
18+
var testServers = {};
1819

1920
//
2021
// Simple 'hello world' response for test purposes
@@ -28,47 +29,98 @@ var helloWorld = function(req, res) {
2829
//
2930
// Creates the reverse proxy server
3031
//
31-
var startProxy = function (server, port, proxy) {
32-
http.createServer(function (req, res){
32+
var startProxyServer = function (server, port, proxy) {
33+
var proxyServer = http.createServer(function (req, res){
3334
// Initialize the nodeProxy and start proxying the request
3435
proxy.init(req, res);
3536
proxy.proxyRequest(server, port, req, res);
36-
}).listen(8080);
37+
});
38+
39+
proxyServer.listen(8080);
40+
return proxyServer;
41+
};
42+
43+
//
44+
// Creates the reverse proxy server with a specified latency
45+
//
46+
var startLatentProxyServer = function (server, port, proxy, latency) {
47+
var proxyServer = http.createServer(function (req, res){
48+
// Initialize the nodeProxy and start proxying the request
49+
proxy.init(req, res);
50+
setTimeout(function () {
51+
proxy.proxyRequest(server, port, req, res);
52+
}, latency);
53+
});
54+
55+
proxyServer.listen(8081);
56+
return proxyServer;
3757
};
3858

3959
//
4060
// Creates the 'hellonode' server
4161
//
42-
var startProxyTarget = function () {
43-
http.createServer(function (req, res) {
62+
var startTargetServer = function (port) {
63+
var targetServer = http.createServer(function (req, res) {
4464
helloWorld(req, res);
45-
}).listen(8081);
65+
})
66+
67+
targetServer.listen(port);
68+
return targetServer;
4669
};
4770

4871
//
49-
// The default test bootstrapper
72+
// The default test bootstrapper with no latency
5073
//
51-
var startProxyTest = function () {
52-
var proxy = new (NodeProxy);
53-
startProxy('127.0.0.1', 8081, proxy);
54-
startProxyTarget();
55-
return proxy;
74+
var startTest = function (proxy, port) {
75+
testServers.noLatency = [];
76+
testServers.noLatency.push(startProxyServer('127.0.0.1', port, proxy));
77+
testServers.noLatency.push(startTargetServer(port));
5678
};
5779

80+
//
81+
// The test bootstrapper with some latency
82+
//
83+
var startTestWithLatency = function (proxy, port) {
84+
testServers.latency = [];
85+
testServers.latency.push(startLatentProxyServer('127.0.0.1', port, proxy, 2000));
86+
testServers.latency.push(startTargetServer(port));
87+
};
5888

5989
vows.describe('node-proxy').addBatch({
6090
"When an incoming request is proxied to the helloNode server" : {
61-
topic: function () {
62-
// Create the proxy and start listening
63-
var proxy = startProxyTest();
64-
proxy.emitter.addListener('end', this.callback);
91+
"with no latency" : {
92+
topic: function () {
93+
var proxy = new (NodeProxy);
94+
startTest(proxy, 8082);
95+
proxy.emitter.addListener('end', this.callback);
6596

66-
var client = http.createClient(8080, '127.0.0.1');
67-
var request = client.request('GET', '/');
68-
request.end();
97+
var client = http.createClient(8080, '127.0.0.1');
98+
var request = client.request('GET', '/');
99+
request.end();
100+
},
101+
"it should received 'hello world'": function (err, body) {
102+
assert.equal(body, 'hello world');
103+
testServers.noLatency.forEach(function (server) {
104+
server.close();
105+
})
106+
}
69107
},
70-
"it should received 'hello world'": function (err, body) {
71-
assert.equal(body, 'hello world');
108+
"with latency": {
109+
topic: function () {
110+
var proxy = new (NodeProxy);
111+
startTestWithLatency(proxy, 8083);
112+
proxy.emitter.addListener('end', this.callback);
113+
114+
var client = http.createClient(8081, '127.0.0.1');
115+
var request = client.request('GET', '/');
116+
request.end();
117+
},
118+
"it should receive 'hello world'": function (err, body) {
119+
assert.equal(body, 'hello world');
120+
testServers.latency.forEach(function (server) {
121+
server.close();
122+
})
123+
}
72124
}
73125
}
74126
}).export(module);

0 commit comments

Comments
 (0)