@@ -5,6 +5,7 @@ var queryString = require('query-string');
5
5
function mockTemplate ( ) {
6
6
var queryStringParse = '<place_query_string_parse_here>' ;
7
7
var expectations = '<place_content_here>' ;
8
+ var plugins = '<place_plugins_here>' ;
8
9
9
10
var newModule = angular . module ( 'httpMock' , [ ] ) ;
10
11
@@ -153,13 +154,26 @@ function mockTemplate() {
153
154
return expectationRequest . method . toLowerCase ( ) === configMethod ;
154
155
}
155
156
157
+ function matchByPlugins ( expectationRequest , config ) {
158
+ var match = true ;
159
+
160
+ if ( plugins . length > 0 ) {
161
+ match = plugins . reduce ( function ( value , plugin ) {
162
+ return plugin ( expectationRequest , config ) && value ;
163
+ } , true ) ;
164
+ }
165
+
166
+ return match ;
167
+ }
168
+
156
169
function match ( config , expectationRequest ) {
157
170
return matchMethod ( expectationRequest , config ) &&
158
171
endsWith ( config . url , expectationRequest . path ) &&
159
172
matchParams ( expectationRequest , config ) &&
160
173
matchData ( expectationRequest , config ) &&
161
174
matchQueryString ( expectationRequest , config ) &&
162
- matchHeaders ( expectationRequest , config ) ;
175
+ matchHeaders ( expectationRequest , config ) &&
176
+ matchByPlugins ( expectationRequest , config ) ;
163
177
}
164
178
165
179
function matchExpectation ( config ) {
@@ -353,11 +367,30 @@ function getExpectationsString(expectations){
353
367
return printExpectations . toString ( ) ;
354
368
}
355
369
370
+ function getPluginsString ( plugins ) {
371
+ if ( plugins ) {
372
+ var pluginStrings = [ ] ;
373
+
374
+ plugins . forEach ( function ( plugin ) {
375
+ pluginStrings . push ( plugin . match . toString ( ) ) ;
376
+ } ) ;
377
+
378
+ return pluginStrings . join ( ) ;
379
+ } else {
380
+ return '' ;
381
+ }
382
+ }
383
+
356
384
module . exports = function ( expectations , plugins ) {
357
385
var templateString = mockTemplate . toString ( ) ;
358
386
var template = templateString . substring ( templateString . indexOf ( '{' ) + 1 , templateString . lastIndexOf ( '}' ) ) ;
359
- var newFunc = template . replace ( / ' < p l a c e _ c o n t e n t _ h e r e > ' / , '[' + getExpectationsString ( expectations ) + ']' ) ;
360
- newFunc = newFunc . replace ( / ' < p l a c e _ q u e r y _ s t r i n g _ p a r s e _ h e r e > ' / , queryString . parse . toString ( ) ) ;
387
+ var pluginsString = getPluginsString ( plugins ) ;
388
+
389
+ var newFunc =
390
+ template
391
+ . replace ( / ' < p l a c e _ c o n t e n t _ h e r e > ' / , '[' + getExpectationsString ( expectations ) + ']' )
392
+ . replace ( / ' < p l a c e _ q u e r y _ s t r i n g _ p a r s e _ h e r e > ' / , queryString . parse . toString ( ) )
393
+ . replace ( / ' < p l a c e _ p l u g i n s _ h e r e > ' / , '[' + pluginsString + ']' ) ;
361
394
362
395
/*jslint evil: true */
363
396
return new Function ( newFunc ) ;
0 commit comments