mockRuleMatches function

bool mockRuleMatches(
  1. MockRule rule,
  2. String method,
  3. String url, [
  4. String? appId,
])

Reference matcher (identical semantics in every SDK):

  • method: * matches anything, otherwise case-insensitive equality;
  • contains / exact / glob: case-insensitive; glob * = any run of chars (incl. /), ? = one char, and must match the WHOLE url;
  • regex: pattern used as-is (case-sensitive), searched anywhere in the url; an invalid regex never matches;
  • apps: empty/null = all apps, otherwise must contain appId.

Implementation

bool mockRuleMatches(MockRule rule, String method, String url, [String? appId]) {
  if (!rule.enabled) return false;
  final apps = rule.apps;
  if (apps != null &&
      apps.isNotEmpty &&
      (appId == null || !apps.contains(appId))) {
    return false;
  }
  if (rule.method != '*' && rule.method.toUpperCase() != method.toUpperCase()) {
    return false;
  }
  final u = url.toLowerCase();
  final p = rule.url.toLowerCase();
  switch (rule.match) {
    case 'contains':
      return p.isNotEmpty && u.contains(p);
    case 'exact':
      return u == p;
    case 'glob':
      return _globRegExp(p).hasMatch(u);
    case 'regex':
      final re = _userRegExp(rule.url);
      return re != null && re.hasMatch(url);
    default:
      return false;
  }
}