Skip to content

change proxyTable routing target path #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 15, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions lib/node-http-proxy/proxy-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ ProxyTable.prototype.setRoutes = function (router) {

self.routes.push({
route: route,
target: router[path]
target: router[path],
path: path
});
});
}
Expand Down Expand Up @@ -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];
Expand Down
10 changes: 6 additions & 4 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -73,14 +75,14 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx
}
};

if (requestPath) options.uri += requestPath;

function startTest () {
if (port) {
return self.startTargetServer(port, output, function () {
request(options, that.callback);
});
}

request(options, this.callback);
}

Expand Down
8 changes: 4 additions & 4 deletions test/http/http-proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ 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) {
runner.startProxyServer(8082, 9000, 'localhost', callback);
})
},
"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) {
Expand All @@ -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);
})
}
Expand Down
6 changes: 5 additions & 1 deletion test/http/routing-proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
};

Expand All @@ -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": {
Expand Down