CompactingJsonBodyFilter implements BodyFilter { @Override public String filter(@Nullable final String contentType, final String body) { if (!JsonMediaType.JSON.test(contentType)) { return body; } try { return compactor.compact(body); } catch (final IOException e) { log.trace("Unable to compact body, is it a JSON?. Keep it as-is: `{}`", e.getMessage()); return body; } } CompactingJsonBodyFilter(); @Override String filter(@Nullable final String contentType, final String body);  }
@Test void shouldIgnoreEmptyBody() { final String filtered = unit.filter("application/json", ""); assertThat(filtered, is("")); }
@Test void shouldIgnoreInvalidContent() { final String invalidBody = "{\ninvalid}"; final String filtered = unit.filter("application/json", invalidBody); assertThat(filtered, is(invalidBody)); }
@Test void shouldIgnoreInvalidContentType() { final String filtered = unit.filter("text/plain", pretty); assertThat(filtered, is(pretty)); }
@Test void shouldTransformValidJsonRequestWithSimpleContentType() { final String filtered = unit.filter("application/json", pretty); assertThat(filtered, is(compacted)); }
@Test void shouldTransformValidJsonRequestWithCompatibleContentType() { final String filtered = unit.filter("application/custom+json", pretty); assertThat(filtered, is(compacted)); }
@Test void shouldSkipInvalidJsonLookingLikeAValidOne() { final String invalidJson = "{invalid}"; final String filtered = unit.filter("application/custom+json", invalidJson); assertThat(filtered, is(invalidJson)); }