diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessor.java index 04bf32f7..9b2316ed 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessor.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessor.java @@ -17,6 +17,7 @@ package org.springframework.restdocs.operation.preprocess; import java.net.URI; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -191,15 +192,9 @@ private void setPort(String port) { @Override public byte[] modifyContent(byte[] content, MediaType contentType) { - String input; - if (contentType != null && contentType.getCharset() != null) { - input = new String(content, contentType.getCharset()); - } - else { - input = new String(content); - } - - return modify(input).getBytes(); + Charset charset = (contentType != null && contentType.getCharset() != null) ? contentType.getCharset() + : Charset.defaultCharset(); + return modify(new String(content, charset)).getBytes(charset); } private String modify(String input) { diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessorTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessorTests.java index cbe49e4b..61378034 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessorTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessorTests.java @@ -17,6 +17,7 @@ package org.springframework.restdocs.operation.preprocess; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -26,6 +27,7 @@ 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.OperationRequestFactory; import org.springframework.restdocs.operation.OperationRequestPart; @@ -299,6 +301,32 @@ public void modifiedUriDoesNotGetDoubleEncoded() { } + @Test + public void requestContentWithNonAsciiCharactersIsPreservedWhenCharsetIsIso88591() { + this.preprocessor.scheme("https"); + String original = "café http://localhost:12345 done"; + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.parseMediaType("text/plain;charset=ISO-8859-1")); + OperationRequest request = this.requestFactory.create(URI.create("http://localhost"), HttpMethod.GET, + original.getBytes(StandardCharsets.ISO_8859_1), headers, Collections.emptyList()); + OperationRequest processed = this.preprocessor.preprocess(request); + String result = new String(processed.getContent(), StandardCharsets.ISO_8859_1); + assertThat(result).isEqualTo("café https://localhost:12345 done"); + } + + @Test + public void responseContentWithNonAsciiCharactersIsPreservedWhenCharsetIsIso88591() { + this.preprocessor.scheme("https"); + String original = "café http://localhost:12345 done"; + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.parseMediaType("text/plain;charset=ISO-8859-1")); + OperationResponse response = this.responseFactory.create(HttpStatus.OK, headers, + original.getBytes(StandardCharsets.ISO_8859_1)); + OperationResponse processed = this.preprocessor.preprocess(response); + String result = new String(processed.getContent(), StandardCharsets.ISO_8859_1); + assertThat(result).isEqualTo("café https://localhost:12345 done"); + } + @Test public void resultingRequestHasCookiesFromOriginalRequst() { List cookies = Arrays.asList(new RequestCookie("a", "alpha"));