@@ -27,18 +27,16 @@ const debug = Debug('cypress:net-stubbing:server:intercept-request')
27
27
/**
28
28
* Returns `true` if `req` matches all supplied properties on `routeMatcher`, `false` otherwise.
29
29
*/
30
- // TODO: optimize to short-circuit on route not match
31
30
export function _doesRouteMatch ( routeMatcher : RouteMatcherOptions , req : CypressIncomingRequest ) {
32
31
const matchable = _getMatchableForRequest ( req )
33
32
34
- let match = true
35
-
36
33
// get a list of all the fields which exist where a rule needs to be succeed
37
34
const stringMatcherFields = getAllStringMatcherFields ( routeMatcher )
38
35
const booleanFields = _ . filter ( _ . keys ( routeMatcher ) , _ . partial ( _ . includes , [ 'https' , 'webSocket' ] ) )
39
36
const numberFields = _ . filter ( _ . keys ( routeMatcher ) , _ . partial ( _ . includes , [ 'port' ] ) )
40
37
41
- stringMatcherFields . forEach ( ( field ) => {
38
+ for ( let i = 0 ; i < stringMatcherFields . length ; i ++ ) {
39
+ const field = stringMatcherFields [ i ]
42
40
const matcher = _ . get ( routeMatcher , field )
43
41
let value = _ . get ( matchable , field , '' )
44
42
@@ -47,44 +45,53 @@ export function _doesRouteMatch (routeMatcher: RouteMatcherOptions, req: Cypress
47
45
}
48
46
49
47
if ( matcher . test ) {
50
- // value is a regex
51
- match = match && matcher . test ( value )
48
+ if ( ! matcher . test ( value ) ) {
49
+ return false
50
+ }
52
51
53
- return
52
+ continue
54
53
}
55
54
56
55
if ( field === 'url' ) {
57
- // for urls, check that it appears anywhere in the string
58
56
if ( value . includes ( matcher ) ) {
59
- return
57
+ continue
60
58
}
61
59
}
62
60
63
- match = match && minimatch ( value , matcher , { matchBase : true } )
64
- } )
61
+ if ( ! minimatch ( value , matcher , { matchBase : true } ) ) {
62
+ return false
63
+ }
64
+ }
65
65
66
- booleanFields . forEach ( ( field ) => {
66
+ for ( let i = 0 ; i < booleanFields . length ; i ++ ) {
67
+ const field = booleanFields [ i ]
67
68
const matcher = _ . get ( routeMatcher , field )
68
69
const value = _ . get ( matchable , field )
69
70
70
- match = match && ( matcher === value )
71
- } )
71
+ if ( matcher !== value ) {
72
+ return false
73
+ }
74
+ }
72
75
73
- numberFields . forEach ( ( field ) => {
76
+ for ( let i = 0 ; i < numberFields . length ; i ++ ) {
77
+ const field = numberFields [ i ]
74
78
const matcher = _ . get ( routeMatcher , field )
75
79
const value = _ . get ( matchable , field )
76
80
77
81
if ( matcher . length ) {
78
- // list of numbers, any one can match
79
- match = match && matcher . includes ( value )
82
+ if ( ! matcher . includes ( value ) ) {
83
+ return false
84
+ }
80
85
81
- return
86
+ continue
82
87
}
83
88
84
- match = match && ( matcher === value )
85
- } )
89
+ if ( matcher !== value ) {
90
+ return false
91
+ }
92
+ }
86
93
87
- return match
94
+ return true
88
95
}
89
96
90
97
export function _getMatchableForRequest ( req : CypressIncomingRequest ) {
0 commit comments