BodyFilters { @API(status = EXPERIMENTAL) public static BodyFilter replaceFormUrlEncodedProperty(final Set<String> properties, final String replacement) { final Predicate<String> formUrlEncoded = MediaTypeQuery.compile("application/x-www-form-urlencoded"); final QueryFilter delegate = replaceQuery(properties::contains, replacement); return (contentType, body) -> formUrlEncoded.test(contentType) ? delegate.filter(body) : body; } private  BodyFilters(); @API(status = MAINTAINED) static BodyFilter defaultValue(); @API(status = EXPERIMENTAL) static BodyFilter oauthRequest(); @API(status = EXPERIMENTAL) static BodyFilter replaceFormUrlEncodedProperty(final Set<String> properties, final String replacement); @API(status = EXPERIMENTAL) static BodyFilter truncate(final int maxSize); @API(status = EXPERIMENTAL) static BodyFilter compactXml();  }
@Test void shouldFilterFormUrlEncodedBodyIfValidRequest() { final BodyFilter unit = replaceFormUrlEncodedProperty(singleton("q"), "XXX"); final String contentType = "application/x-www-form-urlencoded"; assertThat(unit.filter(contentType, "q=boots&sort=price&direction=asc"), is("q=XXX&sort=price&direction=asc")); assertThat(unit.filter(contentType, "sort=price&direction=asc&q=boots"), is("sort=price&direction=asc&q=XXX")); assertThat(unit.filter(contentType, "sort=price&q=boots&direction=asc"), is("sort=price&q=XXX&direction=asc")); assertThat(unit.filter(contentType, "sort=price&direction=asc"), is("sort=price&direction=asc")); assertThat(unit.filter(contentType, "q=boots"), is("q=XXX")); assertThat(unit.filter(contentType, ""), is("")); }
@Test void shouldNotFilterFormUrlEncodedBodyIfNotValidContentType() { final BodyFilter unit = replaceFormUrlEncodedProperty(singleton("q"), "XXX"); assertThat(unit.filter("application/json", "{\"q\":\"boots\"}"), is("{\"q\":\"boots\"}")); assertThat(unit.filter("application/xml", "<q>boots</q>"), is("<q>boots</q>")); assertThat(unit.filter("invalid", "{\"q\":\"boots\"}"), is("{\"q\":\"boots\"}")); assertThat(unit.filter(null, "{\"q\":\"boots\"}"), is("{\"q\":\"boots\"}")); }
@Test void shouldNotFilterFormUrlEncodedBodyIfNotValidContent() { final BodyFilter unit = replaceFormUrlEncodedProperty(singleton("q"), "XXX"); final String contentType = "application/x-www-form-urlencoded"; assertThat(unit.filter(contentType, "{\"q\":\"boots\"}"), is("{\"q\":\"boots\"}")); assertThat(unit.filter(contentType, "<q>boots</q>"), is("<q>boots</q>")); }