@@ -218,6 +218,69 @@ MockServer.prototype.startProxy = function() {
218
218
} ) . listen ( this . options . port ) ;
219
219
} ;
220
220
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
+
221
284
/**
222
285
* Call either with shouldProxy(req) or shouldProxy(path, method).
223
286
*/
@@ -236,15 +299,19 @@ MockServer.prototype.shouldProxy = function(path, method) {
236
299
var config = this . readConfig ( ) ;
237
300
if ( config . proxy ) {
238
301
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
+ }
244
314
}
245
- return defaultProxy ;
246
- } else if ( typeof ( entry ) === 'boolean' ) {
247
- return entry ;
248
315
}
249
316
} else {
250
317
return defaultProxy ;
0 commit comments