Skip to content

Commit e7f0e1c

Browse files
committed
Use consistent charset for encoding and decoding in UriModifyingContentModifier
Fixes gh-1039 Signed-off-by: config25 <yhkim052556@naver.com>
1 parent 4a55491 commit e7f0e1c

2 files changed

Lines changed: 32 additions & 9 deletions

File tree

spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessor.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.restdocs.operation.preprocess;
1818

1919
import java.net.URI;
20+
import java.nio.charset.Charset;
2021
import java.util.ArrayList;
2122
import java.util.Arrays;
2223
import java.util.Collection;
@@ -191,15 +192,9 @@ private void setPort(String port) {
191192

192193
@Override
193194
public byte[] modifyContent(byte[] content, MediaType contentType) {
194-
String input;
195-
if (contentType != null && contentType.getCharset() != null) {
196-
input = new String(content, contentType.getCharset());
197-
}
198-
else {
199-
input = new String(content);
200-
}
201-
202-
return modify(input).getBytes();
195+
Charset charset = (contentType != null && contentType.getCharset() != null) ? contentType.getCharset()
196+
: Charset.defaultCharset();
197+
return modify(new String(content, charset)).getBytes(charset);
203198
}
204199

205200
private String modify(String input) {

spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessorTests.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.restdocs.operation.preprocess;
1818

1919
import java.net.URI;
20+
import java.nio.charset.StandardCharsets;
2021
import java.util.Arrays;
2122
import java.util.Collections;
2223
import java.util.List;
@@ -26,6 +27,7 @@
2627
import org.springframework.http.HttpHeaders;
2728
import org.springframework.http.HttpMethod;
2829
import org.springframework.http.HttpStatus;
30+
import org.springframework.http.MediaType;
2931
import org.springframework.restdocs.operation.OperationRequest;
3032
import org.springframework.restdocs.operation.OperationRequestFactory;
3133
import org.springframework.restdocs.operation.OperationRequestPart;
@@ -299,6 +301,32 @@ public void modifiedUriDoesNotGetDoubleEncoded() {
299301

300302
}
301303

304+
@Test
305+
public void requestContentWithNonAsciiCharactersIsPreservedWhenCharsetIsIso88591() {
306+
this.preprocessor.scheme("https");
307+
String original = "café http://localhost:12345 done";
308+
HttpHeaders headers = new HttpHeaders();
309+
headers.setContentType(MediaType.parseMediaType("text/plain;charset=ISO-8859-1"));
310+
OperationRequest request = this.requestFactory.create(URI.create("http://localhost"), HttpMethod.GET,
311+
original.getBytes(StandardCharsets.ISO_8859_1), headers, Collections.<OperationRequestPart>emptyList());
312+
OperationRequest processed = this.preprocessor.preprocess(request);
313+
String result = new String(processed.getContent(), StandardCharsets.ISO_8859_1);
314+
assertThat(result).isEqualTo("café https://localhost:12345 done");
315+
}
316+
317+
@Test
318+
public void responseContentWithNonAsciiCharactersIsPreservedWhenCharsetIsIso88591() {
319+
this.preprocessor.scheme("https");
320+
String original = "café http://localhost:12345 done";
321+
HttpHeaders headers = new HttpHeaders();
322+
headers.setContentType(MediaType.parseMediaType("text/plain;charset=ISO-8859-1"));
323+
OperationResponse response = this.responseFactory.create(HttpStatus.OK, headers,
324+
original.getBytes(StandardCharsets.ISO_8859_1));
325+
OperationResponse processed = this.preprocessor.preprocess(response);
326+
String result = new String(processed.getContent(), StandardCharsets.ISO_8859_1);
327+
assertThat(result).isEqualTo("café https://localhost:12345 done");
328+
}
329+
302330
@Test
303331
public void resultingRequestHasCookiesFromOriginalRequst() {
304332
List<RequestCookie> cookies = Arrays.asList(new RequestCookie("a", "alpha"));

0 commit comments

Comments
 (0)