Skip to content

Commit

Permalink
First attempt at adding the ability to dynamically addHost(host,targe…
Browse files Browse the repository at this point in the history
…t) and removeHost(host) to the ProxyTable
  • Loading branch information
Travis Glines committed Feb 12, 2012
1 parent 91bb2a9 commit 5e38f74
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/node-http-proxy/proxy-table.js
Expand Up @@ -80,6 +80,37 @@ 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 @@ -104,6 +104,29 @@ RoutingProxy.prototype.remove = function (options) {
var key = this._getKey(options);
};

//
// ### 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);
}
};

//
// ### function close()
// Cleans up any state left behind (sockets, timeouts, etc)
Expand Down
21 changes: 21 additions & 0 deletions test/http/routing-proxy-test.js
Expand Up @@ -43,6 +43,17 @@ var hostnameOptions = {
}
};

var staticOptions = {
hostnameOnly: true,
router: {
"static.com": "127.0.0.1:8121",
"removed.com": "127.0.0.1:8122"
}
};

var dynamicHost = "dynamic1.com";
var dynamicTarget = "127.0.0.1:8123";

vows.describe('node-http-proxy/routing-proxy/' + testName).addBatch({
"When using server created by httpProxy.createServer()": {
"when passed a routing table": {
Expand All @@ -63,6 +74,16 @@ vows.describe('node-http-proxy/routing-proxy/' + testName).addBatch({
"an incoming request to foo.com": runner.assertProxied('foo.com', 8093, 8094),
"an incoming request to bar.com": runner.assertProxied('bar.com', 8093, 8095),
"an incoming request to unknown.com": runner.assertResponseCode(8093, 404)
},
"and adding a route by Hostname": {
topic: function () {
this.server = runner.startProxyServerWithTable(8120, staticOptions, this.callback);
this.server.proxy.removeHost('removed.com');
this.server.proxy.addHost(dynamicHost, dynamicTarget);
},
"an incoming request to static.com": runner.assertProxied('static.com', 8120, 8121),
"an incoming request to removed.com": !runner.assertProxied('removed.com', 8120, 8122),
"an incoming request to dynamic1.com": runner.assertProxied('dynamic1.com', 8120, 8123)
}
},
"when passed a routing file": {
Expand Down

0 comments on commit 5e38f74

Please sign in to comment.