applyBodyParameterCensors method

String applyBodyParameterCensors(
  1. String body,
  2. ContentType contentType
)

Applies the body parameter censors to the given body with the given contentType.

Currently only supports JSON bodies.

Implementation

// TODO: Only works on JSON bodies
String applyBodyParameterCensors(String body, ContentType contentType) {
  if (body.isEmpty) {
    // short circuit if body is empty
    return body;
  }

  if (_bodyElementsToCensor.isEmpty) {
    // short circuit if there are no censors to apply
    return body;
  }

  try {
    switch (contentType) {
      case ContentType.html:
      case ContentType.text:
        return body; // We can't censor plaintext bodies or HTML bodies
      case ContentType.xml:
        return body; // XML parsing is not supported yet, so we can't censor XML bodies
      case ContentType.json:
        return censorJsonData(body, _censorString, _bodyElementsToCensor);
      default:
        throw Exception("Unknown content type: $contentType");
    }
  } catch (e) {
    // short circuit if body is not a valid serializable type
    throw VCRException("Body is not valid serializable type");
  }
}