[tests] now each test use a different port to avoid some slow opening and closing ports

This commit is contained in:
cronopio 2013-10-22 15:57:52 -05:00
parent d60353f80b
commit c75d06c5f9

View File

@ -5,6 +5,17 @@ var httpProxy = require('../lib/http-proxy'),
io = require('socket.io'), io = require('socket.io'),
ioClient = require('socket.io-client'); ioClient = require('socket.io-client');
//
// Expose a port number generator.
// thanks to @3rd-Eden
//
var initialPort = 1024, gen = {};
Object.defineProperty(gen, 'port', {
get: function get() {
return initialPort++;
}
});
describe('lib/http-proxy.js', function() { describe('lib/http-proxy.js', function() {
describe('#createProxyServer', function() { describe('#createProxyServer', function() {
@ -32,44 +43,45 @@ describe('lib/http-proxy.js', function() {
describe('#createProxyServer with forward options and using web-incoming passes', function () { describe('#createProxyServer with forward options and using web-incoming passes', function () {
it('should pipe the request using web-incoming#stream method', function (done) { it('should pipe the request using web-incoming#stream method', function (done) {
var ports = { source: gen.port, proxy: gen.port };
var proxy = httpProxy.createProxyServer({ var proxy = httpProxy.createProxyServer({
forward: 'http://127.0.0.1:8080' forward: 'http://127.0.0.1:' + ports.source
}).listen('8081') }).listen(ports.proxy);
var source = http.createServer(function(req, res) { var source = http.createServer(function(req, res) {
expect(req.method).to.eql('GET'); expect(req.method).to.eql('GET');
expect(req.headers.host.split(':')[1]).to.eql('8081'); expect(req.headers.host.split(':')[1]).to.eql(ports.proxy);
source.close(); source.close();
proxy._server.close(); proxy._server.close();
done(); done();
}); });
source.listen('8080'); source.listen(ports.source);
http.request('http://127.0.0.1:' + ports.proxy, function() {}).end();
http.request('http://127.0.0.1:8081', function() {}).end();
}) })
}); });
describe('#createProxyServer using the web-incoming passes', function () { describe('#createProxyServer using the web-incoming passes', function () {
it('should make the request on pipe and finish it', function(done) { it('should make the request on pipe and finish it', function(done) {
var ports = { source: gen.port, proxy: gen.port };
var proxy = httpProxy.createProxyServer({ var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080' target: 'http://127.0.0.1:' + ports.source
}).listen('8081'); }).listen(ports.proxy);
var source = http.createServer(function(req, res) { var source = http.createServer(function(req, res) {
expect(req.method).to.eql('POST'); expect(req.method).to.eql('POST');
expect(req.headers['x-forwarded-for']).to.eql('127.0.0.1'); expect(req.headers['x-forwarded-for']).to.eql('127.0.0.1');
expect(req.headers.host.split(':')[1]).to.eql('8081'); expect(req.headers.host.split(':')[1]).to.eql(ports.proxy);
source.close(); source.close();
proxy._server.close(); proxy._server.close();
done(); done();
}); });
source.listen('8080'); source.listen(ports.source);
http.request({ http.request({
hostname: '127.0.0.1', hostname: '127.0.0.1',
port: '8081', port: ports.proxy,
method: 'POST', method: 'POST',
headers: { headers: {
'x-forwarded-for': '127.0.0.1' 'x-forwarded-for': '127.0.0.1'
@ -80,28 +92,29 @@ describe('lib/http-proxy.js', function() {
describe('#createProxyServer using the web-incoming passes', function () { describe('#createProxyServer using the web-incoming passes', function () {
it('should make the request, handle response and finish it', function(done) { it('should make the request, handle response and finish it', function(done) {
var ports = { source: gen.port, proxy: gen.port };
var proxy = httpProxy.createProxyServer({ var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080' target: 'http://127.0.0.1:' + ports.source
}).listen('8081'); }).listen(ports.proxy);
var source = http.createServer(function(req, res) { var source = http.createServer(function(req, res) {
expect(req.method).to.eql('GET'); expect(req.method).to.eql('GET');
expect(req.headers.host.split(':')[1]).to.eql('8081'); expect(req.headers.host.split(':')[1]).to.eql(ports.proxy);
res.writeHead(200, {'Content-Type': 'text/plain'}) res.writeHead(200, {'Content-Type': 'text/plain'})
res.end('Hello from ' + source.address().port); res.end('Hello from ' + source.address().port);
}); });
source.listen('8080'); source.listen(ports.source);
http.request({ http.request({
hostname: '127.0.0.1', hostname: '127.0.0.1',
port: '8081', port: ports.proxy,
method: 'GET', method: 'GET'
}, function(res) { }, function(res) {
expect(res.statusCode).to.eql(200); expect(res.statusCode).to.eql(200);
res.on('data', function (data) { res.on('data', function (data) {
expect(data.toString()).to.eql('Hello from 8080'); expect(data.toString()).to.eql('Hello from ' + ports.source);
}); });
res.on('end', function () { res.on('end', function () {
@ -115,8 +128,9 @@ describe('lib/http-proxy.js', function() {
describe('#createProxyServer() method with error response', function () { describe('#createProxyServer() method with error response', function () {
it('should make the request and emit the error event', function(done) { it('should make the request and emit the error event', function(done) {
var ports = { source: gen.port, proxy: gen.port };
var proxy = httpProxy.createProxyServer({ var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080' target: 'http://127.0.0.1:' + ports.source
}); });
proxy.on('error', function (err) { proxy.on('error', function (err) {
@ -126,11 +140,11 @@ describe('lib/http-proxy.js', function() {
done(); done();
}) })
proxy.listen('8081'); proxy.listen(ports.proxy);
http.request({ http.request({
hostname: '127.0.0.1', hostname: '127.0.0.1',
port: '8081', port: ports.proxy,
method: 'GET', method: 'GET',
}, function() {}).end(); }, function() {}).end();
}); });
@ -139,10 +153,11 @@ describe('lib/http-proxy.js', function() {
describe('#createProxyServer setting the correct timeout value', function () { describe('#createProxyServer setting the correct timeout value', function () {
it('should hang up the socket at the timeout', function (done) { it('should hang up the socket at the timeout', function (done) {
this.timeout(30); this.timeout(30);
var ports = { source: gen.port, proxy: gen.port };
var proxy = httpProxy.createProxyServer({ var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080', target: 'http://127.0.0.1:' + ports.source,
timeout: 3 timeout: 3
}).listen('8081'); }).listen(ports.proxy);
var source = http.createServer(function(req, res) { var source = http.createServer(function(req, res) {
setTimeout(function () { setTimeout(function () {
@ -150,11 +165,11 @@ describe('lib/http-proxy.js', function() {
}, 5) }, 5)
}); });
source.listen('8080'); source.listen(ports.source);
var testReq = http.request({ var testReq = http.request({
hostname: '127.0.0.1', hostname: '127.0.0.1',
port: '8081', port: ports.proxy,
method: 'GET', method: 'GET',
}, function() {}); }, function() {});
@ -217,13 +232,14 @@ describe('lib/http-proxy.js', function() {
describe('#createProxyServer using the ws-incoming passes', function () { describe('#createProxyServer using the ws-incoming passes', function () {
it('should proxy the websockets stream', function (done) { it('should proxy the websockets stream', function (done) {
var ports = { source: gen.port, proxy: gen.port };
var proxy = httpProxy.createProxyServer({ var proxy = httpProxy.createProxyServer({
target: 'ws://127.0.0.1:8080', target: 'ws://127.0.0.1:' + ports.source,
ws: true ws: true
}), }),
proxyServer = proxy.listen('8081'), proxyServer = proxy.listen(ports.proxy),
destiny = new ws.Server({ port: 8080 }, function () { destiny = new ws.Server({ port: ports.source }, function () {
var client = new ws('ws://127.0.0.1:8081'); var client = new ws('ws://127.0.0.1:' + ports.proxy);
client.on('open', function () { client.on('open', function () {
client.send('hello there'); client.send('hello there');
@ -249,13 +265,14 @@ describe('lib/http-proxy.js', function() {
describe('#createProxyServer using the ws-incoming passes', function () { describe('#createProxyServer using the ws-incoming passes', function () {
it('should proxy a socket.io stream', function (done) { it('should proxy a socket.io stream', function (done) {
var ports = { source: gen.port, proxy: gen.port };
var proxy = httpProxy.createProxyServer({ var proxy = httpProxy.createProxyServer({
target: 'ws://127.0.0.1:8080', target: 'ws://127.0.0.1:' + ports.source,
ws: true ws: true
}), }),
proxyServer = proxy.listen('8081'), proxyServer = proxy.listen(ports.proxy),
destiny = io.listen(8080, function () { destiny = io.listen(ports.source, function () {
var client = ioClient.connect('ws://127.0.0.1:8081'); var client = ioClient.connect('ws://127.0.0.1:' + ports.proxy);
client.on('connect', function () { client.on('connect', function () {
client.emit('incoming', 'hello there'); client.emit('incoming', 'hello there');