Skip to content

Commit 5d515e4

Browse files
committed
[api test] Manually merge #195 from @tglines since that fork was deleted. Update tests to use new macros. Fixes #195. Fixes #60.
1 parent 5e6be6c commit 5d515e4

File tree

4 files changed

+144
-2
lines changed

4 files changed

+144
-2
lines changed

lib/node-http-proxy/proxy-table.js

+29
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,35 @@ var ProxyTable = exports.ProxyTable = function (options) {
8484
//
8585
util.inherits(ProxyTable, events.EventEmitter);
8686

87+
//
88+
// ### function addRoute (route, target)
89+
// #### @route {String} String containing route coming in
90+
// #### @target {String} String containing the target
91+
// Adds a host-based route to this instance.
92+
//
93+
ProxyTable.prototype.addRoute = function (route, target) {
94+
if (!this.router) {
95+
throw new Error('Cannot update ProxyTable routes without router.');
96+
}
97+
98+
this.router[route] = target;
99+
this.setRoutes(this.router);
100+
};
101+
102+
//
103+
// ### function removeRoute (route)
104+
// #### @route {String} String containing route to remove
105+
// Removes a host-based route from this instance.
106+
//
107+
ProxyTable.prototype.removeRoute = function (route) {
108+
if (!this.router) {
109+
throw new Error('Cannot update ProxyTable routes without router.');
110+
}
111+
112+
delete this.router[route];
113+
this.setRoutes(this.router);
114+
};
115+
87116
//
88117
// ### function setRoutes (router)
89118
// #### @router {Object} Object containing the host based routes

lib/node-http-proxy/routing-proxy.js

+23
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,29 @@ RoutingProxy.prototype.proxyWebSocketRequest = function (req, socket, head, opti
276276
proxy.proxyWebSocketRequest(req, socket, head, options.buffer);
277277
};
278278

279+
//
280+
// ### function addHost (host, target)
281+
// #### @host {String} Host to add to proxyTable
282+
// #### @target {String} Target to add to proxyTable
283+
// Adds a host to proxyTable
284+
//
285+
RoutingProxy.prototype.addHost = function (host, target) {
286+
if (this.proxyTable) {
287+
this.proxyTable.addRoute(host, target);
288+
}
289+
};
290+
291+
//
292+
// ### function removeHost (host)
293+
// #### @host {String} Host to remove from proxyTable
294+
// Removes a host to proxyTable
295+
//
296+
RoutingProxy.prototype.removeHost = function (host) {
297+
if (this.proxyTable) {
298+
this.proxyTable.removeRoute(host);
299+
}
300+
};
301+
279302
//
280303
// ### @private function _getKey (options)
281304
// #### @options {Object} Options to extract the key from

test/http/routing-table-test.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var assert = require('assert'),
1212
request = require('request'),
1313
vows = require('vows'),
1414
macros = require('../macros'),
15-
helpers = require('../helpers/index');
15+
helpers = require('../helpers');
1616

1717
var routeFile = path.join(__dirname, 'config.json');
1818

@@ -25,6 +25,16 @@ vows.describe(helpers.describe('routing-table')).addBatch({
2525
"latency.com": "127.0.0.1:{PORT}"
2626
}
2727
}),
28+
"addHost() / removeHost()": macros.http.assertDynamicProxy({
29+
hostnameOnly: true,
30+
routes: {
31+
"static.com": "127.0.0.1:{PORT}",
32+
"removed.com": "127.0.0.1:{PORT}"
33+
}
34+
}, {
35+
add: [{ host: 'dynamic1.com', target: '127.0.0.1:' }],
36+
drop: ['removed.com']
37+
}),
2838
"using RegExp": macros.http.assertProxiedToRoutes({
2939
routes: {
3040
"foo.com": "127.0.0.1:{PORT}",

test/macros/http.js

+81-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ exports.assertProxiedToRoutes = function (options, nested) {
243243
// Parse locations from routes for making assertion requests.
244244
//
245245
var locations = helpers.http.parseRoutes(options),
246-
port = helpers.nextPort,
246+
port = options.pport || helpers.nextPort,
247247
protocol = helpers.protocols.proxy,
248248
context,
249249
proxy;
@@ -365,3 +365,83 @@ exports.assertProxiedToRoutes = function (options, nested) {
365365

366366
return context;
367367
};
368+
369+
//
370+
// ### function assertDynamicProxy (static, dynamic)
371+
// Asserts that after the `static` routes have been tested
372+
// and the `dynamic` routes are added / removed the appropriate
373+
// proxy responses are received.
374+
//
375+
exports.assertDynamicProxy = function (static, dynamic) {
376+
var proxyPort = helpers.nextPort,
377+
protocol = helpers.protocols.proxy,
378+
context;
379+
380+
if (dynamic.add) {
381+
dynamic.add = dynamic.add.map(function (dyn) {
382+
dyn.port = helpers.nextPort;
383+
dyn.target = dyn.target + dyn.port;
384+
return dyn;
385+
});
386+
}
387+
388+
context = {
389+
topic: function () {
390+
var that = this;
391+
392+
setTimeout(function () {
393+
if (dynamic.drop) {
394+
dynamic.drop.forEach(function (dropHost) {
395+
that.proxyServer.proxy.removeHost(dropHost);
396+
});
397+
}
398+
399+
if (dynamic.add) {
400+
async.forEachSeries(dynamic.add, function addOne (dyn, next) {
401+
that.proxyServer.proxy.addHost(dyn.host, dyn.target);
402+
helpers.http.createServer({
403+
port: dyn.port,
404+
output: 'hello ' + dyn.host
405+
}, next);
406+
}, that.callback);
407+
}
408+
else {
409+
that.callback();
410+
}
411+
}, 200);
412+
}
413+
};
414+
415+
if (dynamic.drop) {
416+
dynamic.drop.forEach(function (dropHost) {
417+
context[dropHost] = exports.assertRequest({
418+
assert: { statusCode: 404 },
419+
request: {
420+
uri: protocol + '://127.0.0.1:' + proxyPort,
421+
headers: {
422+
host: dropHost
423+
}
424+
}
425+
});
426+
});
427+
}
428+
429+
if (dynamic.add) {
430+
dynamic.add.forEach(function (dyn) {
431+
context[dyn.host] = exports.assertRequest({
432+
assert: { body: 'hello ' + dyn.host },
433+
request: {
434+
uri: protocol + '://127.0.0.1:' + proxyPort,
435+
headers: {
436+
host: dyn.host
437+
}
438+
}
439+
});
440+
});
441+
}
442+
443+
static.pport = proxyPort;
444+
return exports.assertProxiedToRoutes(static, {
445+
"once the server has started": context
446+
});
447+
};

0 commit comments

Comments
 (0)