LinkMaskingContentModifier(String mask) { this.contentModifier = new PatternReplacingContentModifier(LINK_HREF, mask); }
@Test public void contentThatDoesNotMatchIsUnchanged() throws Exception { Pattern pattern = Pattern.compile( "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", Pattern.CASE_INSENSITIVE); PatternReplacingContentModifier contentModifier = new PatternReplacingContentModifier( pattern, "<<uuid>>"); assertThat(contentModifier .modifyContent("{\"id\" : \"CA76-ED42-11CE-BACD\"}".getBytes(), null)) .isEqualTo("{\"id\" : \"CA76-ED42-11CE-BACD\"}".getBytes()); }
@Test public void patternsAreReplaced() throws Exception { Pattern pattern = Pattern.compile( "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", Pattern.CASE_INSENSITIVE); PatternReplacingContentModifier contentModifier = new PatternReplacingContentModifier( pattern, "<<uuid>>"); assertThat(contentModifier.modifyContent( "{\"id\" : \"CA761232-ED42-11CE-BACD-00AA0057B223\"}".getBytes(), null)) .isEqualTo("{\"id\" : \"<<uuid>>\"}".getBytes()); }
@Test public void encodingIsPreserved() { String japaneseContent = "\u30b3\u30f3\u30c6\u30f3\u30c4"; Pattern pattern = Pattern.compile("[0-9]+"); PatternReplacingContentModifier contentModifier = new PatternReplacingContentModifier( pattern, "<<number>>"); assertThat(contentModifier.modifyContent((japaneseContent + " 123").getBytes(), new MediaType("text", "plain", Charset.forName("UTF-8")))) .isEqualTo((japaneseContent + " <<number>>").getBytes()); }
/** * Returns an {@code OperationPreprocessor} that will modify the content of the * request or response by replacing occurrences of the given {@code pattern} with the * given {@code replacement}. * @param pattern the pattern * @param replacement the replacement * @return the preprocessor */ public static OperationPreprocessor replacePattern(Pattern pattern, String replacement) { return new ContentModifyingOperationPreprocessor( new PatternReplacingContentModifier(pattern, replacement)); }