Skip to content

Commit 5fb58e5

Browse files
committed
Infer UTF-8 encoding for JSON response with MockMvc's andDo(print(...))
Prior to this commit, the PrintingResultHandler in MockMvc -- typically invoked via .andDo(print()) -- printed an `application/json` response body using the default encoding (ISO-8859-1), which resulted in UTF-8 characters being garbled. Since an `application/json` response is implicitly encoded using UTF-8, the PrintingResultHandler now infers UTF-8 encoding for such response bodies. Closes gh-27926
1 parent 67c4b41 commit 5fb58e5

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 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,6 +16,7 @@
1616

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

19+
import java.nio.charset.StandardCharsets;
1920
import java.util.Collections;
2021
import java.util.Enumeration;
2122
import java.util.Map;
@@ -27,6 +28,7 @@
2728

2829
import org.springframework.core.style.ToStringCreator;
2930
import org.springframework.http.HttpHeaders;
31+
import org.springframework.http.MediaType;
3032
import org.springframework.lang.Nullable;
3133
import org.springframework.mock.web.MockHttpServletRequest;
3234
import org.springframework.mock.web.MockHttpServletResponse;
@@ -249,7 +251,9 @@ protected void printResponse(MockHttpServletResponse response) throws Exception
249251
this.printer.printValue("Error message", response.getErrorMessage());
250252
this.printer.printValue("Headers", getResponseHeaders(response));
251253
this.printer.printValue("Content type", response.getContentType());
252-
this.printer.printValue("Body", response.getContentAsString());
254+
String body = (MediaType.APPLICATION_JSON_VALUE.equals(response.getContentType()) ?
255+
response.getContentAsString(StandardCharsets.UTF_8) : response.getContentAsString());
256+
this.printer.printValue("Body", body);
253257
this.printer.printValue("Forwarded URL", response.getForwardedUrl());
254258
this.printer.printValue("Redirected URL", response.getRedirectedUrl());
255259
printCookies(response.getCookies());

spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resulthandlers/PrintingResultHandlerIntegrationTests.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -23,10 +23,12 @@
2323

2424
import org.junit.jupiter.api.Test;
2525

26+
import org.springframework.http.MediaType;
2627
import org.springframework.test.web.servlet.result.PrintingResultHandler;
2728
import org.springframework.web.bind.annotation.GetMapping;
2829
import org.springframework.web.bind.annotation.RestController;
2930

31+
import static java.nio.charset.StandardCharsets.UTF_8;
3032
import static org.assertj.core.api.Assertions.assertThat;
3133
import static org.assertj.core.api.Assertions.fail;
3234
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@@ -61,6 +63,22 @@ void printMvcResultsToWriter() throws Exception {
6163
.contains("Headers = [Set-Cookie:\"enigma=42\", Content-Type:\"text/plain;charset=ISO-8859-1\", Content-Length:\"14\"]");
6264
}
6365

66+
@Test
67+
void printMvcResultsToWriterWithJsonResponseBodyInterpretedAsUtf8() throws Exception {
68+
StringWriter writer = new StringWriter();
69+
70+
standaloneSetup(new SimpleController()).build()
71+
// "Hallöchen" is German slang for "hello".
72+
.perform(get("/utf8").accept(MediaType.APPLICATION_JSON).content("Hallöchen, Welt!".getBytes()).characterEncoding(UTF_8))
73+
.andDo(print(writer))
74+
// "Grüß dich!" is German for "greetings to you".
75+
.andExpect(content().bytes("Grüß dich!".getBytes()));
76+
77+
assertThat(writer).asString()
78+
.contains("Body = Hallöchen, Welt!")
79+
.contains("Body = Grüß dich!");
80+
}
81+
6482
@Test
6583
void printMvcResultsToWriterWithFailingGlobalResultMatcher() throws Exception {
6684
StringWriter writer = new StringWriter();
@@ -92,6 +110,11 @@ String hello(HttpServletResponse response) {
92110
response.addCookie(new Cookie("enigma", "42"));
93111
return "Hello Response";
94112
}
113+
114+
@GetMapping("/utf8")
115+
String utf8(HttpServletResponse response) {
116+
return "Grüß dich!";
117+
}
95118
}
96119

97120
}

0 commit comments

Comments
 (0)