Use UTF-8 with JSON in MockHttpServletResponse

This commit makes MockHttpServletResponse consistent with the other
parts of the framework where the body of a response is read as an UTF-8
String when the content type is application/json or similar, overriding
the ISO-8859-1 default Servlet encoding.

Closes gh-33019
This commit is contained in:
Sébastien Deleuze
2024-06-13 16:02:28 +02:00
parent 2116d71d81
commit 1d363e5a41
5 changed files with 21 additions and 10 deletions

View File

@@ -30,6 +30,7 @@ import org.junit.jupiter.api.Test;
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;
@@ -620,4 +621,12 @@ class MockHttpServletResponseTests {
assertThat(contentTypeHeader).isEqualTo("text/plain");
}
@Test // gh-33019
void contentAsStringEncodingWithJson() throws IOException {
String content = "{\"name\": \"Jürgen\"}";
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.getWriter().write(content);
assertThat(response.getContentAsString()).isEqualTo(content);
}
}