Skip to content

Commit 07ed546

Browse files
authored
chore: Short circuit _doesRouteMatch(). (#8891)
1 parent 4df05d4 commit 07ed546

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

packages/net-stubbing/lib/server/intercept-request.ts

+28-21
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,16 @@ const debug = Debug('cypress:net-stubbing:server:intercept-request')
2727
/**
2828
* Returns `true` if `req` matches all supplied properties on `routeMatcher`, `false` otherwise.
2929
*/
30-
// TODO: optimize to short-circuit on route not match
3130
export function _doesRouteMatch (routeMatcher: RouteMatcherOptions, req: CypressIncomingRequest) {
3231
const matchable = _getMatchableForRequest(req)
3332

34-
let match = true
35-
3633
// get a list of all the fields which exist where a rule needs to be succeed
3734
const stringMatcherFields = getAllStringMatcherFields(routeMatcher)
3835
const booleanFields = _.filter(_.keys(routeMatcher), _.partial(_.includes, ['https', 'webSocket']))
3936
const numberFields = _.filter(_.keys(routeMatcher), _.partial(_.includes, ['port']))
4037

41-
stringMatcherFields.forEach((field) => {
38+
for (let i = 0; i < stringMatcherFields.length; i++) {
39+
const field = stringMatcherFields[i]
4240
const matcher = _.get(routeMatcher, field)
4341
let value = _.get(matchable, field, '')
4442

@@ -47,44 +45,53 @@ export function _doesRouteMatch (routeMatcher: RouteMatcherOptions, req: Cypress
4745
}
4846

4947
if (matcher.test) {
50-
// value is a regex
51-
match = match && matcher.test(value)
48+
if (!matcher.test(value)) {
49+
return false
50+
}
5251

53-
return
52+
continue
5453
}
5554

5655
if (field === 'url') {
57-
// for urls, check that it appears anywhere in the string
5856
if (value.includes(matcher)) {
59-
return
57+
continue
6058
}
6159
}
6260

63-
match = match && minimatch(value, matcher, { matchBase: true })
64-
})
61+
if (!minimatch(value, matcher, { matchBase: true })) {
62+
return false
63+
}
64+
}
6565

66-
booleanFields.forEach((field) => {
66+
for (let i = 0; i < booleanFields.length; i++) {
67+
const field = booleanFields[i]
6768
const matcher = _.get(routeMatcher, field)
6869
const value = _.get(matchable, field)
6970

70-
match = match && (matcher === value)
71-
})
71+
if (matcher !== value) {
72+
return false
73+
}
74+
}
7275

73-
numberFields.forEach((field) => {
76+
for (let i = 0; i < numberFields.length; i++) {
77+
const field = numberFields[i]
7478
const matcher = _.get(routeMatcher, field)
7579
const value = _.get(matchable, field)
7680

7781
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+
}
8085

81-
return
86+
continue
8287
}
8388

84-
match = match && (matcher === value)
85-
})
89+
if (matcher !== value) {
90+
return false
91+
}
92+
}
8693

87-
return match
94+
return true
8895
}
8996

9097
export function _getMatchableForRequest (req: CypressIncomingRequest) {

0 commit comments

Comments
 (0)