|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2021 the original author or authors. |
| 2 | + * Copyright 2002-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
22 | 22 | import jakarta.servlet.http.HttpServletResponse;
|
23 | 23 | import org.junit.jupiter.api.Test;
|
24 | 24 |
|
| 25 | +import org.springframework.http.MediaType; |
25 | 26 | import org.springframework.test.web.servlet.result.PrintingResultHandler;
|
26 | 27 | import org.springframework.web.bind.annotation.GetMapping;
|
27 | 28 | import org.springframework.web.bind.annotation.RestController;
|
28 | 29 |
|
| 30 | +import static java.nio.charset.StandardCharsets.UTF_8; |
29 | 31 | import static org.assertj.core.api.Assertions.assertThat;
|
30 | 32 | import static org.assertj.core.api.Assertions.fail;
|
31 | 33 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
@@ -60,6 +62,22 @@ void printMvcResultsToWriter() throws Exception {
|
60 | 62 | .contains("Headers = [Set-Cookie:\"enigma=42\", Content-Type:\"text/plain;charset=ISO-8859-1\", Content-Length:\"14\"]");
|
61 | 63 | }
|
62 | 64 |
|
| 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 | + |
63 | 81 | @Test
|
64 | 82 | void printMvcResultsToWriterWithFailingGlobalResultMatcher() throws Exception {
|
65 | 83 | StringWriter writer = new StringWriter();
|
@@ -91,6 +109,11 @@ String hello(HttpServletResponse response) {
|
91 | 109 | response.addCookie(new Cookie("enigma", "42"));
|
92 | 110 | return "Hello Response";
|
93 | 111 | }
|
| 112 | + |
| 113 | + @GetMapping("/utf8") |
| 114 | + String utf8(HttpServletResponse response) { |
| 115 | + return "Grüß dich!"; |
| 116 | + } |
94 | 117 | }
|
95 | 118 |
|
96 | 119 | }
|
0 commit comments