Skip to content

Commit

Permalink
adding tests for url segment proxytable routing
Browse files Browse the repository at this point in the history
  • Loading branch information
max-mapper committed Oct 15, 2011
1 parent 4d50915 commit 91e9bb9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
17 changes: 9 additions & 8 deletions lib/node-http-proxy/proxy-table.js
Expand Up @@ -135,16 +135,17 @@ 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 root = "/" + route.path.split('/')[1];
var beginningSegment = new RegExp("^" + root);
var segments = route.path.split('/');

if(req.url.match(beginningSegment)) {
req.url = req.url.replace(beginningSegment, '');
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(':'),
Expand Down
10 changes: 6 additions & 4 deletions test/helpers.js
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
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
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

0 comments on commit 91e9bb9

Please sign in to comment.