diff --git a/lib/node-http-proxy.js b/lib/node-http-proxy.js index c0c85a536..c89a98a04 100644 --- a/lib/node-http-proxy.js +++ b/lib/node-http-proxy.js @@ -192,6 +192,28 @@ exports.createServer = function () { }); } + server.changeHost = function (host, target){ + this.addHost(host, target); + } + + server.addHost = function (host, target){ + if(!proxy instanceof RoutingProxy){ + throw new Error('Cannot update ProxyTable routes on HttpProxy.'); + } + else{ + proxy.addHost(host, target); + } + } + + server.removeHost = function (host){ + if(!proxy instanceof RoutingProxy){ + throw new Error('Cannot update ProxyTable routes on HttpProxy.'); + } + else{ + proxy.removeHost(host); + } + } + // // Set the proxy on the server so it is available // to the consumer of the server @@ -392,4 +414,4 @@ exports._getBase = function _getBase (options) { } return result; -}; \ No newline at end of file +}; 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..9e59aa7c4 100644 --- a/test/http/routing-proxy-test.js +++ b/test/http/routing-proxy-test.js @@ -43,6 +43,18 @@ var hostnameOptions = { } }; +var staticOptions = { + hostnameOnly: true, + router: { + "static.com": "127.0.0.1:8121", + "removed.com": "127.0.0.1:8122", + "change.com": "127.0.0.1:8123" + } +}; + +var dynamicHost = "dynamic1.com"; +var dynamicTarget = "127.0.0.1:8125"; + vows.describe('node-http-proxy/routing-proxy/' + testName).addBatch({ "When using server created by httpProxy.createServer()": { "when passed a routing table": { @@ -63,6 +75,18 @@ 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.removeHost('removed.com'); + this.server.addHost(dynamicHost, dynamicTarget); + this.server.changeHost('change.com', '127.0.0.1:8124'); + }, + "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 change.com": runner.assertProxied('change.com', 8120, 8124), + "an incoming request to dynamic1.com": runner.assertProxied('dynamic1.com', 8120, 8125) } }, "when passed a routing file": {