@@ -25,7 +25,7 @@ function parseStatus(header) {
25
25
* Parses an HTTP header, splitting
26
26
* by colon.
27
27
*/
28
- const parseHeader = function ( header , context , request ) {
28
+ const parseHeader = function ( header , context , request ) {
29
29
header = header . split ( ': ' ) ;
30
30
31
31
return { key : normalizeHeader ( header [ 0 ] ) , value : parseValue ( header [ 1 ] , context , request ) } ;
@@ -283,27 +283,87 @@ function getBody(req, callback) {
283
283
}
284
284
285
285
function getMockedContent ( path , prefix , body , query ) {
286
- const mockName = prefix + ( getBodyOrQueryString ( body , query ) || '' ) + '.mock' ;
287
- const mockFile = join ( mockserver . directory , path , mockName ) ;
288
- let content ;
286
+ // Check for an exact match
287
+ const exactName = prefix + ( getBodyOrQueryString ( body , query ) || '' ) + '.mock' ;
288
+ let content = handleMatch ( path , exactName , fs . existsSync ) ;
289
289
290
- try {
291
- content = fs . readFileSync ( mockFile , { encoding : 'utf8' } ) ;
292
- if ( mockserver . verbose ) {
293
- console . log (
294
- 'Reading from ' + mockFile . yellow + ' file: ' + 'Matched' . green
295
- ) ;
290
+ // Compare params without regard to order
291
+ if ( ! content && query && ! body ) {
292
+ content = testForQuery ( path , prefix , query , false ) ;
293
+
294
+ // Compare params without regard to order and allow wildcards
295
+ if ( ! content ) {
296
+ content = testForQuery ( path , prefix , query , true ) ;
296
297
}
297
- } catch ( err ) {
298
- if ( mockserver . verbose ) {
299
- console . log (
300
- 'Reading from ' + mockFile . yellow + ' file: ' + 'Not matched' . red
298
+ }
299
+
300
+ // fallback option (e.g. GET.mock). ignores body and query
301
+ if ( ! content ) {
302
+ const fallbackName = prefix + '.mock' ;
303
+ content = handleMatch ( path , fallbackName , fs . existsSync ) ;
304
+ }
305
+
306
+ return content ;
307
+ }
308
+
309
+ function testForQuery ( path , prefix , query , allowWildcards ) {
310
+ // Find all files in the directory
311
+ return fs
312
+ . readdirSync ( join ( mockserver . directory , path ) )
313
+ . filter ( possibleFile => possibleFile . startsWith ( prefix ) && possibleFile . endsWith ( '.mock' ) )
314
+ . filter ( possibleFile => possibleFile . match ( / - - [ \s \S ] * _ _ / ) )
315
+ . reduce ( ( prev , possibleFile ) => {
316
+ if ( prev ) {
317
+ return prev ;
318
+ }
319
+
320
+ let isMatch = true ;
321
+ //get params from file
322
+ const paramMap = queryStringToMap ( query ) ;
323
+ const possibleFileParamMap = queryStringToMap (
324
+ possibleFile . replace ( '.mock' , '' ) . split ( '--' ) [ 1 ]
301
325
) ;
326
+
327
+ for ( const key in paramMap ) {
328
+ if ( ! isMatch ) {
329
+ continue ;
330
+ }
331
+ isMatch =
332
+ possibleFileParamMap [ key ] === paramMap [ key ] ||
333
+ ( allowWildcards && possibleFileParamMap [ key ] === '__' ) ;
334
+ }
335
+
336
+ return handleMatch ( path , possibleFile , isMatch ) ;
337
+ } , undefined ) ;
338
+ }
339
+
340
+ function queryStringToMap ( query ) {
341
+ const result = { } ;
342
+ query . split ( '&' ) . forEach ( param => {
343
+ const [ key , val ] = param . split ( '=' ) ;
344
+ result [ key ] = val ;
345
+ } ) ;
346
+ return result ;
347
+ }
348
+
349
+ function handleMatch ( path , fileName , isMatchOrTest ) {
350
+ const mockFile = join ( mockserver . directory , path , fileName ) ;
351
+
352
+ let isMatch = isMatchOrTest ;
353
+ if ( typeof isMatchOrTest === 'function' ) {
354
+ isMatch = isMatchOrTest ( mockFile ) ;
355
+ }
356
+
357
+ if ( isMatch ) {
358
+ if ( mockserver . verbose ) {
359
+ console . log ( 'Reading from ' + mockFile . yellow + ' file: ' + 'Matched' . green ) ;
302
360
}
303
- content = ( body || query ) && getMockedContent ( path , prefix ) ;
361
+ return fs . readFileSync ( mockFile , { encoding : 'utf8' } ) ;
304
362
}
305
363
306
- return content ;
364
+ if ( mockserver . verbose ) {
365
+ console . log ( 'Reading from ' + mockFile . yellow + ' file: ' + 'Not matched' . red ) ;
366
+ }
307
367
}
308
368
309
369
function getContentFromPermutations ( path , method , body , query , permutations ) {
0 commit comments