Skip to content

Commit 148d7ab

Browse files
committed
Merge branch '5.3.x'
2 parents 4b68dfb + 5fb58e5 commit 148d7ab

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;
@@ -248,7 +250,9 @@ protected void printResponse(MockHttpServletResponse response) throws Exception
248250
this.printer.printValue("Error message", response.getErrorMessage());
249251
this.printer.printValue("Headers", getResponseHeaders(response));
250252
this.printer.printValue("Content type", response.getContentType());
251-
this.printer.printValue("Body", response.getContentAsString());
253+
String body = (MediaType.APPLICATION_JSON_VALUE.equals(response.getContentType()) ?
254+
response.getContentAsString(StandardCharsets.UTF_8) : response.getContentAsString());
255+
this.printer.printValue("Body", body);
252256
this.printer.printValue("Forwarded URL", response.getForwardedUrl());
253257
this.printer.printValue("Redirected URL", response.getRedirectedUrl());
254258
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.
@@ -22,10 +22,12 @@
2222
import jakarta.servlet.http.HttpServletResponse;
2323
import org.junit.jupiter.api.Test;
2424

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

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

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

96119
}

0 commit comments

Comments
 (0)