@@ -15,40 +15,24 @@ var fs = require('fs'),
15
15
websocket = require ( './../vendor/websocket' ) ,
16
16
httpProxy = require ( './../lib/node-http-proxy' ) ;
17
17
18
- function merge ( target ) {
19
- var objs = Array . prototype . slice . call ( arguments , 1 ) ;
20
- objs . forEach ( function ( o ) {
21
- Object . keys ( o ) . forEach ( function ( attr ) {
22
- if ( ! o . __lookupGetter__ ( attr ) ) {
23
- target [ attr ] = o [ attr ] ;
24
- }
25
- } ) ;
26
- } ) ;
27
- return target ;
28
- }
29
-
30
18
var loadHttps = exports . loadHttps = function ( ) {
31
19
return {
32
20
key : fs . readFileSync ( path . join ( __dirname , 'fixtures' , 'agent2-key.pem' ) , 'utf8' ) ,
33
21
cert : fs . readFileSync ( path . join ( __dirname , 'fixtures' , 'agent2-cert.pem' ) , 'utf8' )
34
22
} ;
35
23
} ;
36
24
37
- var TestRunner = exports . TestRunner = function ( protocol , target ) {
38
- this . options = { } ;
39
- this . options . target = { } ;
40
- this . protocol = protocol ;
41
- this . target = target ;
42
- this . testServers = [ ] ;
25
+ var TestRunner = exports . TestRunner = function ( source , target ) {
26
+ this . source = { protocol : source } ,
27
+ this . target = { protocol : target } ;
28
+ this . testServers = [ ] ;
43
29
44
- if ( protocol === 'https' ) {
45
- this . options . https = loadHttps ( ) ;
30
+ if ( source === 'https' ) {
31
+ this . source . https = loadHttps ( ) ;
46
32
}
47
33
48
34
if ( target === 'https' ) {
49
- this . options . target = {
50
- https : loadHttps ( )
51
- } ;
35
+ this . target . https = loadHttps ( ) ;
52
36
}
53
37
} ;
54
38
@@ -64,11 +48,12 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx
64
48
65
49
options = {
66
50
method : 'GET' ,
67
- uri : self . protocol + '://localhost:' + proxyPort ,
51
+ uri : self . source . protocol + '://localhost:' + proxyPort ,
68
52
headers : {
69
53
host : host
70
54
}
71
55
} ;
56
+
72
57
73
58
function startTest ( ) {
74
59
if ( port ) {
@@ -94,7 +79,7 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx
94
79
95
80
TestRunner . prototype . assertResponseCode = function ( proxyPort , statusCode , createProxy ) {
96
81
var assertion = "should receive " + statusCode + " responseCode" ,
97
- protocol = this . protocol ;
82
+ protocol = this . source . protocol ;
98
83
99
84
var test = {
100
85
topic : function ( ) {
@@ -202,8 +187,7 @@ TestRunner.prototype.webSocketTestWithTable = function (options) {
202
187
//
203
188
TestRunner . prototype . startProxyServer = function ( port , targetPort , host , callback ) {
204
189
var that = this ,
205
- options = that . options ,
206
- proxyServer = httpProxy . createServer ( targetPort , host , options ) ;
190
+ proxyServer = httpProxy . createServer ( host , targetPort , this . getOptions ( ) ) ;
207
191
208
192
proxyServer . listen ( port , function ( ) {
209
193
that . testServers . push ( proxyServer ) ;
@@ -215,21 +199,19 @@ TestRunner.prototype.startProxyServer = function (port, targetPort, host, callba
215
199
// Creates the reverse proxy server with a specified latency
216
200
//
217
201
TestRunner . prototype . startLatentProxyServer = function ( port , targetPort , host , latency , callback ) {
202
+ //
218
203
// Initialize the nodeProxy and start proxying the request
204
+ //
219
205
var that = this ,
220
206
proxyServer ;
221
-
222
- proxyServer = httpProxy . createServer ( function ( req , res , proxy ) {
223
- var buffer = proxy . buffer ( req ) ;
207
+
208
+ proxyServer = httpProxy . createServer ( host , targetPort , function ( req , res , proxy ) {
209
+ var buffer = httpProxy . buffer ( req ) ;
224
210
225
211
setTimeout ( function ( ) {
226
- proxy . proxyRequest ( req , res , {
227
- port : targetPort ,
228
- host : host ,
229
- buffer : buffer
230
- } ) ;
212
+ proxy . proxyRequest ( req , res , buffer ) ;
231
213
} , latency ) ;
232
- } , this . options ) ;
214
+ } , this . getOptions ( ) ) ;
233
215
234
216
proxyServer . listen ( port , function ( ) {
235
217
that . testServers . push ( proxyServer ) ;
@@ -242,7 +224,7 @@ TestRunner.prototype.startLatentProxyServer = function (port, targetPort, host,
242
224
//
243
225
TestRunner . prototype . startProxyServerWithTable = function ( port , options , callback ) {
244
226
var that = this ,
245
- proxyServer = httpProxy . createServer ( merge ( { } , options , this . options ) ) ;
227
+ proxyServer = httpProxy . createServer ( merge ( { } , options , this . getOptions ( ) ) ) ;
246
228
247
229
proxyServer . listen ( port , function ( ) {
248
230
that . testServers . push ( proxyServer ) ;
@@ -260,7 +242,7 @@ TestRunner.prototype.startProxyServerWithTableAndLatency = function (port, laten
260
242
// Initialize the nodeProxy and start proxying the request
261
243
//
262
244
var that = this ,
263
- proxy = new httpProxy . HttpProxy ( merge ( { } , options , that . options ) ) ,
245
+ proxy = new httpProxy . HttpProxy ( merge ( { } , options , this . getOptions ( ) ) ) ,
264
246
proxyServer ;
265
247
266
248
var handler = function ( req , res ) {
@@ -289,7 +271,7 @@ TestRunner.prototype.startProxyServerWithTableAndLatency = function (port, laten
289
271
//
290
272
TestRunner . prototype . startProxyServerWithForwarding = function ( port , targetPort , host , options , callback ) {
291
273
var that = this ,
292
- proxyServer = httpProxy . createServer ( targetPort , host , merge ( { } , options , this . options ) ) ;
274
+ proxyServer = httpProxy . createServer ( targetPort , host , merge ( { } , options , this . getOptions ( ) ) ) ;
293
275
294
276
proxyServer . listen ( port , function ( ) {
295
277
that . testServers . push ( proxyServer ) ;
@@ -310,9 +292,9 @@ TestRunner.prototype.startTargetServer = function (port, output, callback) {
310
292
res . write ( output ) ;
311
293
res . end ( ) ;
312
294
} ;
313
-
314
- targetServer = this . options . target . https
315
- ? https . createServer ( this . options . target . https , handler )
295
+
296
+ targetServer = this . target . https
297
+ ? https . createServer ( this . target . https , handler )
316
298
: http . createServer ( handler ) ;
317
299
318
300
targetServer . listen ( port , function ( ) {
@@ -331,3 +313,42 @@ TestRunner.prototype.closeServers = function () {
331
313
332
314
return this . testServers ;
333
315
} ;
316
+
317
+ //
318
+ // Creates a new instance of the options to
319
+ // pass to `httpProxy.createServer()`
320
+ //
321
+ TestRunner . prototype . getOptions = function ( ) {
322
+ return {
323
+ https : clone ( this . source . https ) ,
324
+ target : {
325
+ https : clone ( this . target . https )
326
+ }
327
+ } ;
328
+ } ;
329
+
330
+ //
331
+ // ### @private function clone (object)
332
+ // #### @object {Object} Object to clone
333
+ // Shallow clones the specified object.
334
+ //
335
+ function clone ( object ) {
336
+ if ( ! object ) { return null }
337
+
338
+ return Object . keys ( object ) . reduce ( function ( obj , k ) {
339
+ obj [ k ] = object [ k ] ;
340
+ return obj ;
341
+ } , { } ) ;
342
+ }
343
+
344
+ function merge ( target ) {
345
+ var objs = Array . prototype . slice . call ( arguments , 1 ) ;
346
+ objs . forEach ( function ( o ) {
347
+ Object . keys ( o ) . forEach ( function ( attr ) {
348
+ if ( ! o . __lookupGetter__ ( attr ) ) {
349
+ target [ attr ] = o [ attr ] ;
350
+ }
351
+ } ) ;
352
+ } ) ;
353
+ return target ;
354
+ }
0 commit comments