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,89 @@ 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
+ // Don't test raw requests over HTTPS since options.rawRequest won't be
160
+ // encrypted.
161
+ if ( helpers . protocols . proxy == 'https' ) {
162
+ return true ;
163
+ }
164
+
165
+ options = options || { } ;
166
+
167
+ var ports = options . ports || helpers . nextPortPair ,
168
+ output = options . output || 'hello world from ' + ports . target ,
169
+ outputHeaders = options . outputHeaders ,
170
+ targetHeaders = options . targetHeaders ,
171
+ proxyHeaders = options . proxyHeaders ,
172
+ protocol = helpers . protocols . proxy ,
173
+ timeout = options . timeout || null ,
174
+ assertFn = options . shouldFail
175
+ ? exports . assertFailedRequest
176
+ : exports . assertRequest ;
177
+
178
+ return {
179
+ topic : function ( ) {
180
+ var topicCallback = this . callback ;
181
+
182
+ //
183
+ // Create a target server and a proxy server
184
+ // using the `options` supplied.
185
+ //
186
+ helpers . http . createServerPair ( {
187
+ target : {
188
+ output : output ,
189
+ outputHeaders : targetHeaders ,
190
+ port : ports . target ,
191
+ latency : options . requestLatency
192
+ } ,
193
+ proxy : {
194
+ latency : options . latency ,
195
+ port : ports . proxy ,
196
+ outputHeaders : proxyHeaders ,
197
+ proxy : {
198
+ forward : options . forward ,
199
+ target : {
200
+ https : helpers . protocols . target === 'https' ,
201
+ host : '127.0.0.1' ,
202
+ port : ports . target
203
+ } ,
204
+ timeout : timeout
205
+ }
206
+ }
207
+ } , function ( ) {
208
+ var response = '' ;
209
+ var client = net . connect ( ports . proxy , '127.0.0.1' , function ( ) {
210
+ client . write ( options . rawRequest ) ;
211
+ } ) ;
212
+
213
+ client . on ( 'data' , function ( data ) {
214
+ response += data . toString ( ) ;
215
+ } ) ;
216
+
217
+ client . on ( 'end' , function ( ) {
218
+ topicCallback ( null , options . match , response ) ;
219
+ } ) ;
220
+ } ) ;
221
+ } ,
222
+ "should succeed" : function ( err , match , response ) {
223
+ assert . match ( response , match ) ;
224
+ }
225
+ } ;
226
+ } ;
227
+
144
228
//
145
229
// ### function assertInvalidProxy (options)
146
230
// #### @options {Object} Options for this test
@@ -444,4 +528,4 @@ exports.assertDynamicProxy = function (static, dynamic) {
444
528
return exports . assertProxiedToRoutes ( static , {
445
529
"once the server has started" : context
446
530
} ) ;
447
- } ;
531
+ } ;
0 commit comments