Skip to content

Commit 791410f

Browse files
committed
add gitignore intelj resources
extend proxy path matching functions that not exact path matching(before) but path pattern matching(after) - original source from angular-router.js
1 parent aa49f99 commit 791410f

File tree

2 files changed

+76
-8
lines changed

2 files changed

+76
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.DS_Store
2+
.idea
23
node_modules/
34
lib-cov/
45
coverage.html

lib/easymock.js

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,69 @@ MockServer.prototype.startProxy = function() {
218218
}).listen(this.options.port);
219219
};
220220

221+
/**
222+
* @param on {string} current url
223+
* @param route {Object} route regexp to match the url against
224+
* @return {?Object}
225+
*
226+
* @description
227+
* Check if the route matches the current url.
228+
*
229+
* Inspired by match in
230+
* visionmedia/express/lib/router/router.js.
231+
* angular-router.js.
232+
*/
233+
function pathRegExp(path, opts) {
234+
var insensitive = opts.caseInsensitiveMatch,
235+
ret = {
236+
originalPath: path,
237+
regexp: path
238+
},
239+
keys = ret.keys = [];
240+
241+
path = path
242+
.replace(/([().])/g, '\\$1')
243+
.replace(/(\/)?:(\w+)([\?\*])?/g, function(_, slash, key, option) {
244+
var optional = option === '?' ? option : null;
245+
var star = option === '*' ? option : null;
246+
keys.push({ name: key, optional: !!optional });
247+
slash = slash || '';
248+
return ''
249+
+ (optional ? '' : slash)
250+
+ '(?:'
251+
+ (optional ? slash : '')
252+
+ (star && '(.+?)' || '([^/]+)')
253+
+ (optional || '')
254+
+ ')'
255+
+ (optional || '');
256+
})
257+
.replace(/([\/$\*])/g, '\\$1');
258+
259+
ret.regexp = new RegExp('^' + path + '$', insensitive ? 'i' : '');
260+
return ret;
261+
}
262+
263+
function switchRouteMatcher(on, route) {
264+
var keys = route.keys,
265+
params = {};
266+
267+
if (!route.regexp) return null;
268+
269+
var m = route.regexp.exec(on);
270+
if (!m) return null;
271+
272+
for (var i = 1, len = m.length; i < len; ++i) {
273+
var key = keys[i - 1];
274+
275+
var val = m[i];
276+
277+
if (key && val) {
278+
params[key.name] = val;
279+
}
280+
}
281+
return params;
282+
}
283+
221284
/**
222285
* Call either with shouldProxy(req) or shouldProxy(path, method).
223286
*/
@@ -236,15 +299,19 @@ MockServer.prototype.shouldProxy = function(path, method) {
236299
var config = this.readConfig();
237300
if (config.proxy) {
238301
var defaultProxy = config.proxy['default'] || false;
239-
if (config.proxy.calls && config.proxy.calls[path] !== undefined) {
240-
var entry = config.proxy.calls[path];
241-
if (typeof(entry) === 'object') {
242-
if (typeof(entry[method]) === 'boolean') {
243-
return entry[method];
302+
if (config.proxy.calls) {
303+
var entry, router;
304+
for (var prop in config.proxy.calls) {
305+
entry = config.proxy.calls[prop];
306+
router = pathRegExp(prop, { caseInsensitiveMatch : false} );
307+
if (switchRouteMatcher(path, router)) {
308+
if (_.isObject(entry)) {
309+
return _.isBoolean(entry[method]) ? entry[method] : defaultProxy;
310+
}
311+
if (_.isBoolean(entry)) {
312+
return entry;
313+
}
244314
}
245-
return defaultProxy;
246-
} else if (typeof(entry) === 'boolean') {
247-
return entry;
248315
}
249316
} else {
250317
return defaultProxy;

0 commit comments

Comments
 (0)