Skip to content

Commit c75d06c

Browse files
committed
[tests] now each test use a different port to avoid some slow opening and closing ports
1 parent d60353f commit c75d06c

File tree

1 file changed

+50
-33
lines changed

1 file changed

+50
-33
lines changed

Diff for: test/lib-http-proxy-test.js

+50-33
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ var httpProxy = require('../lib/http-proxy'),
55
io = require('socket.io'),
66
ioClient = require('socket.io-client');
77

8+
//
9+
// Expose a port number generator.
10+
// thanks to @3rd-Eden
11+
//
12+
var initialPort = 1024, gen = {};
13+
Object.defineProperty(gen, 'port', {
14+
get: function get() {
15+
return initialPort++;
16+
}
17+
});
18+
819

920
describe('lib/http-proxy.js', function() {
1021
describe('#createProxyServer', function() {
@@ -32,44 +43,45 @@ describe('lib/http-proxy.js', function() {
3243

3344
describe('#createProxyServer with forward options and using web-incoming passes', function () {
3445
it('should pipe the request using web-incoming#stream method', function (done) {
46+
var ports = { source: gen.port, proxy: gen.port };
3547
var proxy = httpProxy.createProxyServer({
36-
forward: 'http://127.0.0.1:8080'
37-
}).listen('8081')
48+
forward: 'http://127.0.0.1:' + ports.source
49+
}).listen(ports.proxy);
3850

3951
var source = http.createServer(function(req, res) {
4052
expect(req.method).to.eql('GET');
41-
expect(req.headers.host.split(':')[1]).to.eql('8081');
53+
expect(req.headers.host.split(':')[1]).to.eql(ports.proxy);
4254
source.close();
4355
proxy._server.close();
4456
done();
4557
});
4658

47-
source.listen('8080');
48-
49-
http.request('http://127.0.0.1:8081', function() {}).end();
59+
source.listen(ports.source);
60+
http.request('http://127.0.0.1:' + ports.proxy, function() {}).end();
5061
})
5162
});
5263

5364
describe('#createProxyServer using the web-incoming passes', function () {
5465
it('should make the request on pipe and finish it', function(done) {
66+
var ports = { source: gen.port, proxy: gen.port };
5567
var proxy = httpProxy.createProxyServer({
56-
target: 'http://127.0.0.1:8080'
57-
}).listen('8081');
68+
target: 'http://127.0.0.1:' + ports.source
69+
}).listen(ports.proxy);
5870

5971
var source = http.createServer(function(req, res) {
6072
expect(req.method).to.eql('POST');
6173
expect(req.headers['x-forwarded-for']).to.eql('127.0.0.1');
62-
expect(req.headers.host.split(':')[1]).to.eql('8081');
74+
expect(req.headers.host.split(':')[1]).to.eql(ports.proxy);
6375
source.close();
6476
proxy._server.close();
6577
done();
6678
});
6779

68-
source.listen('8080');
80+
source.listen(ports.source);
6981

7082
http.request({
7183
hostname: '127.0.0.1',
72-
port: '8081',
84+
port: ports.proxy,
7385
method: 'POST',
7486
headers: {
7587
'x-forwarded-for': '127.0.0.1'
@@ -80,28 +92,29 @@ describe('lib/http-proxy.js', function() {
8092

8193
describe('#createProxyServer using the web-incoming passes', function () {
8294
it('should make the request, handle response and finish it', function(done) {
95+
var ports = { source: gen.port, proxy: gen.port };
8396
var proxy = httpProxy.createProxyServer({
84-
target: 'http://127.0.0.1:8080'
85-
}).listen('8081');
97+
target: 'http://127.0.0.1:' + ports.source
98+
}).listen(ports.proxy);
8699

87100
var source = http.createServer(function(req, res) {
88101
expect(req.method).to.eql('GET');
89-
expect(req.headers.host.split(':')[1]).to.eql('8081');
102+
expect(req.headers.host.split(':')[1]).to.eql(ports.proxy);
90103
res.writeHead(200, {'Content-Type': 'text/plain'})
91104
res.end('Hello from ' + source.address().port);
92105
});
93106

94-
source.listen('8080');
107+
source.listen(ports.source);
95108

96109
http.request({
97110
hostname: '127.0.0.1',
98-
port: '8081',
99-
method: 'GET',
111+
port: ports.proxy,
112+
method: 'GET'
100113
}, function(res) {
101114
expect(res.statusCode).to.eql(200);
102115

103116
res.on('data', function (data) {
104-
expect(data.toString()).to.eql('Hello from 8080');
117+
expect(data.toString()).to.eql('Hello from ' + ports.source);
105118
});
106119

107120
res.on('end', function () {
@@ -115,8 +128,9 @@ describe('lib/http-proxy.js', function() {
115128

116129
describe('#createProxyServer() method with error response', function () {
117130
it('should make the request and emit the error event', function(done) {
131+
var ports = { source: gen.port, proxy: gen.port };
118132
var proxy = httpProxy.createProxyServer({
119-
target: 'http://127.0.0.1:8080'
133+
target: 'http://127.0.0.1:' + ports.source
120134
});
121135

122136
proxy.on('error', function (err) {
@@ -126,11 +140,11 @@ describe('lib/http-proxy.js', function() {
126140
done();
127141
})
128142

129-
proxy.listen('8081');
143+
proxy.listen(ports.proxy);
130144

131145
http.request({
132146
hostname: '127.0.0.1',
133-
port: '8081',
147+
port: ports.proxy,
134148
method: 'GET',
135149
}, function() {}).end();
136150
});
@@ -139,22 +153,23 @@ describe('lib/http-proxy.js', function() {
139153
describe('#createProxyServer setting the correct timeout value', function () {
140154
it('should hang up the socket at the timeout', function (done) {
141155
this.timeout(30);
156+
var ports = { source: gen.port, proxy: gen.port };
142157
var proxy = httpProxy.createProxyServer({
143-
target: 'http://127.0.0.1:8080',
158+
target: 'http://127.0.0.1:' + ports.source,
144159
timeout: 3
145-
}).listen('8081');
160+
}).listen(ports.proxy);
146161

147162
var source = http.createServer(function(req, res) {
148163
setTimeout(function () {
149164
res.end('At this point the socket should be closed');
150165
}, 5)
151166
});
152167

153-
source.listen('8080');
168+
source.listen(ports.source);
154169

155170
var testReq = http.request({
156171
hostname: '127.0.0.1',
157-
port: '8081',
172+
port: ports.proxy,
158173
method: 'GET',
159174
}, function() {});
160175

@@ -217,13 +232,14 @@ describe('lib/http-proxy.js', function() {
217232

218233
describe('#createProxyServer using the ws-incoming passes', function () {
219234
it('should proxy the websockets stream', function (done) {
235+
var ports = { source: gen.port, proxy: gen.port };
220236
var proxy = httpProxy.createProxyServer({
221-
target: 'ws://127.0.0.1:8080',
237+
target: 'ws://127.0.0.1:' + ports.source,
222238
ws: true
223239
}),
224-
proxyServer = proxy.listen('8081'),
225-
destiny = new ws.Server({ port: 8080 }, function () {
226-
var client = new ws('ws://127.0.0.1:8081');
240+
proxyServer = proxy.listen(ports.proxy),
241+
destiny = new ws.Server({ port: ports.source }, function () {
242+
var client = new ws('ws://127.0.0.1:' + ports.proxy);
227243

228244
client.on('open', function () {
229245
client.send('hello there');
@@ -249,13 +265,14 @@ describe('lib/http-proxy.js', function() {
249265

250266
describe('#createProxyServer using the ws-incoming passes', function () {
251267
it('should proxy a socket.io stream', function (done) {
268+
var ports = { source: gen.port, proxy: gen.port };
252269
var proxy = httpProxy.createProxyServer({
253-
target: 'ws://127.0.0.1:8080',
270+
target: 'ws://127.0.0.1:' + ports.source,
254271
ws: true
255272
}),
256-
proxyServer = proxy.listen('8081'),
257-
destiny = io.listen(8080, function () {
258-
var client = ioClient.connect('ws://127.0.0.1:8081');
273+
proxyServer = proxy.listen(ports.proxy),
274+
destiny = io.listen(ports.source, function () {
275+
var client = ioClient.connect('ws://127.0.0.1:' + ports.proxy);
259276

260277
client.on('connect', function () {
261278
client.emit('incoming', 'hello there');

0 commit comments

Comments
 (0)