From bd31fcfde2c23bfb77612fe990b0110e2c1f8ed9 Mon Sep 17 00:00:00 2001 From: izeye Date: Fri, 24 Dec 2021 08:57:13 +0900 Subject: [PATCH] Use UTF-8 for application/json in MockHttpServletResponse --- .../mock/web/MockHttpServletResponse.java | 6 ++++++ .../mock/web/MockHttpServletResponseTests.java | 9 +++++++++ .../web/testfixture/servlet/MockHttpServletResponse.java | 6 ++++++ 3 files changed, 21 insertions(+) diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java index 397cec961678..bc08c5f83964 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java @@ -24,6 +24,7 @@ import java.io.UnsupportedEncodingException; import java.io.Writer; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -315,6 +316,11 @@ public void setContentType(@Nullable String contentType) { if (mediaType.getCharset() != null) { setExplicitCharacterEncoding(mediaType.getCharset().name()); } + else { + if (contentType.equals(MediaType.APPLICATION_JSON_VALUE)) { + this.characterEncoding = StandardCharsets.UTF_8.name(); + } + } } catch (Exception ex) { // Try to get charset value anyway diff --git a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java index bc4ebd8a9ea0..c291cecea2c8 100644 --- a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java +++ b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java @@ -30,6 +30,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import org.springframework.http.MediaType; import org.springframework.web.util.WebUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -360,6 +361,14 @@ void servletWriterAutoFlushedForString() throws IOException { assertThat(response.getContentAsString()).isEqualTo("X"); } + @Test + void getContentAsStringWhenContentTypeIsApplicationJsonShouldUseUtf8() throws IOException { + String content = "{\"value\": \"테스트\"}"; + response.setContentType(MediaType.APPLICATION_JSON_VALUE); + response.getWriter().write(content); + assertThat(response.getContentAsString()).isEqualTo(content); + } + @Test void sendRedirect() throws IOException { String redirectUrl = "/redirect"; diff --git a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java index af9773ce330c..abd22eee096a 100644 --- a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java +++ b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java @@ -24,6 +24,7 @@ import java.io.UnsupportedEncodingException; import java.io.Writer; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -315,6 +316,11 @@ public void setContentType(@Nullable String contentType) { if (mediaType.getCharset() != null) { setExplicitCharacterEncoding(mediaType.getCharset().name()); } + else { + if (contentType.equals(MediaType.APPLICATION_JSON_VALUE)) { + this.characterEncoding = StandardCharsets.UTF_8.name(); + } + } } catch (Exception ex) { // Try to get charset value anyway