byBody method

MatchRules byBody({
  1. List<CensorElement> ignoreElements = const [],
})

Enforces that both requests have the same body.

Ignore specific ignoreElements in the body when comparing.

MatchRules rules = MatchRules().byBody();

Implementation

MatchRules byBody({List<CensorElement> ignoreElements = const []}) {
  _by((Request received, Request recorded) {
    if (received.body == null && recorded.body == null) {
      // both have null bodies, so they match
      return true;
    } else if (received.body == null || recorded.body == null) {
      // one has a null body, so they don't match
      return false;
    } else {
      String receivedBody =
          Censors.censorJsonData(received.body!, "FILTERED", ignoreElements);
      String recordedBody =
          Censors.censorJsonData(recorded.body!, "FILTERED", ignoreElements);

      if (receivedBody == recordedBody) {
        return true;
      }
    }
    return false;
  });
  return this;
}