mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
[test] Simplified tests. Added tests for experimental websocket support
This commit is contained in:
parent
f84880fcd9
commit
8c3e993833
@ -28,7 +28,6 @@ var sys = require('sys'),
|
||||
http = require('http'),
|
||||
events = require('events'),
|
||||
pool = require('./../vendor/pool/main'),
|
||||
eyes = require('eyes'),
|
||||
min = 0,
|
||||
max = 100;
|
||||
|
||||
@ -47,10 +46,6 @@ exports.createServer = function () {
|
||||
var server = http.createServer(function (req, res) {
|
||||
var proxy = new HttpProxy(req, res);
|
||||
|
||||
proxy.emitter.on('proxy', function (err, body) {
|
||||
server.emit('proxy', err, body);
|
||||
});
|
||||
|
||||
// If we were passed a callback to process the request
|
||||
// or response in some way, then call it.
|
||||
if(callback) {
|
||||
@ -61,15 +56,14 @@ exports.createServer = function () {
|
||||
}
|
||||
});
|
||||
|
||||
// If callback is empty - tunnel websocket request automatically
|
||||
if (!callback) {
|
||||
// WebSocket support
|
||||
// WebSocket support: if callback is empty tunnel
|
||||
// websocket request automatically
|
||||
server.on('upgrade', function(req, socket, head) {
|
||||
var proxy = new HttpProxy(req, socket, head);
|
||||
|
||||
// Tunnel websocket requests too
|
||||
proxy.proxyWebSocketRequest(port, host);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@ -87,7 +81,6 @@ exports.setMax = function (value) {
|
||||
};
|
||||
|
||||
var HttpProxy = function (req, res, head) {
|
||||
this.emitter = new(events.EventEmitter);
|
||||
this.events = {};
|
||||
this.req = req;
|
||||
|
||||
@ -145,20 +138,16 @@ HttpProxy.prototype = {
|
||||
self.body = '';
|
||||
|
||||
// Open new HTTP request to internal resource with will act as a reverse proxy pass
|
||||
//var p = manager.getPool(port, server);
|
||||
//sys.puts('Current pool count for ' + req.headers.host + ":" + port + ' ' + p.clients.length + ', Busy: ' + p.getBusy() + ', Free: ' + p.getFree());
|
||||
var p = manager.getPool(port, server);
|
||||
|
||||
//p.on('error', function (err) {
|
||||
p.on('error', function (err) {
|
||||
// Remark: We should probably do something here
|
||||
// but this is a hot-fix because I don't think 'pool'
|
||||
// should be emitting this event.
|
||||
//});
|
||||
});
|
||||
|
||||
var client = http.createClient(port, server);
|
||||
var reverse_proxy = client.request(req.method, req.url, req.headers);
|
||||
|
||||
//p.request(req.method, req.url, req.headers, function (reverse_proxy) {
|
||||
|
||||
p.request(req.method, req.url, req.headers, function (reverse_proxy) {
|
||||
// Create an error handler so we can use it temporarily
|
||||
function error(obj) {
|
||||
var fn = function (err) {
|
||||
@ -210,8 +199,6 @@ HttpProxy.prototype = {
|
||||
|
||||
// Add event listener for end of proxied response
|
||||
response.addListener('end', function () {
|
||||
// Remark: Emit the end event for testability
|
||||
self.emitter.emit('proxy', null, self.body);
|
||||
reverse_proxy.removeListener('error', reverseProxyError);
|
||||
res.end();
|
||||
});
|
||||
@ -234,7 +221,7 @@ HttpProxy.prototype = {
|
||||
|
||||
self.unwatch(req);
|
||||
|
||||
//});
|
||||
});
|
||||
},
|
||||
|
||||
proxyWebSocketRequest: function (port, server, host) {
|
||||
@ -251,8 +238,7 @@ HttpProxy.prototype = {
|
||||
return h;
|
||||
}
|
||||
|
||||
// WebSocket requests has
|
||||
// method = GET
|
||||
// WebSocket requests has method = GET
|
||||
if (req.method !== 'GET' || headers.upgrade.toLowerCase() !== 'websocket') {
|
||||
// This request is not WebSocket request
|
||||
return;
|
||||
|
||||
@ -1,140 +0,0 @@
|
||||
/*
|
||||
node-http-proxy-test.js: http proxy for node.js
|
||||
|
||||
Copyright (c) 2010 Charlie Robbins & Marak Squires http://github.com/nodejitsu/node-http-proxy
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
var vows = require('vows'),
|
||||
sys = require('sys'),
|
||||
assert = require('assert'),
|
||||
http = require('http');
|
||||
|
||||
var httpProxy = require('./../lib/node-http-proxy');
|
||||
var testServers = {};
|
||||
|
||||
//
|
||||
// Creates the reverse proxy server
|
||||
//
|
||||
var startProxyServer = function (port, server) {
|
||||
var proxyServer = httpProxy.createServer(port, server);
|
||||
proxyServer.listen(8080);
|
||||
return proxyServer;
|
||||
};
|
||||
|
||||
//
|
||||
// Creates the reverse proxy server with a specified latency
|
||||
//
|
||||
var startLatentProxyServer = function (port, server, latency) {
|
||||
// Initialize the nodeProxy and start proxying the request
|
||||
var proxyServer = httpProxy.createServer(function (req, res, proxy) {
|
||||
setTimeout(function () {
|
||||
proxy.proxyRequest(port, server);
|
||||
}, latency);
|
||||
});
|
||||
|
||||
proxyServer.listen(8081);
|
||||
return proxyServer;
|
||||
};
|
||||
|
||||
//
|
||||
// Creates the 'hellonode' server
|
||||
//
|
||||
var startTargetServer = function (port) {
|
||||
var targetServer = http.createServer(function (req, res) {
|
||||
res.writeHead(200, {'Content-Type': 'text/plain'});
|
||||
res.write('hello world')
|
||||
res.end();
|
||||
});
|
||||
|
||||
targetServer.listen(port);
|
||||
return targetServer;
|
||||
};
|
||||
|
||||
//
|
||||
// The default test bootstrapper with no latency
|
||||
//
|
||||
var startTest = function (port) {
|
||||
var proxyServer = startProxyServer(port, 'localhost'),
|
||||
targetServer = startTargetServer(port + 1000);
|
||||
|
||||
testServers.noLatency = [];
|
||||
testServers.noLatency.push(proxyServer);
|
||||
testServers.noLatency.push(targetServer);
|
||||
|
||||
return proxyServer;
|
||||
};
|
||||
|
||||
//
|
||||
// The test bootstrapper with some latency
|
||||
//
|
||||
var startTestWithLatency = function (port) {
|
||||
var proxyServer = startLatentProxyServer(port, 'localhost', 2000),
|
||||
targetServer = startTargetServer(port);
|
||||
|
||||
testServers.latency = [];
|
||||
testServers.latency.push(proxyServer);
|
||||
testServers.latency.push(targetServer);
|
||||
|
||||
return proxyServer;
|
||||
};
|
||||
|
||||
vows.describe('node-http-proxy').addBatch({
|
||||
"A node-http-proxy": {
|
||||
"when instantiated directly": {
|
||||
"and an incoming request is proxied to the helloNode server" : {
|
||||
"with no latency" : {
|
||||
topic: function () {
|
||||
var proxy = startTest(8082);
|
||||
proxy.addListener('proxy', this.callback);
|
||||
|
||||
var client = http.createClient(8080, 'localhost');
|
||||
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();
|
||||
})
|
||||
}
|
||||
},
|
||||
"with latency": {
|
||||
topic: function () {
|
||||
var proxy = startTestWithLatency(8083);
|
||||
proxy.addListener('proxy', this.callback);
|
||||
|
||||
var client = http.createClient(8081, 'localhost');
|
||||
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);
|
||||
@ -26,34 +26,35 @@
|
||||
|
||||
var vows = require('vows'),
|
||||
sys = require('sys'),
|
||||
request = require('request'),
|
||||
assert = require('assert'),
|
||||
http = require('http');
|
||||
|
||||
var httpProxy = require('./../lib/node-http-proxy');
|
||||
var testServers = {};
|
||||
var testServers = [];
|
||||
|
||||
//
|
||||
// Creates the reverse proxy server
|
||||
//
|
||||
var startProxyServer = function (port, server) {
|
||||
var proxyServer = httpProxy.createServer(port, server);
|
||||
proxyServer.listen(8080);
|
||||
return proxyServer;
|
||||
var startProxyServer = function (port, targetPort, server) {
|
||||
var proxyServer = httpProxy.createServer(targetPort, server);
|
||||
proxyServer.listen(port);
|
||||
testServers.push(proxyServer);
|
||||
};
|
||||
|
||||
//
|
||||
// Creates the reverse proxy server with a specified latency
|
||||
//
|
||||
var startLatentProxyServer = function (port, server, latency) {
|
||||
var startLatentProxyServer = function (port, targetPort, server, latency) {
|
||||
// Initialize the nodeProxy and start proxying the request
|
||||
var proxyServer = httpProxy.createServer(function (req, res, proxy) {
|
||||
setTimeout(function () {
|
||||
proxy.proxyRequest(port, server);
|
||||
proxy.proxyRequest(targetPort, server);
|
||||
}, latency);
|
||||
});
|
||||
|
||||
proxyServer.listen(8081);
|
||||
return proxyServer;
|
||||
proxyServer.listen(port);
|
||||
testServers.push(proxyServer);
|
||||
};
|
||||
|
||||
//
|
||||
@ -67,74 +68,114 @@ var startTargetServer = function (port) {
|
||||
});
|
||||
|
||||
targetServer.listen(port);
|
||||
testServers.push(targetServer);
|
||||
return targetServer;
|
||||
};
|
||||
|
||||
//
|
||||
// The default test bootstrapper with no latency
|
||||
//
|
||||
var startTest = function (port) {
|
||||
var proxyServer = startProxyServer(port, 'localhost'),
|
||||
targetServer = startTargetServer(port);
|
||||
|
||||
testServers.noLatency = [];
|
||||
testServers.noLatency.push(proxyServer);
|
||||
testServers.noLatency.push(targetServer);
|
||||
|
||||
return proxyServer;
|
||||
};
|
||||
|
||||
//
|
||||
// The test bootstrapper with some latency
|
||||
//
|
||||
var startTestWithLatency = function (port) {
|
||||
var proxyServer = startLatentProxyServer(port, 'localhost', 2000),
|
||||
targetServer = startTargetServer(port);
|
||||
|
||||
testServers.latency = [];
|
||||
testServers.latency.push(proxyServer);
|
||||
testServers.latency.push(targetServer);
|
||||
|
||||
return proxyServer;
|
||||
};
|
||||
|
||||
vows.describe('node-http-proxy').addBatch({
|
||||
"A node-http-proxy": {
|
||||
"when instantiated directly": {
|
||||
"and an incoming request is proxied to the helloNode server" : {
|
||||
"An instance of HttpProxy": {
|
||||
"an incoming request to the helloNode server": {
|
||||
"with no latency" : {
|
||||
"and a valid target server": {
|
||||
topic: function () {
|
||||
var proxy = startTest(8082);
|
||||
proxy.addListener('proxy', this.callback);
|
||||
startProxyServer(8080, 8081, 'localhost'),
|
||||
startTargetServer(8081);
|
||||
var options = {
|
||||
method: 'GET',
|
||||
uri: 'http://localhost:8080'
|
||||
};
|
||||
|
||||
var client = http.createClient(8080, 'localhost');
|
||||
var request = client.request('GET', '/');
|
||||
request.end();
|
||||
request(options, this.callback);
|
||||
},
|
||||
"it should received 'hello world'": function (err, body) {
|
||||
"it should received 'hello world'": function (err, res, body) {
|
||||
assert.equal(body, 'hello world');
|
||||
testServers.noLatency.forEach(function (server) {
|
||||
server.close();
|
||||
})
|
||||
}
|
||||
},
|
||||
"and without a valid target server": {
|
||||
topic: function () {
|
||||
startProxyServer(8082, 9000, 'localhost');
|
||||
var options = {
|
||||
method: 'GET',
|
||||
uri: 'http://localhost:8082'
|
||||
};
|
||||
|
||||
request(options, this.callback);
|
||||
},
|
||||
"it should receive 500 response code": function (err, res, body) {
|
||||
assert.equal(res.statusCode, 500);
|
||||
}
|
||||
}
|
||||
},
|
||||
"with latency": {
|
||||
"and a valid target server": {
|
||||
topic: function () {
|
||||
var proxy = startTestWithLatency(8083);
|
||||
proxy.addListener('proxy', this.callback);
|
||||
startLatentProxyServer(8083, 8084, 'localhost', 1000),
|
||||
startTargetServer(8084);
|
||||
var options = {
|
||||
method: 'GET',
|
||||
uri: 'http://localhost:8083'
|
||||
};
|
||||
|
||||
var client = http.createClient(8081, 'localhost');
|
||||
var request = client.request('GET', '/');
|
||||
request.end();
|
||||
request(options, this.callback);
|
||||
},
|
||||
"it should receive 'hello world'": function (err, body) {
|
||||
"it should receive 'hello world'": function (err, res, body) {
|
||||
assert.equal(body, 'hello world');
|
||||
testServers.latency.forEach(function (server) {
|
||||
}
|
||||
},
|
||||
"and without a valid target server": {
|
||||
topic: function () {
|
||||
startLatentProxyServer(8085, 9000, 'localhost', 1000);
|
||||
var options = {
|
||||
method: 'GET',
|
||||
uri: 'http://localhost:8085'
|
||||
};
|
||||
|
||||
request(options, this.callback);
|
||||
},
|
||||
"it should receive 500 response code": function (err, res, body) {
|
||||
assert.equal(res.statusCode, 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}).addBatch({
|
||||
"An instance of HttpProxy": {
|
||||
"an incoming WebSocket request to the helloNode server": {
|
||||
"with no latency" : {
|
||||
// Remark: This test is not working
|
||||
/*topic: function () {
|
||||
startProxyServer(8086, 8087, 'localhost'),
|
||||
startTargetServer(8087);
|
||||
var options = {
|
||||
method: 'GET',
|
||||
uri: 'http://localhost:8086',
|
||||
headers: {
|
||||
'Upgrade': 'WebSocket',
|
||||
'Connection': 'WebSocket',
|
||||
'Host': 'localhost'
|
||||
}
|
||||
};
|
||||
|
||||
request(options, this.callback);
|
||||
},
|
||||
"it should receive 'hello world'": function (err, res, body) {
|
||||
assert.equal(body, 'hello world');
|
||||
}*/
|
||||
}
|
||||
}
|
||||
}
|
||||
}).addBatch({
|
||||
"When the tests are over": {
|
||||
topic: function () {
|
||||
testServers.forEach(function (server) {
|
||||
server.close();
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return testServers;
|
||||
},
|
||||
"the servers should clean up": function () {
|
||||
assert.isTrue(true);
|
||||
}
|
||||
}
|
||||
}).export(module);
|
||||
@ -1,54 +0,0 @@
|
||||
/*
|
||||
node-http-proxy-websocket-test.js: http proxy for node.js
|
||||
|
||||
Copyright (c) 2010 Fedor Indutny
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
var vows = require('vows'),
|
||||
sys = require('sys'),
|
||||
assert = require('assert'),
|
||||
http = require('http');
|
||||
|
||||
var httpProxy = require('./../lib/node-http-proxy');
|
||||
var testServers = {};
|
||||
|
||||
var server = httpProxy.createServer(function(req, res) {
|
||||
var p = new httpProxy.HttpProxy(req, res);
|
||||
|
||||
sys.puts('http request');
|
||||
|
||||
p.proxyRequest(8080, 'localhost');
|
||||
});
|
||||
|
||||
server.on('upgrade', function(req, socket, head) {
|
||||
var p = new httpProxy.HttpProxy(req, socket, head);
|
||||
|
||||
sys.puts('websocket request');
|
||||
|
||||
p.proxyWebSocketRequest(8080, 'localhost');
|
||||
});
|
||||
|
||||
server.listen(8000);
|
||||
|
||||
httpProxy.createServer(8080, 'localhost').listen(8001);
|
||||
|
||||
1
vendor/pool/main.js
vendored
1
vendor/pool/main.js
vendored
@ -1,5 +1,4 @@
|
||||
var sys = require('sys')
|
||||
, eyes = require('eyes')
|
||||
, http = require('http')
|
||||
, events = require('events')
|
||||
;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user