Honour Content-Type charset whenever content is turned into a String

This commit builds on the changes made in the previous commit to
make wider use of the new getContentAsString methods on
OperationRequest and OperationResponse. It also adds such a method to
OperationRequestPart. Any code that previously got the content as a
byte array and then created a String has been updated to either use
getContentAsString or to use the charset from the Content-Type header
when creating a String.

See gh-126
This commit is contained in:
Andy Wilkinson
2015-09-23 17:05:26 +01:00
parent 9b31c0ce9b
commit 3f6d4b9639
22 changed files with 202 additions and 198 deletions

View File

@@ -84,23 +84,22 @@ public class HttpRequestSnippetTests {
.request("http://localhost/foo").method("POST").content("Hello, world")
.build());
}
@Test
public void postRequestWithCharset() throws IOException {
this.snippet.expectHttpRequest("post-request-with-charset").withContents(
httpRequest(POST, "/foo")
.header(HttpHeaders.HOST, "localhost")
.header("Content-Type", "text/plain;charset=UTF-8").content(
"こんにちわ, 世界")); // Hello, World in japanese.
httpRequest(RequestMethod.POST, "/foo")
.header(HttpHeaders.HOST, "localhost")
.header("Content-Type", "text/plain;charset=UTF-8")
.content("こんにちわ, 世界")); // Hello, World in Japanese
new HttpRequestSnippet().document(new OperationBuilder(
"post-request-with-charset", this.snippet.getOutputDirectory())
.request("http://localhost/foo").method("POST")
.header("Content-Type", "text/plain;charset=UTF-8")
.content("こんにちわ, 世界")
.header("Content-Type", "text/plain;charset=UTF-8").content("こんにちわ, 世界")
.build());
}
@Test
public void postRequestWithParameter() throws IOException {
this.snippet.expectHttpRequest("post-request-with-parameter").withContents(

View File

@@ -68,8 +68,7 @@ public class HttpResponseSnippetTests {
@Test
public void responseWithHeaders() throws IOException {
this.snippet.expectHttpResponse("response-with-headers").withContents(
httpResponse(HttpStatus.OK) //
.header("Content-Type", "application/json") //
httpResponse(HttpStatus.OK).header("Content-Type", "application/json")
.header("a", "alpha"));
new HttpResponseSnippet().document(new OperationBuilder("response-with-headers",
this.snippet.getOutputDirectory()).response()
@@ -84,15 +83,18 @@ public class HttpResponseSnippetTests {
new HttpResponseSnippet().document(new OperationBuilder("response-with-content",
this.snippet.getOutputDirectory()).response().content("content").build());
}
@Test
public void responseWithCharset() throws IOException {
this.snippet.expectHttpResponse("response-with-charset").withContents(
httpResponse(OK).header("Content-Type", "text/plain;charset=UTF-8").content("コンテンツ"));
httpResponse(HttpStatus.OK).header("Content-Type",
"text/plain;charset=UTF-8").content("コンテンツ"));
new HttpResponseSnippet().document(new OperationBuilder("response-with-charset",
this.snippet.getOutputDirectory()).response().header("Content-Type", "text/plain;charset=UTF-8").content("コンテンツ").build());
this.snippet.getOutputDirectory()).response()
.header("Content-Type", "text/plain;charset=UTF-8").content("コンテンツ")
.build());
}
@Test
public void responseWithCustomSnippetAttributes() throws IOException {
this.snippet.expectHttpResponse("response-with-snippet-attributes").withContents(

View File

@@ -23,6 +23,7 @@ import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.restdocs.operation.OperationRequest;
import org.springframework.restdocs.operation.OperationRequestPart;
import org.springframework.restdocs.operation.OperationResponse;
@@ -46,7 +47,7 @@ public class ContentModifyingOperationPreprocessorTests {
new ContentModifier() {
@Override
public byte[] modifyContent(byte[] originalContent) {
public byte[] modifyContent(byte[] originalContent, MediaType mediaType) {
return "modified".getBytes();
}

View File

@@ -52,37 +52,36 @@ public class LinkMaskingContentModifierTests {
@Test
public void halLinksAreMasked() throws Exception {
assertThat(this.contentModifier.modifyContent(halPayloadWithLinks(this.links)),
assertThat(
this.contentModifier.modifyContent(halPayloadWithLinks(this.links), null),
is(equalTo(halPayloadWithLinks(this.maskedLinks))));
}
@Test
public void formattedHalLinksAreMasked() throws Exception {
assertThat(
this.contentModifier
.modifyContent(formattedHalPayloadWithLinks(this.links)),
assertThat(this.contentModifier.modifyContent(
formattedHalPayloadWithLinks(this.links), null),
is(equalTo(formattedHalPayloadWithLinks(this.maskedLinks))));
}
@Test
public void atomLinksAreMasked() throws Exception {
assertThat(this.contentModifier.modifyContent(atomPayloadWithLinks(this.links)),
is(equalTo(atomPayloadWithLinks(this.maskedLinks))));
assertThat(this.contentModifier.modifyContent(atomPayloadWithLinks(this.links),
null), is(equalTo(atomPayloadWithLinks(this.maskedLinks))));
}
@Test
public void formattedAtomLinksAreMasked() throws Exception {
assertThat(
this.contentModifier
.modifyContent(formattedAtomPayloadWithLinks(this.links)),
assertThat(this.contentModifier.modifyContent(
formattedAtomPayloadWithLinks(this.links), null),
is(equalTo(formattedAtomPayloadWithLinks(this.maskedLinks))));
}
@Test
public void maskCanBeCustomized() throws Exception {
assertThat(
new LinkMaskingContentModifier("custom")
.modifyContent(formattedAtomPayloadWithLinks(this.links)),
new LinkMaskingContentModifier("custom").modifyContent(
formattedAtomPayloadWithLinks(this.links), null),
is(equalTo(formattedAtomPayloadWithLinks(new Link("a", "custom"),
new Link("b", "custom")))));
}

View File

@@ -16,9 +16,11 @@
package org.springframework.restdocs.operation.preprocess;
import java.nio.charset.Charset;
import java.util.regex.Pattern;
import org.junit.Test;
import org.springframework.http.MediaType;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
@@ -35,13 +37,23 @@ public class PatternReplacingContentModifierTests {
@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})",
"[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()), is(equalTo("{\"id\" : \"<<uuid>>\"}".getBytes())));
assertThat(contentModifier.modifyContent(
"{\"id\" : \"CA761232-ED42-11CE-BACD-00AA0057B223\"}".getBytes(), null),
is(equalTo("{\"id\" : \"<<uuid>>\"}".getBytes())));
}
@Test
public void encodingIsPreserved() {
Pattern pattern = Pattern.compile("[0-9]+");
PatternReplacingContentModifier contentModifier = new PatternReplacingContentModifier(
pattern, "<<number>>");
assertThat(contentModifier.modifyContent("こんにちわ, 世界 123".getBytes(),
new MediaType("text", "plain", Charset.forName("UTF-8"))),
is(equalTo("こんにちわ, 世界 <<number>>".getBytes())));
}
}

View File

@@ -31,16 +31,16 @@ public class PrettyPrintingContentModifierTests {
@Test
public void prettyPrintJson() throws Exception {
assertThat(
new PrettyPrintingContentModifier().modifyContent("{\"a\":5}".getBytes()),
equalTo(String.format("{%n \"a\" : 5%n}").getBytes()));
assertThat(new PrettyPrintingContentModifier().modifyContent(
"{\"a\":5}".getBytes(), null), equalTo(String.format("{%n \"a\" : 5%n}")
.getBytes()));
}
@Test
public void prettyPrintXml() throws Exception {
assertThat(
new PrettyPrintingContentModifier().modifyContent("<one a=\"alpha\"><two b=\"bravo\"/></one>"
.getBytes()), equalTo(String.format(
assertThat(new PrettyPrintingContentModifier().modifyContent(
"<one a=\"alpha\"><two b=\"bravo\"/></one>".getBytes(), null),
equalTo(String.format(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>%n"
+ "<one a=\"alpha\">%n <two b=\"bravo\"/>%n</one>%n")
.getBytes()));
@@ -48,14 +48,15 @@ public class PrettyPrintingContentModifierTests {
@Test
public void empytContentIsHandledGracefully() throws Exception {
assertThat(new PrettyPrintingContentModifier().modifyContent("".getBytes()),
assertThat(
new PrettyPrintingContentModifier().modifyContent("".getBytes(), null),
equalTo("".getBytes()));
}
@Test
public void nonJsonAndNonXmlContentIsHandledGracefully() throws Exception {
String content = "abcdefg";
assertThat(new PrettyPrintingContentModifier().modifyContent(content.getBytes()),
equalTo(content.getBytes()));
assertThat(new PrettyPrintingContentModifier().modifyContent(content.getBytes(),
null), equalTo(content.getBytes()));
}
}