Skip to content

Commit

Permalink
[api test] Manually merge #195 from @tglines since that fork was dele…
Browse files Browse the repository at this point in the history
…ted. Update tests to use new macros. Fixes #195. Fixes #60.
  • Loading branch information
indexzero committed Mar 9, 2013
1 parent 5e6be6c commit 5d515e4
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 2 deletions.
29 changes: 29 additions & 0 deletions lib/node-http-proxy/proxy-table.js
Expand Up @@ -84,6 +84,35 @@ var ProxyTable = exports.ProxyTable = function (options) {
//
util.inherits(ProxyTable, events.EventEmitter);

//
// ### function addRoute (route, target)
// #### @route {String} String containing route coming in
// #### @target {String} String containing the target
// Adds a host-based route to this instance.
//
ProxyTable.prototype.addRoute = function (route, target) {
if (!this.router) {
throw new Error('Cannot update ProxyTable routes without router.');
}

this.router[route] = target;
this.setRoutes(this.router);
};

//
// ### function removeRoute (route)
// #### @route {String} String containing route to remove
// Removes a host-based route from this instance.
//
ProxyTable.prototype.removeRoute = function (route) {
if (!this.router) {
throw new Error('Cannot update ProxyTable routes without router.');
}

delete this.router[route];
this.setRoutes(this.router);
};

//
// ### function setRoutes (router)
// #### @router {Object} Object containing the host based routes
Expand Down
23 changes: 23 additions & 0 deletions lib/node-http-proxy/routing-proxy.js
Expand Up @@ -276,6 +276,29 @@ RoutingProxy.prototype.proxyWebSocketRequest = function (req, socket, head, opti
proxy.proxyWebSocketRequest(req, socket, head, options.buffer);
};

//
// ### function addHost (host, target)
// #### @host {String} Host to add to proxyTable
// #### @target {String} Target to add to proxyTable
// Adds a host to proxyTable
//
RoutingProxy.prototype.addHost = function (host, target) {
if (this.proxyTable) {
this.proxyTable.addRoute(host, target);
}
};

//
// ### function removeHost (host)
// #### @host {String} Host to remove from proxyTable
// Removes a host to proxyTable
//
RoutingProxy.prototype.removeHost = function (host) {
if (this.proxyTable) {
this.proxyTable.removeRoute(host);
}
};

//
// ### @private function _getKey (options)
// #### @options {Object} Options to extract the key from
Expand Down
12 changes: 11 additions & 1 deletion test/http/routing-table-test.js
Expand Up @@ -12,7 +12,7 @@ var assert = require('assert'),
request = require('request'),
vows = require('vows'),
macros = require('../macros'),
helpers = require('../helpers/index');
helpers = require('../helpers');

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

Expand All @@ -25,6 +25,16 @@ vows.describe(helpers.describe('routing-table')).addBatch({
"latency.com": "127.0.0.1:{PORT}"
}
}),
"addHost() / removeHost()": macros.http.assertDynamicProxy({
hostnameOnly: true,
routes: {
"static.com": "127.0.0.1:{PORT}",
"removed.com": "127.0.0.1:{PORT}"
}
}, {
add: [{ host: 'dynamic1.com', target: '127.0.0.1:' }],
drop: ['removed.com']
}),
"using RegExp": macros.http.assertProxiedToRoutes({
routes: {
"foo.com": "127.0.0.1:{PORT}",
Expand Down
82 changes: 81 additions & 1 deletion test/macros/http.js
Expand Up @@ -243,7 +243,7 @@ exports.assertProxiedToRoutes = function (options, nested) {
// Parse locations from routes for making assertion requests.
//
var locations = helpers.http.parseRoutes(options),
port = helpers.nextPort,
port = options.pport || helpers.nextPort,
protocol = helpers.protocols.proxy,
context,
proxy;
Expand Down Expand Up @@ -365,3 +365,83 @@ exports.assertProxiedToRoutes = function (options, nested) {

return context;
};

//
// ### function assertDynamicProxy (static, dynamic)
// Asserts that after the `static` routes have been tested
// and the `dynamic` routes are added / removed the appropriate
// proxy responses are received.
//
exports.assertDynamicProxy = function (static, dynamic) {
var proxyPort = helpers.nextPort,
protocol = helpers.protocols.proxy,
context;

if (dynamic.add) {
dynamic.add = dynamic.add.map(function (dyn) {
dyn.port = helpers.nextPort;
dyn.target = dyn.target + dyn.port;
return dyn;
});
}

context = {
topic: function () {
var that = this;

setTimeout(function () {
if (dynamic.drop) {
dynamic.drop.forEach(function (dropHost) {
that.proxyServer.proxy.removeHost(dropHost);
});
}

if (dynamic.add) {
async.forEachSeries(dynamic.add, function addOne (dyn, next) {
that.proxyServer.proxy.addHost(dyn.host, dyn.target);
helpers.http.createServer({
port: dyn.port,
output: 'hello ' + dyn.host
}, next);
}, that.callback);
}
else {
that.callback();
}
}, 200);
}
};

if (dynamic.drop) {
dynamic.drop.forEach(function (dropHost) {
context[dropHost] = exports.assertRequest({
assert: { statusCode: 404 },
request: {
uri: protocol + '://127.0.0.1:' + proxyPort,
headers: {
host: dropHost
}
}
});
});
}

if (dynamic.add) {
dynamic.add.forEach(function (dyn) {
context[dyn.host] = exports.assertRequest({
assert: { body: 'hello ' + dyn.host },
request: {
uri: protocol + '://127.0.0.1:' + proxyPort,
headers: {
host: dyn.host
}
}
});
});
}

static.pport = proxyPort;
return exports.assertProxiedToRoutes(static, {
"once the server has started": context
});
};

0 comments on commit 5d515e4

Please sign in to comment.