Skip to content

Commit bd31fcf

Browse files
committed
Use UTF-8 for application/json in MockHttpServletResponse
1 parent 544e9bb commit bd31fcf

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.UnsupportedEncodingException;
2525
import java.io.Writer;
2626
import java.nio.charset.Charset;
27+
import java.nio.charset.StandardCharsets;
2728
import java.text.DateFormat;
2829
import java.text.ParseException;
2930
import java.text.SimpleDateFormat;
@@ -315,6 +316,11 @@ public void setContentType(@Nullable String contentType) {
315316
if (mediaType.getCharset() != null) {
316317
setExplicitCharacterEncoding(mediaType.getCharset().name());
317318
}
319+
else {
320+
if (contentType.equals(MediaType.APPLICATION_JSON_VALUE)) {
321+
this.characterEncoding = StandardCharsets.UTF_8.name();
322+
}
323+
}
318324
}
319325
catch (Exception ex) {
320326
// Try to get charset value anyway

spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java

+9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.junit.jupiter.params.ParameterizedTest;
3131
import org.junit.jupiter.params.provider.ValueSource;
3232

33+
import org.springframework.http.MediaType;
3334
import org.springframework.web.util.WebUtils;
3435

3536
import static org.assertj.core.api.Assertions.assertThat;
@@ -360,6 +361,14 @@ void servletWriterAutoFlushedForString() throws IOException {
360361
assertThat(response.getContentAsString()).isEqualTo("X");
361362
}
362363

364+
@Test
365+
void getContentAsStringWhenContentTypeIsApplicationJsonShouldUseUtf8() throws IOException {
366+
String content = "{\"value\": \"테스트\"}";
367+
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
368+
response.getWriter().write(content);
369+
assertThat(response.getContentAsString()).isEqualTo(content);
370+
}
371+
363372
@Test
364373
void sendRedirect() throws IOException {
365374
String redirectUrl = "/redirect";

spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.UnsupportedEncodingException;
2525
import java.io.Writer;
2626
import java.nio.charset.Charset;
27+
import java.nio.charset.StandardCharsets;
2728
import java.text.DateFormat;
2829
import java.text.ParseException;
2930
import java.text.SimpleDateFormat;
@@ -315,6 +316,11 @@ public void setContentType(@Nullable String contentType) {
315316
if (mediaType.getCharset() != null) {
316317
setExplicitCharacterEncoding(mediaType.getCharset().name());
317318
}
319+
else {
320+
if (contentType.equals(MediaType.APPLICATION_JSON_VALUE)) {
321+
this.characterEncoding = StandardCharsets.UTF_8.name();
322+
}
323+
}
318324
}
319325
catch (Exception ex) {
320326
// Try to get charset value anyway

0 commit comments

Comments
 (0)