forked from http-party/node-http-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.js
More file actions
324 lines (300 loc) · 8.26 KB
/
http.js
File metadata and controls
324 lines (300 loc) · 8.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* http.js: Macros for proxying HTTP requests
*
* (C) 2010 Nodejitsu Inc.
* MIT LICENCE
*
*/
var assert = require('assert'),
fs = require('fs'),
async = require('async'),
request = require('request'),
helpers = require('../helpers/index');
//
// ### function assertRequest (options)
// #### @options {Object} Options for this request assertion.
// #### @request {Object} Options to use for `request`.
// #### @assert {Object} Test assertions against the response.
//
// Makes a request using `options.request` and then asserts the response
// and body against anything in `options.assert`.
//
exports.assertRequest = function (options) {
return {
topic: function () {
//
// Now make the HTTP request and assert.
//
options.request.rejectUnauthorized = false;
request(options.request, this.callback);
},
"should succeed": function (err, res, body) {
assert.isNull(err);
if (options.assert.body) {
assert.equal(body, options.assert.body);
}
if (options.assert.statusCode) {
assert.equal(res.statusCode, options.assert.statusCode);
}
}
};
};
//
// ### function assertProxied (options)
// #### @options {Object} Options for this test
// #### @latency {number} Latency in milliseconds for the proxy server.
// #### @ports {Object} Ports for the request (target, proxy).
// #### @output {string} Output to assert from.
// #### @forward {Object} Options for forward proxying.
//
// Creates a complete end-to-end test for requesting against an
// http proxy.
//
exports.assertProxied = function (options) {
options = options || {};
var ports = options.ports || helpers.nextPortPair,
output = options.output || 'hello world from ' + ports.target,
protocol = helpers.protocols.proxy,
req = options.request || {};
req.uri = req.uri || protocol + '://127.0.0.1:' + ports.proxy;
return {
topic: function () {
//
// Create a target server and a proxy server
// using the `options` supplied.
//
helpers.http.createServerPair({
target: {
output: output,
port: ports.target,
headers: req.headers
},
proxy: {
latency: options.latency,
port: ports.proxy,
proxy: {
forward: options.forward,
target: {
https: helpers.protocols.target === 'https',
host: '127.0.0.1',
port: ports.target
}
}
}
}, this.callback);
},
"the proxy request": exports.assertRequest({
request: req,
assert: {
body: output
}
})
};
};
//
// ### function assertInvalidProxy (options)
// #### @options {Object} Options for this test
// #### @latency {number} Latency in milliseconds for the proxy server
// #### @ports {Object} Ports for the request (target, proxy)
//
// Creates a complete end-to-end test for requesting against an
// http proxy with no target server.
//
exports.assertInvalidProxy = function (options) {
options = options || {};
var ports = options.ports || helpers.nextPortPair,
req = options.request || {},
protocol = helpers.protocols.proxy;
req.uri = req.uri || protocol + '://127.0.0.1:' + ports.proxy;
return {
topic: function () {
//
// Only create the proxy server, simulating a reverse-proxy
// to an invalid location.
//
helpers.http.createProxyServer({
latency: options.latency,
port: ports.proxy,
proxy: {
target: {
host: '127.0.0.1',
port: ports.target
}
}
}, this.callback);
},
"the proxy request": exports.assertRequest({
request: req,
assert: {
statusCode: 500
}
})
};
};
//
// ### function assertForwardProxied (options)
// #### @options {Object} Options for this test.
//
// Creates a complete end-to-end test for requesting against an
// http proxy with both a valid and invalid forward target.
//
exports.assertForwardProxied = function (options) {
var forwardPort = helpers.nextPort;
return {
topic: function () {
helpers.http.createServer({
output: 'hello from forward',
port: forwardPort
}, this.callback);
},
"and a valid forward target": exports.assertProxied({
forward: {
port: forwardPort,
host: '127.0.0.1'
}
}),
"and an invalid forward target": exports.assertProxied({
forward: {
port: 9898,
host: '127.0.0.1'
}
})
};
};
//
// ### function assertProxiedtoRoutes (options, nested)
// #### @options {Object} Options for this ProxyTable-based test
// #### @routes {Object|string} Routes to use for the proxy.
// #### @hostnameOnly {boolean} Enables hostnameOnly routing.
// #### @nested {Object} Nested vows to add to the returned context.
//
// Creates a complete end-to-end test for requesting against an
// http proxy using `options.routes`:
//
// 1. Creates target servers for all routes in `options.routes.`
// 2. Creates a proxy server.
// 3. Ensure requests to the proxy server for all route targets
// returns the unique expected output.
//
exports.assertProxiedToRoutes = function (options, nested) {
//
// Assign dynamic ports to the routes to use.
//
options.routes = helpers.http.assignPortsToRoutes(options.routes);
//
// Parse locations from routes for making assertion requests.
//
var locations = helpers.http.parseRoutes(options),
port = helpers.nextPort,
protocol = helpers.protocols.proxy,
context,
proxy;
if (options.filename) {
//
// If we've been passed a filename write the routes to it
// and setup the proxy options to use that file.
//
fs.writeFileSync(options.filename, JSON.stringify({ router: options.routes }));
proxy = { router: options.filename };
}
else {
//
// Otherwise just use the routes themselves.
//
proxy = {
hostnameOnly: options.hostnameOnly,
router: options.routes
};
}
//
// Set the https options if necessary
//
if (helpers.protocols.target === 'https') {
proxy.target = { https: true };
}
//
// Create the test context which creates all target
// servers for all routes and a proxy server.
//
context = {
topic: function () {
var that = this;
async.waterfall([
//
// 1. Create all the target servers
//
async.apply(
async.forEach,
locations,
function createRouteTarget(location, next) {
helpers.http.createServer({
port: location.target.port,
output: 'hello from ' + location.source.href
}, next);
}
),
//
// 2. Create the proxy server
//
async.apply(
helpers.http.createProxyServer,
{
port: port,
latency: options.latency,
routing: true,
proxy: proxy
}
)
], function (_, server) {
//
// 3. Set the proxy server for later use
//
that.proxyServer = server;
that.callback();
});
//
// 4. Assign the port to the context for later use
//
this.port = port;
},
//
// Add an extra assertion to a route which
// should respond with 404
//
"a request to unknown.com": exports.assertRequest({
assert: { statusCode: 404 },
request: {
uri: protocol + '://127.0.0.1:' + port,
headers: {
host: 'unknown.com'
}
}
})
};
//
// Add test assertions for each of the route locations.
//
locations.forEach(function (location) {
context[location.source.href] = exports.assertRequest({
request: {
uri: protocol + '://127.0.0.1:' + port + location.source.path,
headers: {
host: location.source.hostname
}
},
assert: {
body: 'hello from ' + location.source.href
}
});
});
//
// If there are any nested vows to add to the context
// add them before returning the full context.
//
if (nested) {
Object.keys(nested).forEach(function (key) {
context[key] = nested[key];
});
}
return context;
};