diff --git a/lib/node-http-proxy/proxy-table.js b/lib/node-http-proxy/proxy-table.js index 9035ab8da..0f61cb590 100644 --- a/lib/node-http-proxy/proxy-table.js +++ b/lib/node-http-proxy/proxy-table.js @@ -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 diff --git a/lib/node-http-proxy/routing-proxy.js b/lib/node-http-proxy/routing-proxy.js index 4e4fc32a5..2ae862b70 100644 --- a/lib/node-http-proxy/routing-proxy.js +++ b/lib/node-http-proxy/routing-proxy.js @@ -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) diff --git a/test/http/routing-proxy-test.js b/test/http/routing-proxy-test.js index b97094bd4..522d90cf8 100644 --- a/test/http/routing-proxy-test.js +++ b/test/http/routing-proxy-test.js @@ -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": { @@ -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": {