Skip to content

Commit 16d6020

Browse files
committed
Updated README with link to good test cases
1 parent 8ea5444 commit 16d6020

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed

README.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ npm install http-proxy-rules --save
2020
// Set up proxy rules instance
2121
var proxyRules = new HttpProxyRules({
2222
rules: {
23-
'.*/test': 'http://localhost:8080/cool',
24-
'.*/test2/': 'http://localhost:8080/cool2/'
23+
'.*/test': 'http://localhost:8080/cool', // Rule (1)
24+
'.*/test2/': 'http://localhost:8080/cool2/' // Rule (2)
2525
},
26-
default: 'http://localhost:8080'
26+
default: 'http://localhost:8080' // default target
2727
});
2828

2929
// Create reverse proxy instance
@@ -33,7 +33,7 @@ npm install http-proxy-rules --save
3333
// and proxy rules to proxy requests to different targets
3434
http.createServer(function(req, res) {
3535

36-
// a test method is exposed on the proxy rules instance
36+
// a match method is exposed on the proxy rules instance
3737
// to test a request to see if it matches against one of the specified rules
3838
var target = proxyRules.match(req);
3939
if (target) {
@@ -47,6 +47,8 @@ npm install http-proxy-rules --save
4747
}).listen(6010, cb);
4848
```
4949

50+
Given the object we used to initialize the `HttpProxyRules` instance above, here are some [**examples**](test/index.tests.js#L38) of how sample url paths would be translated.
51+
5052
## Options
5153

5254
You can initialize a new `http-proxy-rules` instance with the following options:
@@ -57,7 +59,7 @@ You can initialize a new `http-proxy-rules` instance with the following options:
5759
default: '' // (optional) if no rules matched, translate url path to specified default
5860
}
5961
```
60-
The rules object contains a set of key-value pairs mapping a regex-supported url path to a target route. The target route must include the protocol (e.g., http) and the FQDN. See the [tests](test/index.tests.js) for examples of how incoming route url paths may be translated with the use of this module.
62+
The rules object contains a set of key-value pairs mapping a regex-supported url path to a target route. The module only tries to match the visited url path, and not the entire url, with a specified rule. The target route must include the protocol (e.g., http) and the FQDN. See the [tests](test/index.tests.js) for examples of how incoming route url paths may be translated with the use of this module.
6163

6264
## Other Notes
6365
* `(?:\\W|$)` is appended to the end of the regex-supported url path, so that if there is a key like `.*/test` in the rules, the module matches paths `/test`, `/test/`, `/test?` but not `/testing`.

example/simple.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ module.exports = function spawnReverseProxy(cb) {
88
// Set up proxy rules instance
99
var proxyRules = new HttpProxyRules({
1010
rules: {
11-
'.*/test': 'http://localhost:8080/cool',
12-
'.*/test2/': 'http://localhost:8080/cool2/'
11+
'.*/test': 'http://localhost:8080/cool', // Rule (1)
12+
'.*/test2/': 'http://localhost:8080/cool2/' // Rule (2)
1313
},
14-
default: 'http://localhost:8080'
14+
default: 'http://localhost:8080' // default target
1515
});
1616

1717
// Create reverse proxy instance
@@ -21,7 +21,7 @@ module.exports = function spawnReverseProxy(cb) {
2121
// and proxy rules to proxy requests to different targets
2222
http.createServer(function(req, res) {
2323

24-
// a test method is exposed on the proxy rules instance
24+
// a match method is exposed on the proxy rules instance
2525
// to test a request to see if it matches against one of the specified rules
2626
var target = proxyRules.match(req);
2727
if (target) {

test/index.tests.js

+30-12
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var spawnReverseProxy = require('../example/simple'),
1010
describe('Proxy Routes', function () {
1111
var proxyServerPort = 6010;
1212
var targetPort = 8080;
13-
var targetPrefix = '127.0.0.1:' + targetPort;
13+
var targetFQDN = '127.0.0.1';
1414

1515
before(function (done) {
1616
// runs before all tests in this block
@@ -35,32 +35,50 @@ describe('Proxy Routes', function () {
3535

3636
it('should translate the url correctly', function (done) {
3737

38-
// Feel free to add more cases
3938
var urlTranslationTests = [{
39+
// visited path matches rule (1).
4040
visitedPath: '/test',
41-
newUrlTarget: targetPrefix + '/cool'
41+
newUrlTarget: targetFQDN + targetPort + '/cool'
4242
}, {
43+
// visited path matches rule (1).
44+
// whatever portion is not part of the match is carried over
45+
// to the new path.
46+
// in this case, the '/' is carried over.
4347
visitedPath: '/test/',
44-
newUrlTarget: targetPrefix + '/cool/'
48+
newUrlTarget: targetFQDN + targetPort + '/cool/'
4549
}, {
50+
// visited path matches rule (1).
51+
// query parameters are carried over.
4652
visitedPath: '/test?hi=5/',
47-
newUrlTarget: targetPrefix + '/cool?hi=5/'
53+
newUrlTarget: targetFQDN + targetPort + '/cool?hi=5/'
4854
}, {
55+
// visited path matches rule (2).
56+
// the unmatched portion '/yo' is carried over.
4957
visitedPath: '/test2/yo',
50-
newUrlTarget: targetPrefix + '/cool2/yo'
58+
newUrlTarget: targetFQDN + targetPort + '/cool2/yo'
5159
}, {
60+
// visited path matches rule (1).
61+
// note that the key is interpreted as a regex expression and the
62+
// module matches against the visited path, and not the entire url.
5263
visitedPath: '/fuzzyshoe/test',
53-
newUrlTarget: targetPrefix + '/cool'
64+
newUrlTarget: targetFQDN + targetPort + '/cool'
5465
}, {
66+
// visited path matches rule (1).
67+
// the unmatched portion '/seven' is carried over.
5568
visitedPath: '/test/seven',
56-
newUrlTarget: targetPrefix + '/cool/seven'
69+
newUrlTarget: targetFQDN + targetPort + '/cool/seven'
5770
}, {
58-
// should match /test but not /testalmost
71+
// no rule matched, so the module uses the specified default target.
72+
// the entire visited path is carried over.
73+
// see the `Other Notes` section of README to see why specifying
74+
// the rule `.*/test` does not match `/testalmost`.
5975
visitedPath: '/testalmost',
60-
newUrlTarget: targetPrefix + '/testalmost'
76+
newUrlTarget: targetFQDN + targetPort + '/testalmost'
6177
}, {
78+
// no rule matched, so the module uses the specified default target.
79+
// the entire visited path is carried over.
6280
visitedPath: '/testalmost/5',
63-
newUrlTarget: targetPrefix + '/testalmost/5'
81+
newUrlTarget: targetFQDN + targetPort + '/testalmost/5'
6482
}
6583
];
6684

@@ -73,7 +91,7 @@ describe('Proxy Routes', function () {
7391
}, function processResp(err, res, body) {
7492

7593
expect(res.statusCode).to.equal(200);
76-
expect(targetPrefix + body.translatedPath).to.equal(comparisonObj.newUrlTarget);
94+
expect(targetFQDN + targetPort + body.translatedPath).to.equal(comparisonObj.newUrlTarget);
7795
cb();
7896
});
7997
}, done);

0 commit comments

Comments
 (0)