1- /*! Angular API Mock v0.2.1
1+ /*! Angular API Mock v0.3.0
22 * Licensed with MIT
33 * Made with ♥ from Seriema + Redhorn */
44/* Create the main module, `apiMock`. It's the one that needs to be included in
@@ -46,11 +46,12 @@ angular.module('apiMock', [])
4646 var $log ;
4747 var $q ;
4848 var config = {
49+ defaultMock : false ,
4950 mockDataPath : '/mock_data' ,
5051 apiPath : '/api' ,
5152 disable : false ,
5253 stripQueries : true ,
53- delay : 0 ,
54+ delay : 0
5455 } ;
5556 var fallbacks = [ ] ;
5657
@@ -112,11 +113,11 @@ angular.module('apiMock', [])
112113 }
113114
114115 if ( angular . isArray ( toSerialize ) ) {
115- angular . forEach ( toSerialize , function ( value , index ) {
116+ angular . forEach ( toSerialize , function ( value , index ) {
116117 serialize ( value , prefix + '[' + ( angular . isObject ( value ) ? index : '' ) + ']' ) ;
117118 } ) ;
118119 } else if ( angular . isObject ( toSerialize ) && ! angular . isDate ( toSerialize ) ) {
119- forEachSorted ( toSerialize , function ( value , key ) {
120+ forEachSorted ( toSerialize , function ( value , key ) {
120121 serialize ( value , prefix +
121122 ( topLevel ? '' : '[' ) +
122123 key +
@@ -139,7 +140,7 @@ angular.module('apiMock', [])
139140 var paramArray = paramString . split ( '&' ) ;
140141
141142 var result = { } ;
142- angular . forEach ( paramArray , function ( param ) {
143+ angular . forEach ( paramArray , function ( param ) {
143144 param = param . split ( '=' ) ;
144145 result [ param [ 0 ] ] = param [ 1 ] || '' ;
145146 } ) ;
@@ -166,9 +167,13 @@ angular.module('apiMock', [])
166167
167168 function getParameter ( req ) {
168169 var mockValue = localMock ( req ) ;
170+ // Note: `false` is a valid option, so we can't use falsy-checks.
169171 if ( mockValue === undefined ) {
170172 mockValue = globalMock ( ) ;
171173 }
174+ if ( mockValue === undefined ) {
175+ mockValue = config . defaultMock ;
176+ }
172177
173178 return mockValue ;
174179 }
@@ -209,7 +214,33 @@ angular.module('apiMock', [])
209214 }
210215
211216 function isApiPath ( url ) {
212- return url . indexOf ( config . apiPath ) === 0 ;
217+ return ( apiPathMatched ( url , config . apiPath ) !== undefined ) ;
218+ }
219+
220+ function apiPathMatched ( url , apiPath ) {
221+ var match ; // Lets initially assume undefined as no match
222+
223+ if ( angular . isArray ( apiPath ) ) {
224+ angular . forEach ( apiPath , function ( path ) {
225+ if ( match ) { return ; } // Hack to skip more recursive calls if already matched
226+ var found = apiPathMatched ( url , path ) ;
227+ if ( found ) {
228+ match = found ;
229+ }
230+ } ) ;
231+ }
232+ if ( match ) {
233+ return match ;
234+ }
235+ if ( apiPath instanceof RegExp ) {
236+ if ( apiPath . test ( url ) ) {
237+ return apiPath ;
238+ }
239+ }
240+ if ( ( url . toString ( ) . indexOf ( apiPath ) === 0 ) ) {
241+ return apiPath ;
242+ }
243+ return match ;
213244 }
214245
215246 function prepareFallback ( req ) {
@@ -237,21 +268,23 @@ angular.module('apiMock', [])
237268
238269 // replace apiPath with mockDataPath.
239270 var oldPath = req . url ;
240- var redirectedPath = req . url . replace ( config . apiPath , config . mockDataPath ) ;
271+
272+ var redirectedPath = req . url . replace ( apiPathMatched ( req . url , config . apiPath ) , config . mockDataPath ) ;
241273
242274 var split = redirectedPath . split ( '?' ) ;
243275 var newPath = split [ 0 ] ;
244276 var queries = split [ 1 ] || '' ;
245277
246278 // query strings are stripped by default (like ?search=banana).
247279 if ( ! config . stripQueries ) {
280+
248281 //test if we have query params
249282 //if we do merge them on to the params object
250283 var queryParamsFromUrl = queryStringToObject ( queries ) ;
251284 var params = angular . extend ( req . params || { } , queryParamsFromUrl ) ;
252285
253286 //test if there is already a trailing /
254- if ( newPath [ newPath . length - 1 ] !== '/' ) {
287+ if ( newPath [ newPath . length - 1 ] !== '/' ) {
255288 newPath += '/' ;
256289 }
257290
@@ -357,7 +390,7 @@ angular.module('apiMock', [])
357390 } ] ;
358391 } )
359392
360- . service ( 'httpInterceptor' , [ '$injector' , '$q' , '$timeout' , 'apiMock' , function ( $injector , $q , $timeout , apiMock ) {
393+ . service ( 'httpInterceptor' , [ '$injector' , '$q' , '$timeout' , 'apiMock' , function ( $injector , $q , $timeout , apiMock ) {
361394 /* The main service. Is jacked in as a interceptor on `$http` so it gets called
362395 * on every http call. This allows us to do our magic. It uses the provider
363396 * `apiMock` to determine if a mock should be done, then do the actual mocking.
@@ -373,8 +406,9 @@ angular.module('apiMock', [])
373406 var deferred = $q . defer ( ) ;
374407
375408 $timeout (
376- function ( ) {
377- deferred . resolve ( apiMock . onResponse ( res ) ) ; // TODO: Apparently, no tests break regardless what this resolves to. Fix the tests!
409+ function ( ) {
410+ // TODO: Apparently, no tests break regardless what this resolves to. Fix the tests!
411+ deferred . resolve ( apiMock . onResponse ( res ) ) ;
378412 } ,
379413 apiMock . getDelay ( ) ,
380414 true // Trigger a $digest.
@@ -396,7 +430,7 @@ angular.module('apiMock', [])
396430 deferred . resolve ( data ) ;
397431 } ) ;
398432 } else {
399- deferred . reject ( rej ) ;
433+ deferred . reject ( rej ) ;
400434 }
401435 } ,
402436 apiMock . getDelay ( ) ,
0 commit comments