Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public byte[] modifyContent(byte[] content, @Nullable MediaType contentType) {
while (matcher.find()) {
String prefix;
if (matcher.groupCount() > 0) {
if (matcher.start(1) == -1) {
continue;
}
prefix = original.substring(previous, matcher.start(1));
previous = matcher.end(1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ void contentThatDoesNotMatchIsUnchanged() {
.isEqualTo("{\"id\" : \"CA76-ED42-11CE-BACD\"}".getBytes());
}

@Test
void whenPatternHasAGroupOnlyTheFirstGroupIsReplaced() {
Pattern pattern = Pattern.compile("\"id\":\"([^\"]+)\"");
PatternReplacingContentModifier contentModifier = new PatternReplacingContentModifier(pattern, "<<id>>");
assertThat(contentModifier.modifyContent("{\"id\":\"abc\",\"name\":\"alpha\"}".getBytes(), null))
.isEqualTo("{\"id\":\"<<id>>\",\"name\":\"alpha\"}".getBytes());
}

@Test
void matchInWhichTheFirstGroupDoesNotParticipateIsUnchanged() {
Pattern pattern = Pattern.compile("\"id\":\"([^\"]+)\"|\"created\":\"[^\"]+\"");
PatternReplacingContentModifier contentModifier = new PatternReplacingContentModifier(pattern, "<<masked>>");
assertThat(contentModifier.modifyContent("{\"created\":\"2026-10-16\",\"id\":\"abc\"}".getBytes(), null))
.isEqualTo("{\"created\":\"2026-10-16\",\"id\":\"<<masked>>\"}".getBytes());
}

@Test
void matchInWhichAnOptionalFirstGroupIsAbsentIsUnchanged() {
Pattern pattern = Pattern.compile("[0-9]+(-SNAPSHOT)?");
PatternReplacingContentModifier contentModifier = new PatternReplacingContentModifier(pattern, "-RELEASE");
assertThat(contentModifier.modifyContent("1.0 2.0-SNAPSHOT".getBytes(), null))
.isEqualTo("1.0 2.0-RELEASE".getBytes());
}

@Test
void encodingIsPreservedUsingCharsetFromContentType() {
String japaneseContent = "\u30b3\u30f3\u30c6\u30f3\u30c4";
Expand Down
Loading