Skip to content

Commit f44c681

Browse files
committed
- ensure first match in path-rewriter.
- use lodash util functions iso custom functions
1 parent df18e5b commit f44c681

File tree

4 files changed

+43
-28
lines changed

4 files changed

+43
-28
lines changed

index.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var _ = require('lodash');
12
var httpProxy = require('http-proxy');
23
var handlers = require('./lib/handlers');
34
var contextMatcher = require('./lib/context-matcher');
@@ -36,7 +37,7 @@ var httpProxyMiddleware = function (context, opts) {
3637
}
3738

3839
// Custom listener for the `proxyRes` event on `proxy`.
39-
if (isFunction(proxyOptions.onProxyRes)) {
40+
if (_.isFunction(proxyOptions.onProxyRes)) {
4041
proxy.on('proxyRes', proxyOptions.onProxyRes);
4142
}
4243

@@ -92,7 +93,7 @@ var httpProxyMiddleware = function (context, opts) {
9293
}
9394

9495
function getProxyErrorHandler () {
95-
if (isFunction(proxyOptions.onError)) {
96+
if (_.isFunction(proxyOptions.onError)) {
9697
return proxyOptions.onError; // custom error listener
9798
}
9899

@@ -104,10 +105,6 @@ var httpProxyMiddleware = function (context, opts) {
104105
console.log('[HPM] Proxy error:', err.code, targetUri);
105106
}
106107

107-
function isFunction (v) {
108-
return (v && typeof v === 'function');
109-
}
110-
111108
};
112109

113110
module.exports = httpProxyMiddleware;

lib/path-rewriter.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var _ = require('lodash');
2+
13
module.exports = {
24
create : createPathRewriter
35
}
@@ -18,9 +20,10 @@ function createPathRewriter (config) {
1820
function rewritePath (path) {
1921
var result = path;
2022

21-
rules.forEach(function (rule) {
23+
_.forEach(rules,function (rule) {
2224
if (rule.regex.test(path)) {
2325
result = result.replace(rule.regex, rule.value);
26+
return false;
2427
}
2528
});
2629

@@ -31,24 +34,20 @@ function createPathRewriter (config) {
3134
function parsePathRewriteRules (config) {
3235
var rules = [];
3336

34-
if (isObject(config) === false) {
37+
if (_.isPlainObject(config) === false) {
3538
throw new Error('[HPM] Invalid pathRewrite config. Expecting an object literal with pathRewrite configuration');
3639
} else {
37-
for (var key in config) {
38-
if (config.hasOwnProperty(key)) {
39-
rules.push({
40-
regex : new RegExp(key),
41-
value : config[key]
42-
});
43-
44-
console.log('[HPM] Proxy rewrite rule created: "' + key + '" -> "' + config[key] + '"');
45-
}
46-
}
40+
41+
_.forIn(config, function (value, key) {
42+
rules.push({
43+
regex : new RegExp(key),
44+
value : config[key]
45+
});
46+
console.log('[HPM] Proxy rewrite rule created: "' + key + '" -> "' + config[key] + '"');
47+
});
48+
4749
}
4850

4951
return rules;
5052
}
5153

52-
function isObject (val) {
53-
return Object.prototype.toString.call(val) === '[object Object]' && typeof val === 'object';
54-
}

test/path-rewriter.spec.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ describe('Path rewriting', function () {
88
describe('Configuration and usage', function () {
99
beforeEach(function () {
1010
var config = {
11-
'^/api/old' : '/api/new',
12-
'^/remove' : '',
13-
'invalid' : 'path/new',
14-
'/valid' : '/path/new'
11+
'^/api/old' : '/api/new',
12+
'^/remove' : '',
13+
'invalid' : 'path/new',
14+
'/valid' : '/path/new',
15+
'/some/specific/path' : '/awe/some/specific/path',
16+
'/some' : '/awe/some'
1517
};
1618
rewriter = pathRewriter.create(config);
1719
});
@@ -41,6 +43,12 @@ describe('Path rewriting', function () {
4143
result = rewriter('/valid/foo/bar.json');
4244
expect(result).to.equal('/path/new/foo/bar.json');
4345
});
46+
47+
it('should return first match when similar paths are configured', function () {
48+
result = rewriter('/some/specific/path/bar.json');
49+
expect(result).to.equal('/awe/some/specific/path/bar.json');
50+
});
51+
4452
});
4553

4654
describe('Invalid configuration', function () {

test/proxy-table.spec.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,20 @@ describe('Proxy Table', function () {
105105

106106
config = {
107107
target : 'http://localhost:6000',
108-
changeOrigin : true // other options should be return, such as changeOrigin
108+
changeOrigin : true // other options should be returned, such as changeOrigin
109109
}
110110

111111
configProxyTable = {
112112
target : 'http://localhost:6000',
113-
changeOrigin : true, // other options should be return, such as changeOrigin
113+
changeOrigin : true, // other options should be returned, such as changeOrigin
114114
proxyTable : {
115115
'alpha.localhost' : 'http://localhost:6001',
116116
'beta.localhost' : 'http://localhost:6002',
117117
'gamma.localhost/api' : 'http://localhost:6003',
118118
'gamma.localhost' : 'http://localhost:6004',
119-
'/rest' : 'http://localhost:6005'
119+
'/rest' : 'http://localhost:6005',
120+
'/some/specific/path' : 'http://localhost:6006',
121+
'/some' : 'http://localhost:6007'
120122
}
121123
}
122124
});
@@ -195,6 +197,15 @@ describe('Proxy Table', function () {
195197
});
196198
});
197199

200+
describe('matching order of proxyTable config', function () {
201+
it('should return first matching target when similar paths are configured', function () {
202+
req.url = '/some/specific/path';
203+
result = ProxyTable.createProxyOptions(req, configProxyTable);
204+
expect(result.target).to.equal('http://localhost:6006');
205+
expect(result.changeOrigin).to.be.true;
206+
});
207+
});
208+
198209
});
199210

200211
});

0 commit comments

Comments
 (0)