Skip to content

Commit be4562d

Browse files
committed
[api test] Updated httpProxy.createServer() for new API exposed by simplified HttpProxy object.
[breaking] Temporarily removed pending refactor: middleware & ProxyTable support
1 parent d2b0e43 commit be4562d

File tree

2 files changed

+112
-110
lines changed

2 files changed

+112
-110
lines changed

Diff for: lib/node-http-proxy.js

+49-68
Original file line numberDiff line numberDiff line change
@@ -56,94 +56,75 @@ var HttpProxy = exports.HttpProxy = require('./node-http-proxy/http-proxy'
5656
//
5757
exports.createServer = function () {
5858
var args = Array.prototype.slice.call(arguments),
59-
callback, forward,
60-
port, host,
61-
proxy, server,
6259
options = {},
63-
middleware = [],
60+
host, port,
61+
server, proxy,
62+
callback,
6463
handler,
6564
silent;
6665

66+
//
67+
// Liberally parse arguments of the form:
68+
//
69+
// httpProxy.createServer('localhost', 9000, callback);
70+
// httpProxy.createServer({ host: 'localhost', port: 9000 }, callback);
71+
// **NEED MORE HERE!!!**
72+
//
6773
args.forEach(function (arg) {
6874
switch (typeof arg) {
69-
case 'string': host = arg; break;
70-
case 'number': port = arg; break;
71-
case 'function': middleware.push(handler = callback = arg); break;
72-
case 'object': options = arg; break;
75+
case 'string': host = arg; break;
76+
case 'number': port = arg; break;
77+
case 'object': options = arg || {}; break;
78+
case 'function': callback = arg; break;
7379
};
7480
});
7581

76-
proxy = new HttpProxy(options);
77-
78-
if (port && host) {
79-
//
80-
// If we have a target host and port for the request
81-
// then proxy to the specified location.
82+
if (!host && !port && !options) {
8283
//
83-
handler = function (req, res) {
84-
proxy.proxyRequest(req, res, {
85-
port: port,
86-
host: host
87-
});
88-
}
89-
90-
if (middleware.length) {
91-
middleware.push(handler);
92-
}
93-
}
94-
else if (proxy.proxyTable) {
95-
//
96-
// If the proxy is configured with a ProxyTable
97-
// instance then use that before failing.
98-
//
99-
handler = function (req, res) {
100-
proxy.proxyRequest(req, res);
101-
}
102-
103-
if (middleware.length) {
104-
middleware.push(handler);
105-
}
106-
}
107-
108-
if (middleware.length > 1) {
109-
handler = callback = exports.stack(middleware, proxy);
110-
}
111-
else if (middleware.length) {
112-
//
113-
// Do not use middleware code if it's not needed.
114-
//
115-
var h = middleware[0];
116-
handler = callback = function (req,res) { h(req,res,proxy) };
117-
}
118-
119-
if (!handler) {
120-
//
121-
// Otherwise this server is improperly configured.
84+
// If `host`, `port` and `options` are all not passed, then
85+
// this server is improperly configured.
12286
//
12387
throw new Error('Cannot proxy without port, host, or router.')
12488
}
89+
90+
//
91+
// Hoist up any explicit `host` or `port` arguments
92+
// that have been passed in to the options we will
93+
// pass to the `httpProxy.HttpProxy` constructor.
94+
//
95+
options.target = options.target || {};
96+
options.target.port = options.target.port || port;
97+
options.target.host = options.target.host || host;
98+
99+
//
100+
// Create the `http[s].Server` instance which will use
101+
// an instance of `httpProxy.HttpProxy`.
102+
//
103+
proxy = new HttpProxy(options);
104+
handler = callback
105+
? function (req, res) { callback(req, res, proxy) }
106+
: proxy.proxyRequest;
107+
108+
server = options.https
109+
? https.createServer(options.https, handler.bind(proxy))
110+
: http.createServer(handler.bind(proxy));
125111

126-
server = options.https
127-
? https.createServer(options.https, handler)
128-
: http.createServer(handler);
129-
130-
server.on('close', function () {
131-
proxy.close();
132-
});
112+
//server.on('close', function () {
113+
// proxy.close();
114+
//});
133115

134116
proxy.on('routes', function (routes) {
135117
server.emit('routes', routes);
136118
});
137119

138120
if (!callback) {
139-
// WebSocket support: if callback is empty tunnel
140-
// websocket request automatically
121+
//
122+
// If an explicit callback has not been supplied then
123+
// automagically proxy the request using the `HttpProxy`
124+
// instance we have created.
125+
//
141126
server.on('upgrade', function (req, socket, head) {
142-
// Tunnel websocket requests too
143-
proxy.proxyWebSocketRequest(req, socket, head, {
144-
port: port,
145-
host: host
146-
});
127+
proxy.proxyWebSocketRequest(req, socket, head);
147128
});
148129
}
149130

@@ -306,7 +287,7 @@ exports._getProtocol = function _getProtocol (options) {
306287
exports._getBase = function _getBase (options) {
307288
var result = {};
308289

309-
if (typeof options.https === 'object') {
290+
if (options.https && typeof options.https === 'object') {
310291
['ca', 'cert', 'key'].forEach(function (key) {
311292
if (options.https[key]) {
312293
result[key] = options.https[key];

Diff for: test/helpers.js

+63-42
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,24 @@ var fs = require('fs'),
1515
websocket = require('./../vendor/websocket'),
1616
httpProxy = require('./../lib/node-http-proxy');
1717

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-
3018
var loadHttps = exports.loadHttps = function () {
3119
return {
3220
key: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-key.pem'), 'utf8'),
3321
cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-cert.pem'), 'utf8')
3422
};
3523
};
3624

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 = [];
4329

44-
if (protocol === 'https') {
45-
this.options.https = loadHttps();
30+
if (source === 'https') {
31+
this.source.https = loadHttps();
4632
}
4733

4834
if (target === 'https') {
49-
this.options.target = {
50-
https: loadHttps()
51-
};
35+
this.target.https = loadHttps();
5236
}
5337
};
5438

@@ -64,11 +48,12 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx
6448

6549
options = {
6650
method: 'GET',
67-
uri: self.protocol + '://localhost:' + proxyPort,
51+
uri: self.source.protocol + '://localhost:' + proxyPort,
6852
headers: {
6953
host: host
7054
}
7155
};
56+
7257

7358
function startTest () {
7459
if (port) {
@@ -94,7 +79,7 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx
9479

9580
TestRunner.prototype.assertResponseCode = function (proxyPort, statusCode, createProxy) {
9681
var assertion = "should receive " + statusCode + " responseCode",
97-
protocol = this.protocol;
82+
protocol = this.source.protocol;
9883

9984
var test = {
10085
topic: function () {
@@ -202,8 +187,7 @@ TestRunner.prototype.webSocketTestWithTable = function (options) {
202187
//
203188
TestRunner.prototype.startProxyServer = function (port, targetPort, host, callback) {
204189
var that = this,
205-
options = that.options,
206-
proxyServer = httpProxy.createServer(targetPort, host, options);
190+
proxyServer = httpProxy.createServer(host, targetPort, this.getOptions());
207191

208192
proxyServer.listen(port, function () {
209193
that.testServers.push(proxyServer);
@@ -215,21 +199,19 @@ TestRunner.prototype.startProxyServer = function (port, targetPort, host, callba
215199
// Creates the reverse proxy server with a specified latency
216200
//
217201
TestRunner.prototype.startLatentProxyServer = function (port, targetPort, host, latency, callback) {
202+
//
218203
// Initialize the nodeProxy and start proxying the request
204+
//
219205
var that = this,
220206
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);
224210

225211
setTimeout(function () {
226-
proxy.proxyRequest(req, res, {
227-
port: targetPort,
228-
host: host,
229-
buffer: buffer
230-
});
212+
proxy.proxyRequest(req, res, buffer);
231213
}, latency);
232-
}, this.options);
214+
}, this.getOptions());
233215

234216
proxyServer.listen(port, function () {
235217
that.testServers.push(proxyServer);
@@ -242,7 +224,7 @@ TestRunner.prototype.startLatentProxyServer = function (port, targetPort, host,
242224
//
243225
TestRunner.prototype.startProxyServerWithTable = function (port, options, callback) {
244226
var that = this,
245-
proxyServer = httpProxy.createServer(merge({}, options, this.options));
227+
proxyServer = httpProxy.createServer(merge({}, options, this.getOptions()));
246228

247229
proxyServer.listen(port, function () {
248230
that.testServers.push(proxyServer);
@@ -260,7 +242,7 @@ TestRunner.prototype.startProxyServerWithTableAndLatency = function (port, laten
260242
// Initialize the nodeProxy and start proxying the request
261243
//
262244
var that = this,
263-
proxy = new httpProxy.HttpProxy(merge({}, options, that.options)),
245+
proxy = new httpProxy.HttpProxy(merge({}, options, this.getOptions())),
264246
proxyServer;
265247

266248
var handler = function (req, res) {
@@ -289,7 +271,7 @@ TestRunner.prototype.startProxyServerWithTableAndLatency = function (port, laten
289271
//
290272
TestRunner.prototype.startProxyServerWithForwarding = function (port, targetPort, host, options, callback) {
291273
var that = this,
292-
proxyServer = httpProxy.createServer(targetPort, host, merge({}, options, this.options));
274+
proxyServer = httpProxy.createServer(targetPort, host, merge({}, options, this.getOptions()));
293275

294276
proxyServer.listen(port, function () {
295277
that.testServers.push(proxyServer);
@@ -310,9 +292,9 @@ TestRunner.prototype.startTargetServer = function (port, output, callback) {
310292
res.write(output);
311293
res.end();
312294
};
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)
316298
: http.createServer(handler);
317299

318300
targetServer.listen(port, function () {
@@ -331,3 +313,42 @@ TestRunner.prototype.closeServers = function () {
331313

332314
return this.testServers;
333315
};
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

Comments
 (0)