Preserve UTF-8 content when masking links in JSON payloads
Closes gh-705
This commit is contained in:
@@ -77,6 +77,14 @@ public class LinkMaskingContentModifierTests {
|
||||
.isEqualTo(formattedAtomPayloadWithLinks(new Link("a", "custom"), new Link("b", "custom")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void maskCanUseUtf8Characters() throws Exception {
|
||||
String ellipsis = "\u2026";
|
||||
assertThat(
|
||||
new LinkMaskingContentModifier(ellipsis).modifyContent(formattedHalPayloadWithLinks(this.links), null))
|
||||
.isEqualTo(formattedHalPayloadWithLinks(new Link("a", ellipsis), new Link("b", ellipsis)));
|
||||
}
|
||||
|
||||
private byte[] atomPayloadWithLinks(Link... links) throws JsonProcessingException {
|
||||
return new ObjectMapper().writeValueAsBytes(createAtomPayload(links));
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.restdocs.operation.preprocess;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -52,7 +53,7 @@ public class PatternReplacingContentModifierTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodingIsPreserved() {
|
||||
public void encodingIsPreservedUsingCharsetFromContentType() {
|
||||
String japaneseContent = "\u30b3\u30f3\u30c6\u30f3\u30c4";
|
||||
Pattern pattern = Pattern.compile("[0-9]+");
|
||||
PatternReplacingContentModifier contentModifier = new PatternReplacingContentModifier(pattern, "<<number>>");
|
||||
@@ -61,4 +62,14 @@ public class PatternReplacingContentModifierTests {
|
||||
.isEqualTo((japaneseContent + " <<number>>").getBytes());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodingIsPreservedUsingFallbackCharset() {
|
||||
String japaneseContent = "\u30b3\u30f3\u30c6\u30f3\u30c4";
|
||||
Pattern pattern = Pattern.compile("[0-9]+");
|
||||
PatternReplacingContentModifier contentModifier = new PatternReplacingContentModifier(pattern, "<<number>>",
|
||||
StandardCharsets.UTF_8);
|
||||
assertThat(contentModifier.modifyContent((japaneseContent + " 123").getBytes(), new MediaType("text", "plain")))
|
||||
.isEqualTo((japaneseContent + " <<number>>").getBytes());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user