Skip to content

Commit 91e9bb9

Browse files
committed
adding tests for url segment proxytable routing
1 parent 4d50915 commit 91e9bb9

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

lib/node-http-proxy/proxy-table.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,17 @@ ProxyTable.prototype.getProxyLocation = function (req) {
135135
else {
136136
target += req.url;
137137
for (var i in this.routes) {
138-
var route = this.routes[i],
139-
match;
140-
141-
if (match = target.match(route.route)) {
138+
var route = this.routes[i];
139+
if (target.match(route.route)) {
142140

143-
var root = "/" + route.path.split('/')[1];
144-
var beginningSegment = new RegExp("^" + root);
141+
var segments = route.path.split('/');
145142

146-
if(req.url.match(beginningSegment)) {
147-
req.url = req.url.replace(beginningSegment, '');
143+
if (segments.length > 0) {
144+
var lastSegment = new RegExp("/" + segments[segments.length - 1] + "$");
145+
146+
if(req.url.match(lastSegment)) {
147+
req.url = req.url.replace(lastSegment, '/');
148+
}
148149
}
149150

150151
var location = route.target.split(':'),

test/helpers.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ var TestRunner = exports.TestRunner = function (options) {
5555
}
5656
};
5757

58-
TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProxy) {
58+
TestRunner.prototype.assertProxied = function (host, proxyPort, port, requestPath, targetPath, createProxy) {
59+
if (!targetPath) targetPath = "";
60+
5961
var self = this,
60-
assertion = "should receive 'hello " + host + "'",
61-
output = 'hello ' + host;
62+
output = "hello " + host + targetPath,
63+
assertion = "should receive '" + output + "'";
6264

6365
var test = {
6466
topic: function () {
@@ -73,14 +75,14 @@ TestRunner.prototype.assertProxied = function (host, proxyPort, port, createProx
7375
}
7476
};
7577

78+
if (requestPath) options.uri += requestPath;
7679

7780
function startTest () {
7881
if (port) {
7982
return self.startTargetServer(port, output, function () {
8083
request(options, that.callback);
8184
});
8285
}
83-
8486
request(options, this.callback);
8587
}
8688

test/http/http-proxy-test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ var options = helpers.parseProtocol(),
5151
vows.describe('node-http-proxy/http-proxy/' + testName).addBatch({
5252
"When using server created by httpProxy.createServer()": {
5353
"with no latency" : {
54-
"and a valid target server": runner.assertProxied('localhost', 8080, 8081, function (callback) {
54+
"and a valid target server": runner.assertProxied('localhost', 8080, 8081, false, false, function (callback) {
5555
runner.startProxyServer(8080, 8081, 'localhost', callback);
5656
}),
5757
"and without a valid target server": runner.assertResponseCode(8082, 500, function (callback) {
5858
runner.startProxyServer(8082, 9000, 'localhost', callback);
5959
})
6060
},
6161
"with latency": {
62-
"and a valid target server": runner.assertProxied('localhost', 8083, 8084, function (callback) {
62+
"and a valid target server": runner.assertProxied('localhost', 8083, 8084, false, false, function (callback) {
6363
runner.startLatentProxyServer(8083, 8084, 'localhost', 1000, callback);
6464
}),
6565
"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({
7171
runner.startTargetServer(8300, 'forward proxy', this.callback);
7272
},
7373
"with no latency" : {
74-
"and a valid target server": runner.assertProxied('localhost', 8120, 8121, function (callback) {
74+
"and a valid target server": runner.assertProxied('localhost', 8120, 8121, false, false, function (callback) {
7575
runner.startProxyServerWithForwarding(8120, 8121, 'localhost', forwardOptions, callback);
7676
}),
7777
"and also a valid target server": runner.assertHeaders(8122, "x-forwarded-for", function (callback) {
7878
runner.startProxyServerWithForwarding(8122, 8123, 'localhost', forwardOptions, callback);
7979
}),
80-
"and without a valid forward server": runner.assertProxied('localhost', 8124, 8125, function (callback) {
80+
"and without a valid forward server": runner.assertProxied('localhost', 8124, 8125, false, false, function (callback) {
8181
runner.startProxyServerWithForwarding(8124, 8125, 'localhost', badForwardOptions, callback);
8282
})
8383
}

test/http/routing-proxy-test.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ var fileOptions = {
2929
var defaultOptions = {
3030
router: {
3131
"foo.com": "127.0.0.1:8091",
32-
"bar.com": "127.0.0.1:8092"
32+
"bar.com": "127.0.0.1:8092",
33+
"baz.com/taco": "127.0.0.1:8098",
34+
"pizza.com/taco/muffins": "127.0.0.1:8099",
3335
}
3436
};
3537

@@ -50,6 +52,8 @@ vows.describe('node-http-proxy/routing-proxy/' + testName).addBatch({
5052
},
5153
"an incoming request to foo.com": runner.assertProxied('foo.com', 8090, 8091),
5254
"an incoming request to bar.com": runner.assertProxied('bar.com', 8090, 8092),
55+
"an incoming request to baz.com/taco": runner.assertProxied('baz.com', 8090, 8098, "/taco", "/"),
56+
"an incoming request to pizza.com/taco/muffins": runner.assertProxied('pizza.com', 8090, 8099, "/taco/muffins", "/taco"),
5357
"an incoming request to unknown.com": runner.assertResponseCode(8090, 404)
5458
},
5559
"and routing by Hostname": {

0 commit comments

Comments
 (0)