Skip to content

Commit

Permalink
Ability to ignore elements when matching by body (#6)
Browse files Browse the repository at this point in the history
- Ability to ignore elements when matching by body
- Reuse censoring code, made functions static
- Header, body and query censors now case-sensitive on a per-element basis
  • Loading branch information
nwithan8 authored Jun 13, 2022
1 parent 58decd7 commit 721ec47
Show file tree
Hide file tree
Showing 18 changed files with 577 additions and 245 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## Next Release
- Improvements to censoring
- Ability to define censored elements individually, with per-element case sensitivity
- Improvements to matching
- Ability to ignore certain elements when matching by body

## v0.2.0 (2022-05-31)

- Enhance censoring to work on nested data
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,28 @@ NOTE: Censors can only be applied to JSON request and response bodies. Attemptin
import com.easypost.easyvcr;
import com.easypost.easyvcr.AdvancedSettings;
import com.easypost.easyvcr.Cassette;
import com.easypost.easyvcr.CensorElement;
import com.easypost.easyvcr.Censors;
import com.easypost.easyvcr.Mode;
import com.easypost.easyvcr.clients.httpurlconnection.RecordableHttpsURLConnection;
import com.easypost.easyvcr.clients.httpurlconnection.RecordableURL;

import java.util.ArrayList;

public class Example {
public static void main(String[] args) {
Cassette cassette = new Cassette("path/to/cassettes", "my_cassette");

AdvancedSettings advancedSettings = new AdvancedSettings();
List<String> headersToCensor = new ArrayList<>();
headersToCensor.add("Authorization"); // Hide the Authorization header
advancedSettings.censors = new Censors().hideHeader(headersToCensor);
advancedSettings.censors = new Censors().censorHeadersByKeys(headersToCensor);
advancedSettings.censors.censorBodyElements(new ArrayList<>() {{
add(new CensorElement("table", true)); // Hide the table element (case-sensitive) in the request and response body
}});
// or
advancedSettings.censors = Censors.strict(); // use the built-in strict censoring mode (hides common sensitive data)
advancedSettings.censors =
Censors.strict(); // use the built-in strict censoring mode (hides common sensitive data)

RecordableURL recordableURL =
new RecordableURL("https://www.example.com", cassette, Mode.Replay, advancedSettings);
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/easypost/easyvcr/CensorElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.easypost.easyvcr;

public class CensorElement {
/**
* The name of the element to censor.
*/
private final String name;
/**
* Whether the name must match exactly to trigger a censor.
*/
private final boolean caseSensitive;

/**
* Constructor.
* @param name The name of the element to censor.
* @param caseSensitive Whether the name must match exactly to trigger a censor.
*/
public CensorElement(String name, boolean caseSensitive) {
this.name = name;
this.caseSensitive = caseSensitive;
}

/**
* Return whether the element matches the name, accounting for case sensitivity.
* @param key The name to check.
* @return True if the element matches the name.
*/
public boolean matches(String key) {
if (caseSensitive) {
return key.equals(name);
} else {
return key.equalsIgnoreCase(name);
}
}
}
Loading

0 comments on commit 721ec47

Please sign in to comment.