diff --git a/lib/node-http-proxy/proxy-table.js b/lib/node-http-proxy/proxy-table.js index 749ea7d45..4ab6690ee 100644 --- a/lib/node-http-proxy/proxy-table.js +++ b/lib/node-http-proxy/proxy-table.js @@ -101,7 +101,8 @@ ProxyTable.prototype.setRoutes = function (router) { self.routes.push({ route: route, - target: router[path] + target: router[path], + path: path }); }); } @@ -134,10 +135,19 @@ ProxyTable.prototype.getProxyLocation = function (req) { else { target += req.url; for (var i in this.routes) { - var route = this.routes[i], - match; - - if (match = target.match(route.route)) { + var route = this.routes[i]; + if (target.match(route.route)) { + + var segments = route.path.split('/'); + + if (segments.length > 0) { + var lastSegment = new RegExp("/" + segments[segments.length - 1] + "$"); + + if(req.url.match(lastSegment)) { + req.url = req.url.replace(lastSegment, '/'); + } + } + var location = route.target.split(':'), host = location[0], port = location.length === 1 ? 80 : location[1]; diff --git a/test/helpers.js b/test/helpers.js index 56ba4aaa3..b00cbc37c 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -55,10 +55,12 @@ var TestRunner = exports.TestRunner = function (options) { } }; -TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProxy) { +TestRunner.prototype.assertProxied = function (host, proxyPort, port, requestPath, targetPath, createProxy) { + if (!targetPath) targetPath = ""; + var self = this, - assertion = "should receive 'hello " + host + "'", - output = 'hello ' + host; + output = "hello " + host + targetPath, + assertion = "should receive '" + output + "'"; var test = { topic: function () { @@ -73,6 +75,7 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx } }; + if (requestPath) options.uri += requestPath; function startTest () { if (port) { @@ -80,7 +83,6 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx request(options, that.callback); }); } - request(options, this.callback); } diff --git a/test/http/http-proxy-test.js b/test/http/http-proxy-test.js index e3b08f9d9..d68d68191 100644 --- a/test/http/http-proxy-test.js +++ b/test/http/http-proxy-test.js @@ -51,7 +51,7 @@ var options = helpers.parseProtocol(), vows.describe('node-http-proxy/http-proxy/' + testName).addBatch({ "When using server created by httpProxy.createServer()": { "with no latency" : { - "and a valid target server": runner.assertProxied('localhost', 8080, 8081, function (callback) { + "and a valid target server": runner.assertProxied('localhost', 8080, 8081, false, false, function (callback) { runner.startProxyServer(8080, 8081, 'localhost', callback); }), "and without a valid target server": runner.assertResponseCode(8082, 500, function (callback) { @@ -59,7 +59,7 @@ vows.describe('node-http-proxy/http-proxy/' + testName).addBatch({ }) }, "with latency": { - "and a valid target server": runner.assertProxied('localhost', 8083, 8084, function (callback) { + "and a valid target server": runner.assertProxied('localhost', 8083, 8084, false, false, function (callback) { runner.startLatentProxyServer(8083, 8084, 'localhost', 1000, callback); }), "and without a valid target server": runner.assertResponseCode(8085, 500, function (callback) { @@ -71,13 +71,13 @@ vows.describe('node-http-proxy/http-proxy/' + testName).addBatch({ runner.startTargetServer(8300, 'forward proxy', this.callback); }, "with no latency" : { - "and a valid target server": runner.assertProxied('localhost', 8120, 8121, function (callback) { + "and a valid target server": runner.assertProxied('localhost', 8120, 8121, false, false, function (callback) { runner.startProxyServerWithForwarding(8120, 8121, 'localhost', forwardOptions, callback); }), "and also a valid target server": runner.assertHeaders(8122, "x-forwarded-for", function (callback) { runner.startProxyServerWithForwarding(8122, 8123, 'localhost', forwardOptions, callback); }), - "and without a valid forward server": runner.assertProxied('localhost', 8124, 8125, function (callback) { + "and without a valid forward server": runner.assertProxied('localhost', 8124, 8125, false, false, function (callback) { runner.startProxyServerWithForwarding(8124, 8125, 'localhost', badForwardOptions, callback); }) } diff --git a/test/http/routing-proxy-test.js b/test/http/routing-proxy-test.js index 1180fb7f8..b97094bd4 100644 --- a/test/http/routing-proxy-test.js +++ b/test/http/routing-proxy-test.js @@ -29,7 +29,9 @@ var fileOptions = { var defaultOptions = { router: { "foo.com": "127.0.0.1:8091", - "bar.com": "127.0.0.1:8092" + "bar.com": "127.0.0.1:8092", + "baz.com/taco": "127.0.0.1:8098", + "pizza.com/taco/muffins": "127.0.0.1:8099", } }; @@ -50,6 +52,8 @@ vows.describe('node-http-proxy/routing-proxy/' + testName).addBatch({ }, "an incoming request to foo.com": runner.assertProxied('foo.com', 8090, 8091), "an incoming request to bar.com": runner.assertProxied('bar.com', 8090, 8092), + "an incoming request to baz.com/taco": runner.assertProxied('baz.com', 8090, 8098, "/taco", "/"), + "an incoming request to pizza.com/taco/muffins": runner.assertProxied('pizza.com', 8090, 8099, "/taco/muffins", "/taco"), "an incoming request to unknown.com": runner.assertResponseCode(8090, 404) }, "and routing by Hostname": {