JsonBodyFilters { @API(status = MAINTAINED) public static BodyFilter replaceJsonStringProperty( final Set<String> properties, final String replacement) { return replaceJsonStringProperty(properties::contains, replacement); } private  JsonBodyFilters(); @API(status = MAINTAINED) static BodyFilter accessToken(); @API(status = MAINTAINED) static BodyFilter replaceJsonStringProperty(
            final Set<String> properties, final String replacement); @API(status = EXPERIMENTAL) static BodyFilter replaceJsonStringProperty(
            final Predicate<String> predicate, final String replacement); @API(status = EXPERIMENTAL) static BodyFilter replaceJsonNumberProperty(
            final Set<String> properties, final Number replacement); @API(status = EXPERIMENTAL) static BodyFilter replaceJsonNumberProperty(
            final Predicate<String> predicate, final Number replacement); @API(status = EXPERIMENTAL) static BodyFilter replacePrimitiveJsonProperty(
            final Predicate<String> predicate, final String replacement);  }
@Test void shouldNotFilterNonJsonMediaType() { final BodyFilter unit = replaceJsonStringProperty( singleton("foo"), "XXX"); final String actual = unit.filter( "text/plain", "{\"foo\":\"secret\",\"bar\":\"public\"}"); assertThat(actual, is("{\"foo\":\"secret\",\"bar\":\"public\"}")); }
@Test void shouldFilterPropertyWithQuotationMark() { final BodyFilter unit = replaceJsonStringProperty( singleton("password"), "XXX"); final String actual = unit.filter( contentType, "{\"password\":\"abc\\\"!?$123\",\"bar\":\"public\"}"); assertThat(actual, is("{\"password\":\"XXX\",\"bar\":\"public\"}")); }
@Test void shouldFilterPropertyWithEscapedBackslash() { final BodyFilter unit = replaceJsonStringProperty( singleton("password"), "XXX"); final String actual = unit.filter( contentType, "{\"password\":\"abc\\\\\",\"bar\":\"public\"}"); assertThat(actual, is("{\"password\":\"XXX\",\"bar\":\"public\"}")); }
@Test void shouldFilterPropertyWithQuotationMarkAndEscapedBackslash() { final BodyFilter unit = replaceJsonStringProperty( singleton("password"), "XXX"); final String actual = unit.filter( contentType, "{\"password\":\"abc\\\"!?$123\\\\\",\"bar\":\"public\"}"); assertThat(actual, is("{\"password\":\"XXX\",\"bar\":\"public\"}")); }
@Test void shouldFilterNotEmptyJSONProperty() { final BodyFilter unit = replaceJsonStringProperty( singleton("foo"), "XXX"); final String actual = unit.filter( contentType, "{\"foo\":\"secret\",\"bar\":\"public\"}"); assertThat(actual, is("{\"foo\":\"XXX\",\"bar\":\"public\"}")); }
@Test void shouldFilterEmptyJSONProperty() { final BodyFilter unit = replaceJsonStringProperty( singleton("foo"), "XXX"); final String actual = unit.filter( contentType, "{\"foo\":\"\",\"bar\":\"public\"}"); assertThat(actual, is("{\"foo\":\"XXX\",\"bar\":\"public\"}")); }
@Test void filtersNestedProperty() { final BodyFilter unit = replaceJsonStringProperty(singleton("date"), "XXX"); final String actual = unit.filter( contentType, "{\"id\":\"123\",\"meta\":{\"date\":\"2020-07-27\"}}"); assertThat(actual, is("{\"id\":\"123\",\"meta\":{\"date\":\"XXX\"}}")); }
@Test void shouldFilterNullProperty() { final BodyFilter unit = replaceJsonStringProperty( singleton("foo"), "XXX"); final String actual = unit.filter( contentType, "{\"foo\":null,\"bar\":\"public\"}"); assertThat(actual, is("{\"foo\":\"XXX\",\"bar\":\"public\"}")); }
@Test void shouldFilterMatchingNullPropertyOnly() { final BodyFilter unit = replaceJsonStringProperty( singleton("foo"), "XXX"); final String actual = unit.filter( contentType, "{\"foo\":null,\"bar\":null}"); assertThat(actual, is("{\"foo\":\"XXX\",\"bar\":null}")); }
@Test void shouldFilterUsingGivenSetSemantics() { final SortedSet<String> properties = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); properties.add("FOO"); final BodyFilter unit = replaceJsonStringProperty(properties, "XXX"); final String actual = unit.filter( contentType, "{\"foo\":null,\"bar\":null}"); assertThat(actual, is("{\"foo\":\"XXX\",\"bar\":null}")); }
@Test void shouldNotFailToProcessHugeJsonPayload() throws IOException { final byte[] bytes = Files.readAllBytes( Paths.get("src/test/resources/huge-sample.json")); final String body = new String(bytes, UTF_8); final BodyFilter unit = replaceJsonStringProperty( new HashSet<>(asList( "name", "gender", "phone", "email", "age", "eyeColor")), "XXX"); final String actual = unit.filter(contentType, body); assertThat(actual, containsString("\"gender\": \"XXX\"")); }
@Test void supportsVeryLargeValues() throws IOException { final byte[] bytes = Files.readAllBytes( Paths.get("src/test/resources/huge-value.json")); final String body = new String(bytes, UTF_8); final BodyFilter unit = replaceJsonStringProperty( singleton("password"), "XXX"); final String actual = unit.filter(contentType, body); assertThat(actual, containsString("\"password\": \"XXX\"")); }
@Test void supportsValuesWithManyEscapedDoubleQuotes() throws IOException { final byte[] bytes = Files.readAllBytes( Paths.get("src/test/resources/many-quotes.json")); final String original = new String(bytes, UTF_8); final BodyFilter unit = replaceJsonStringProperty( singleton("password"), "XXX"); final String actual = unit.filter(contentType, original); assertThat(actual, is(original)); }
@Test void supportsVeryLargeEmbeddedJsonValues() throws IOException { final byte[] bytes = Files.readAllBytes( Paths.get("src/test/resources/huge-json-value.json")); final String original = new String(bytes, UTF_8); final BodyFilter unit = replaceJsonStringProperty( singleton("variables"), "XXX"); final String actual = unit.filter(contentType, original); assertThat(actual, is(original)); }