Skip to content

Commit 1d363e5

Browse files
committed
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
1 parent 2116d71 commit 1d363e5

File tree

5 files changed

+21
-10
lines changed

5 files changed

+21
-10
lines changed

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

+7
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;
@@ -72,6 +73,8 @@ public class MockHttpServletResponse implements HttpServletResponse {
7273

7374
private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
7475

76+
private static final MediaType APPLICATION_PLUS_JSON = new MediaType("application", "*+json");
77+
7578

7679
//---------------------------------------------------------------------
7780
// ServletResponse properties
@@ -348,6 +351,10 @@ public void setContentType(@Nullable String contentType) {
348351
if (mediaType.getCharset() != null) {
349352
setExplicitCharacterEncoding(mediaType.getCharset().name());
350353
}
354+
else if (mediaType.isCompatibleWith(MediaType.APPLICATION_JSON) ||
355+
mediaType.isCompatibleWith(APPLICATION_PLUS_JSON)) {
356+
this.characterEncoding = StandardCharsets.UTF_8.name();
357+
}
351358
}
352359
catch (Exception ex) {
353360
// Try to get charset value anyway

spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -257,7 +257,7 @@ public ResultMatcher json(String jsonContent, JsonCompareMode compareMode) {
257257
*/
258258
public ResultMatcher json(String jsonContent, JsonComparator comparator) {
259259
return result -> {
260-
String content = result.getResponse().getContentAsString(StandardCharsets.UTF_8);
260+
String content = result.getResponse().getContentAsString();
261261
comparator.assertIsMatch(jsonContent, content);
262262
};
263263
}

spring-test/src/main/java/org/springframework/test/web/servlet/result/JsonPathResultMatchers.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.test.web.servlet.result;
1818

1919
import java.io.UnsupportedEncodingException;
20-
import java.nio.charset.StandardCharsets;
2120

2221
import com.jayway.jsonpath.JsonPath;
2322
import org.hamcrest.Matcher;
@@ -238,7 +237,7 @@ public ResultMatcher isMap() {
238237
}
239238

240239
private String getContent(MvcResult result) throws UnsupportedEncodingException {
241-
String content = result.getResponse().getContentAsString(StandardCharsets.UTF_8);
240+
String content = result.getResponse().getContentAsString();
242241
if (StringUtils.hasLength(this.prefix)) {
243242
try {
244243
String reason = String.format("Expected a JSON payload prefixed with \"%s\" but found: %s",

spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.test.web.servlet.result;
1818

19-
import java.nio.charset.StandardCharsets;
2019
import java.util.Collections;
2120
import java.util.Enumeration;
2221
import java.util.Map;
@@ -28,7 +27,6 @@
2827

2928
import org.springframework.core.style.ToStringCreator;
3029
import org.springframework.http.HttpHeaders;
31-
import org.springframework.http.MediaType;
3230
import org.springframework.lang.Nullable;
3331
import org.springframework.mock.web.MockHttpServletRequest;
3432
import org.springframework.mock.web.MockHttpServletResponse;
@@ -250,9 +248,7 @@ protected void printResponse(MockHttpServletResponse response) throws Exception
250248
this.printer.printValue("Error message", response.getErrorMessage());
251249
this.printer.printValue("Headers", getResponseHeaders(response));
252250
this.printer.printValue("Content type", response.getContentType());
253-
String body = (MediaType.APPLICATION_JSON_VALUE.equals(response.getContentType()) ?
254-
response.getContentAsString(StandardCharsets.UTF_8) : response.getContentAsString());
255-
this.printer.printValue("Body", body);
251+
this.printer.printValue("Body", response.getContentAsString());
256252
this.printer.printValue("Forwarded URL", response.getForwardedUrl());
257253
this.printer.printValue("Redirected URL", response.getRedirectedUrl());
258254
printCookies(response.getCookies());

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;
@@ -620,4 +621,12 @@ void resetResetsCharset() {
620621
assertThat(contentTypeHeader).isEqualTo("text/plain");
621622
}
622623

624+
@Test // gh-33019
625+
void contentAsStringEncodingWithJson() throws IOException {
626+
String content = "{\"name\": \"Jürgen\"}";
627+
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
628+
response.getWriter().write(content);
629+
assertThat(response.getContentAsString()).isEqualTo(content);
630+
}
631+
623632
}

0 commit comments

Comments
 (0)