9
9
var assert = require ( 'assert' ) ,
10
10
fs = require ( 'fs' ) ,
11
11
async = require ( 'async' ) ,
12
+ net = require ( 'net' ) ,
12
13
request = require ( 'request' ) ,
13
14
helpers = require ( '../helpers/index' ) ;
14
15
@@ -141,6 +142,83 @@ exports.assertProxied = function (options) {
141
142
} ;
142
143
} ;
143
144
145
+ //
146
+ // ### function assertRawHttpProxied (options)
147
+ // #### @options {Object} Options for this test
148
+ // #### @rawRequest {string} Raw HTTP request to perform.
149
+ // #### @match {RegExp} Output to match in the response.
150
+ // #### @latency {number} Latency in milliseconds for the proxy server.
151
+ // #### @ports {Object} Ports for the request (target, proxy).
152
+ // #### @output {string} Output to assert from.
153
+ // #### @forward {Object} Options for forward proxying.
154
+ //
155
+ // Creates a complete end-to-end test for requesting against an
156
+ // http proxy.
157
+ //
158
+ exports . assertRawHttpProxied = function ( options ) {
159
+ options = options || { } ;
160
+
161
+ var ports = options . ports || helpers . nextPortPair ,
162
+ output = options . output || 'hello world from ' + ports . target ,
163
+ outputHeaders = options . outputHeaders ,
164
+ targetHeaders = options . targetHeaders ,
165
+ proxyHeaders = options . proxyHeaders ,
166
+ protocol = helpers . protocols . proxy ,
167
+ timeout = options . timeout || null ,
168
+ assertFn = options . shouldFail
169
+ ? exports . assertFailedRequest
170
+ : exports . assertRequest ;
171
+
172
+ return {
173
+ topic : function ( ) {
174
+ var topicCallback = this . callback ;
175
+
176
+ //
177
+ // Create a target server and a proxy server
178
+ // using the `options` supplied.
179
+ //
180
+ helpers . http . createServerPair ( {
181
+ target : {
182
+ output : output ,
183
+ outputHeaders : targetHeaders ,
184
+ port : ports . target ,
185
+ latency : options . requestLatency
186
+ } ,
187
+ proxy : {
188
+ latency : options . latency ,
189
+ port : ports . proxy ,
190
+ outputHeaders : proxyHeaders ,
191
+ proxy : {
192
+ forward : options . forward ,
193
+ target : {
194
+ https : helpers . protocols . target === 'https' ,
195
+ host : '127.0.0.1' ,
196
+ port : ports . target
197
+ } ,
198
+ timeout : timeout
199
+ }
200
+ }
201
+ } , function ( ) {
202
+ var response = '' ;
203
+ var client = net . connect ( ports . proxy , '127.0.0.1' , function ( ) {
204
+ client . write ( options . rawRequest ) ;
205
+ } ) ;
206
+
207
+ client . on ( 'data' , function ( data ) {
208
+ response += data . toString ( ) ;
209
+ } ) ;
210
+
211
+ client . on ( 'end' , function ( ) {
212
+ topicCallback ( null , options . match , response ) ;
213
+ } ) ;
214
+ } ) ;
215
+ } ,
216
+ "should succeed" : function ( err , match , response ) {
217
+ assert . match ( response , match ) ;
218
+ }
219
+ } ;
220
+ } ;
221
+
144
222
//
145
223
// ### function assertInvalidProxy (options)
146
224
// #### @options {Object} Options for this test
@@ -444,4 +522,4 @@ exports.assertDynamicProxy = function (static, dynamic) {
444
522
return exports . assertProxiedToRoutes ( static , {
445
523
"once the server has started" : context
446
524
} ) ;
447
- } ;
525
+ } ;
0 commit comments