@@ -5,6 +5,17 @@ var httpProxy = require('../lib/http-proxy'),
5
5
io = require ( 'socket.io' ) ,
6
6
ioClient = require ( 'socket.io-client' ) ;
7
7
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
+
8
19
9
20
describe ( 'lib/http-proxy.js' , function ( ) {
10
21
describe ( '#createProxyServer' , function ( ) {
@@ -32,44 +43,45 @@ describe('lib/http-proxy.js', function() {
32
43
33
44
describe ( '#createProxyServer with forward options and using web-incoming passes' , function ( ) {
34
45
it ( 'should pipe the request using web-incoming#stream method' , function ( done ) {
46
+ var ports = { source : gen . port , proxy : gen . port } ;
35
47
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 ) ;
38
50
39
51
var source = http . createServer ( function ( req , res ) {
40
52
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 ) ;
42
54
source . close ( ) ;
43
55
proxy . _server . close ( ) ;
44
56
done ( ) ;
45
57
} ) ;
46
58
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 ( ) ;
50
61
} )
51
62
} ) ;
52
63
53
64
describe ( '#createProxyServer using the web-incoming passes' , function ( ) {
54
65
it ( 'should make the request on pipe and finish it' , function ( done ) {
66
+ var ports = { source : gen . port , proxy : gen . port } ;
55
67
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 ) ;
58
70
59
71
var source = http . createServer ( function ( req , res ) {
60
72
expect ( req . method ) . to . eql ( 'POST' ) ;
61
73
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 ) ;
63
75
source . close ( ) ;
64
76
proxy . _server . close ( ) ;
65
77
done ( ) ;
66
78
} ) ;
67
79
68
- source . listen ( '8080' ) ;
80
+ source . listen ( ports . source ) ;
69
81
70
82
http . request ( {
71
83
hostname : '127.0.0.1' ,
72
- port : '8081' ,
84
+ port : ports . proxy ,
73
85
method : 'POST' ,
74
86
headers : {
75
87
'x-forwarded-for' : '127.0.0.1'
@@ -80,28 +92,29 @@ describe('lib/http-proxy.js', function() {
80
92
81
93
describe ( '#createProxyServer using the web-incoming passes' , function ( ) {
82
94
it ( 'should make the request, handle response and finish it' , function ( done ) {
95
+ var ports = { source : gen . port , proxy : gen . port } ;
83
96
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 ) ;
86
99
87
100
var source = http . createServer ( function ( req , res ) {
88
101
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 ) ;
90
103
res . writeHead ( 200 , { 'Content-Type' : 'text/plain' } )
91
104
res . end ( 'Hello from ' + source . address ( ) . port ) ;
92
105
} ) ;
93
106
94
- source . listen ( '8080' ) ;
107
+ source . listen ( ports . source ) ;
95
108
96
109
http . request ( {
97
110
hostname : '127.0.0.1' ,
98
- port : '8081' ,
99
- method : 'GET' ,
111
+ port : ports . proxy ,
112
+ method : 'GET'
100
113
} , function ( res ) {
101
114
expect ( res . statusCode ) . to . eql ( 200 ) ;
102
115
103
116
res . on ( 'data' , function ( data ) {
104
- expect ( data . toString ( ) ) . to . eql ( 'Hello from 8080' ) ;
117
+ expect ( data . toString ( ) ) . to . eql ( 'Hello from ' + ports . source ) ;
105
118
} ) ;
106
119
107
120
res . on ( 'end' , function ( ) {
@@ -115,8 +128,9 @@ describe('lib/http-proxy.js', function() {
115
128
116
129
describe ( '#createProxyServer() method with error response' , function ( ) {
117
130
it ( 'should make the request and emit the error event' , function ( done ) {
131
+ var ports = { source : gen . port , proxy : gen . port } ;
118
132
var proxy = httpProxy . createProxyServer ( {
119
- target : 'http://127.0.0.1:8080'
133
+ target : 'http://127.0.0.1:' + ports . source
120
134
} ) ;
121
135
122
136
proxy . on ( 'error' , function ( err ) {
@@ -126,11 +140,11 @@ describe('lib/http-proxy.js', function() {
126
140
done ( ) ;
127
141
} )
128
142
129
- proxy . listen ( '8081' ) ;
143
+ proxy . listen ( ports . proxy ) ;
130
144
131
145
http . request ( {
132
146
hostname : '127.0.0.1' ,
133
- port : '8081' ,
147
+ port : ports . proxy ,
134
148
method : 'GET' ,
135
149
} , function ( ) { } ) . end ( ) ;
136
150
} ) ;
@@ -139,22 +153,23 @@ describe('lib/http-proxy.js', function() {
139
153
describe ( '#createProxyServer setting the correct timeout value' , function ( ) {
140
154
it ( 'should hang up the socket at the timeout' , function ( done ) {
141
155
this . timeout ( 30 ) ;
156
+ var ports = { source : gen . port , proxy : gen . port } ;
142
157
var proxy = httpProxy . createProxyServer ( {
143
- target : 'http://127.0.0.1:8080' ,
158
+ target : 'http://127.0.0.1:' + ports . source ,
144
159
timeout : 3
145
- } ) . listen ( '8081' ) ;
160
+ } ) . listen ( ports . proxy ) ;
146
161
147
162
var source = http . createServer ( function ( req , res ) {
148
163
setTimeout ( function ( ) {
149
164
res . end ( 'At this point the socket should be closed' ) ;
150
165
} , 5 )
151
166
} ) ;
152
167
153
- source . listen ( '8080' ) ;
168
+ source . listen ( ports . source ) ;
154
169
155
170
var testReq = http . request ( {
156
171
hostname : '127.0.0.1' ,
157
- port : '8081' ,
172
+ port : ports . proxy ,
158
173
method : 'GET' ,
159
174
} , function ( ) { } ) ;
160
175
@@ -217,13 +232,14 @@ describe('lib/http-proxy.js', function() {
217
232
218
233
describe ( '#createProxyServer using the ws-incoming passes' , function ( ) {
219
234
it ( 'should proxy the websockets stream' , function ( done ) {
235
+ var ports = { source : gen . port , proxy : gen . port } ;
220
236
var proxy = httpProxy . createProxyServer ( {
221
- target : 'ws://127.0.0.1:8080' ,
237
+ target : 'ws://127.0.0.1:' + ports . source ,
222
238
ws : true
223
239
} ) ,
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 ) ;
227
243
228
244
client . on ( 'open' , function ( ) {
229
245
client . send ( 'hello there' ) ;
@@ -249,13 +265,14 @@ describe('lib/http-proxy.js', function() {
249
265
250
266
describe ( '#createProxyServer using the ws-incoming passes' , function ( ) {
251
267
it ( 'should proxy a socket.io stream' , function ( done ) {
268
+ var ports = { source : gen . port , proxy : gen . port } ;
252
269
var proxy = httpProxy . createProxyServer ( {
253
- target : 'ws://127.0.0.1:8080' ,
270
+ target : 'ws://127.0.0.1:' + ports . source ,
254
271
ws : true
255
272
} ) ,
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 ) ;
259
276
260
277
client . on ( 'connect' , function ( ) {
261
278
client . emit ( 'incoming' , 'hello there' ) ;
0 commit comments