1
- /*! Angular API Mock v0.2.1
1
+ /*! Angular API Mock v0.3.0
2
2
* Licensed with MIT
3
3
* Made with ♥ from Seriema + Redhorn */
4
4
/* Create the main module, `apiMock`. It's the one that needs to be included in
@@ -46,11 +46,12 @@ angular.module('apiMock', [])
46
46
var $log ;
47
47
var $q ;
48
48
var config = {
49
+ defaultMock : false ,
49
50
mockDataPath : '/mock_data' ,
50
51
apiPath : '/api' ,
51
52
disable : false ,
52
53
stripQueries : true ,
53
- delay : 0 ,
54
+ delay : 0
54
55
} ;
55
56
var fallbacks = [ ] ;
56
57
@@ -112,11 +113,11 @@ angular.module('apiMock', [])
112
113
}
113
114
114
115
if ( angular . isArray ( toSerialize ) ) {
115
- angular . forEach ( toSerialize , function ( value , index ) {
116
+ angular . forEach ( toSerialize , function ( value , index ) {
116
117
serialize ( value , prefix + '[' + ( angular . isObject ( value ) ? index : '' ) + ']' ) ;
117
118
} ) ;
118
119
} else if ( angular . isObject ( toSerialize ) && ! angular . isDate ( toSerialize ) ) {
119
- forEachSorted ( toSerialize , function ( value , key ) {
120
+ forEachSorted ( toSerialize , function ( value , key ) {
120
121
serialize ( value , prefix +
121
122
( topLevel ? '' : '[' ) +
122
123
key +
@@ -139,7 +140,7 @@ angular.module('apiMock', [])
139
140
var paramArray = paramString . split ( '&' ) ;
140
141
141
142
var result = { } ;
142
- angular . forEach ( paramArray , function ( param ) {
143
+ angular . forEach ( paramArray , function ( param ) {
143
144
param = param . split ( '=' ) ;
144
145
result [ param [ 0 ] ] = param [ 1 ] || '' ;
145
146
} ) ;
@@ -166,9 +167,13 @@ angular.module('apiMock', [])
166
167
167
168
function getParameter ( req ) {
168
169
var mockValue = localMock ( req ) ;
170
+ // Note: `false` is a valid option, so we can't use falsy-checks.
169
171
if ( mockValue === undefined ) {
170
172
mockValue = globalMock ( ) ;
171
173
}
174
+ if ( mockValue === undefined ) {
175
+ mockValue = config . defaultMock ;
176
+ }
172
177
173
178
return mockValue ;
174
179
}
@@ -209,7 +214,33 @@ angular.module('apiMock', [])
209
214
}
210
215
211
216
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 ;
213
244
}
214
245
215
246
function prepareFallback ( req ) {
@@ -237,21 +268,23 @@ angular.module('apiMock', [])
237
268
238
269
// replace apiPath with mockDataPath.
239
270
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 ) ;
241
273
242
274
var split = redirectedPath . split ( '?' ) ;
243
275
var newPath = split [ 0 ] ;
244
276
var queries = split [ 1 ] || '' ;
245
277
246
278
// query strings are stripped by default (like ?search=banana).
247
279
if ( ! config . stripQueries ) {
280
+
248
281
//test if we have query params
249
282
//if we do merge them on to the params object
250
283
var queryParamsFromUrl = queryStringToObject ( queries ) ;
251
284
var params = angular . extend ( req . params || { } , queryParamsFromUrl ) ;
252
285
253
286
//test if there is already a trailing /
254
- if ( newPath [ newPath . length - 1 ] !== '/' ) {
287
+ if ( newPath [ newPath . length - 1 ] !== '/' ) {
255
288
newPath += '/' ;
256
289
}
257
290
@@ -357,7 +390,7 @@ angular.module('apiMock', [])
357
390
} ] ;
358
391
} )
359
392
360
- . service ( 'httpInterceptor' , [ '$injector' , '$q' , '$timeout' , 'apiMock' , function ( $injector , $q , $timeout , apiMock ) {
393
+ . service ( 'httpInterceptor' , [ '$injector' , '$q' , '$timeout' , 'apiMock' , function ( $injector , $q , $timeout , apiMock ) {
361
394
/* The main service. Is jacked in as a interceptor on `$http` so it gets called
362
395
* on every http call. This allows us to do our magic. It uses the provider
363
396
* `apiMock` to determine if a mock should be done, then do the actual mocking.
@@ -373,8 +406,9 @@ angular.module('apiMock', [])
373
406
var deferred = $q . defer ( ) ;
374
407
375
408
$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 ) ) ;
378
412
} ,
379
413
apiMock . getDelay ( ) ,
380
414
true // Trigger a $digest.
@@ -396,7 +430,7 @@ angular.module('apiMock', [])
396
430
deferred . resolve ( data ) ;
397
431
} ) ;
398
432
} else {
399
- deferred . reject ( rej ) ;
433
+ deferred . reject ( rej ) ;
400
434
}
401
435
} ,
402
436
apiMock . getDelay ( ) ,
0 commit comments