@@ -37,7 +37,7 @@ var TestRunner = exports.TestRunner = function (protocol) {
37
37
this . options = { } ;
38
38
this . protocol = protocol ;
39
39
this . testServers = [ ] ;
40
-
40
+
41
41
if ( protocol === 'https' ) {
42
42
this . options . https = loadHttps ( ) ;
43
43
}
@@ -47,17 +47,17 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx
47
47
var self = this ,
48
48
assertion = "should receive 'hello " + host + "'" ,
49
49
output = 'hello ' + host ;
50
-
50
+
51
51
var test = {
52
52
topic : function ( ) {
53
53
var that = this , options = {
54
- method : 'GET' ,
54
+ method : 'GET' ,
55
55
uri : self . protocol + '://localhost:' + proxyPort ,
56
56
headers : {
57
57
host : host
58
58
}
59
59
} ;
60
-
60
+
61
61
function startTest ( ) {
62
62
if ( port ) {
63
63
return self . startTargetServer ( port , output , function ( ) {
@@ -67,74 +67,77 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx
67
67
68
68
request ( options , this . callback ) ;
69
69
}
70
-
70
+
71
71
return createProxy ? createProxy ( startTest ) : startTest ( ) ;
72
72
}
73
73
} ;
74
-
75
- test [ assertion ] = function ( err , res , body ) { ;
74
+
75
+ test [ assertion ] = function ( err , res , body ) {
76
76
assert . isNull ( err ) ;
77
77
assert . equal ( body , output ) ;
78
78
} ;
79
-
79
+
80
80
return test ;
81
81
} ;
82
82
83
83
TestRunner . prototype . assertResponseCode = function ( proxyPort , statusCode , createProxy ) {
84
- var assertion = "should receive " + statusCode + " responseCode" ;
85
-
84
+ var assertion = "should receive " + statusCode + " responseCode" ,
85
+ protocol = this . protocol ;
86
+
86
87
var test = {
87
88
topic : function ( ) {
88
89
var that = this , options = {
89
- method : 'GET' ,
90
- uri : 'http ://localhost:' + proxyPort ,
90
+ method : 'GET' ,
91
+ uri : protocol + ' ://localhost:' + proxyPort ,
91
92
headers : {
92
93
host : 'unknown.com'
93
94
}
94
95
} ;
95
-
96
+
96
97
if ( createProxy ) {
97
98
return createProxy ( function ( ) {
98
- request ( options , that . callback ) ;
99
+ request ( options , that . callback ) ;
99
100
} ) ;
100
101
}
101
-
102
+
102
103
request ( options , this . callback ) ;
103
104
}
104
105
} ;
105
-
106
+
106
107
test [ assertion ] = function ( err , res , body ) {
107
108
assert . isNull ( err ) ;
108
109
assert . equal ( res . statusCode , statusCode ) ;
109
110
} ;
110
-
111
+
111
112
return test ;
112
113
} ;
113
114
114
115
//
115
116
// Creates the reverse proxy server
116
117
//
117
118
TestRunner . prototype . startProxyServer = function ( port , targetPort , host , callback ) {
118
- var that = this , proxyServer = httpProxy . createServer ( targetPort , host ) ;
119
-
119
+ var that = this ,
120
+ options = that . options ,
121
+ proxyServer = httpProxy . createServer ( targetPort , host , options ) ;
122
+
120
123
proxyServer . listen ( port , function ( ) {
121
124
that . testServers . push ( proxyServer ) ;
122
125
callback ( null , proxyServer ) ;
123
- } ) ;
126
+ } ) ;
124
127
} ;
125
128
126
- //
129
+ //
127
130
// Creates the reverse proxy server with a specified latency
128
131
//
129
132
TestRunner . prototype . startLatentProxyServer = function ( port , targetPort , host , latency , callback ) {
130
133
// Initialize the nodeProxy and start proxying the request
131
134
var that = this , proxyServer = httpProxy . createServer ( function ( req , res , proxy ) {
132
135
var buffer = proxy . buffer ( req ) ;
133
-
136
+
134
137
setTimeout ( function ( ) {
135
138
proxy . proxyRequest ( req , res , {
136
- port : targetPort ,
137
- host : host ,
139
+ port : targetPort ,
140
+ host : host ,
138
141
buffer : buffer
139
142
} ) ;
140
143
} , latency ) ;
@@ -150,12 +153,12 @@ TestRunner.prototype.startLatentProxyServer = function (port, targetPort, host,
150
153
// Creates the reverse proxy server with a ProxyTable
151
154
//
152
155
TestRunner . prototype . startProxyServerWithTable = function ( port , options , callback ) {
153
- var that = this , proxyServer = httpProxy . createServer ( merge ( { } , options , this . options ) ) ;
156
+ var that = this , proxyServer = httpProxy . createServer ( merge ( { } , options , this . options ) ) ;
154
157
proxyServer . listen ( port , function ( ) {
155
158
that . testServers . push ( proxyServer ) ;
156
159
callback ( ) ;
157
160
} ) ;
158
-
161
+
159
162
return proxyServer ;
160
163
} ;
161
164
@@ -164,29 +167,36 @@ TestRunner.prototype.startProxyServerWithTable = function (port, options, callba
164
167
//
165
168
TestRunner . prototype . startProxyServerWithTableAndLatency = function ( port , latency , options , callback ) {
166
169
// Initialize the nodeProxy and start proxying the request
167
- var proxyServer , that = this , proxy = new httpProxy . HttpProxy ( merge ( { } , options , this . options ) ) ;
168
- proxyServer = http . createServer ( function ( req , res ) {
170
+ var proxyServer ,
171
+ that = this ,
172
+ proxy = new httpProxy . HttpProxy ( merge ( { } , options , that . options ) ) ;
173
+
174
+ var handler = function ( req , res ) {
169
175
var buffer = proxy . buffer ( req ) ;
170
176
setTimeout ( function ( ) {
171
177
proxy . proxyRequest ( req , res , {
172
178
buffer : buffer
173
179
} ) ;
174
180
} , latency ) ;
175
- } , this . options ) ;
176
-
181
+ } ;
182
+
183
+ proxyServer = that . options . https
184
+ ? https . createServer ( that . options . https , handler , that . options )
185
+ : http . createServer ( handler , that . options ) ;
186
+
177
187
proxyServer . listen ( port , function ( ) {
178
188
that . testServers . push ( proxyServer ) ;
179
189
callback ( ) ;
180
190
} ) ;
181
-
191
+
182
192
return proxyServer ;
183
193
} ;
184
194
185
195
//
186
196
// Creates proxy server forwarding to the specified options
187
197
//
188
198
TestRunner . prototype . startProxyServerWithForwarding = function ( port , targetPort , host , options , callback ) {
189
- var that = this , proxyServer = httpProxy . createServer ( targetPort , host , merge ( { } , options , this . options ) ) ;
199
+ var that = this , proxyServer = httpProxy . createServer ( targetPort , host , merge ( { } , options , this . options ) ) ;
190
200
proxyServer . listen ( port , function ( ) {
191
201
that . testServers . push ( proxyServer ) ;
192
202
callback ( null , proxyServer ) ;
@@ -200,13 +210,13 @@ TestRunner.prototype.startTargetServer = function (port, output, callback) {
200
210
var that = this , targetServer , handler = function ( req , res ) {
201
211
res . writeHead ( 200 , { 'Content-Type' : 'text/plain' } ) ;
202
212
res . write ( output ) ;
203
- res . end ( ) ;
213
+ res . end ( ) ;
204
214
} ;
205
-
206
- targetServer = this . options . https
215
+
216
+ targetServer = this . options . https
207
217
? https . createServer ( this . options . https , handler )
208
218
: http . createServer ( handler ) ;
209
-
219
+
210
220
targetServer . listen ( port , function ( ) {
211
221
that . testServers . push ( targetServer ) ;
212
222
callback ( null , targetServer ) ;
@@ -222,4 +232,4 @@ TestRunner.prototype.closeServers = function () {
222
232
} ) ;
223
233
224
234
return this . testServers ;
225
- } ;
235
+ } ;
0 commit comments