|
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.
|
|
23 | 23 |
|
24 | 24 | import org.junit.jupiter.api.Test;
|
25 | 25 |
|
| 26 | +import org.springframework.http.MediaType; |
26 | 27 | import org.springframework.test.web.servlet.result.PrintingResultHandler;
|
27 | 28 | import org.springframework.web.bind.annotation.GetMapping;
|
28 | 29 | import org.springframework.web.bind.annotation.RestController;
|
29 | 30 |
|
| 31 | +import static java.nio.charset.StandardCharsets.UTF_8; |
30 | 32 | import static org.assertj.core.api.Assertions.assertThat;
|
31 | 33 | import static org.assertj.core.api.Assertions.fail;
|
32 | 34 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
@@ -61,6 +63,22 @@ void printMvcResultsToWriter() throws Exception {
|
61 | 63 | .contains("Headers = [Set-Cookie:\"enigma=42\", Content-Type:\"text/plain;charset=ISO-8859-1\", Content-Length:\"14\"]");
|
62 | 64 | }
|
63 | 65 |
|
| 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 | + |
64 | 82 | @Test
|
65 | 83 | void printMvcResultsToWriterWithFailingGlobalResultMatcher() throws Exception {
|
66 | 84 | StringWriter writer = new StringWriter();
|
@@ -92,6 +110,11 @@ String hello(HttpServletResponse response) {
|
92 | 110 | response.addCookie(new Cookie("enigma", "42"));
|
93 | 111 | return "Hello Response";
|
94 | 112 | }
|
| 113 | + |
| 114 | + @GetMapping("/utf8") |
| 115 | + String utf8(HttpServletResponse response) { |
| 116 | + return "Grüß dich!"; |
| 117 | + } |
95 | 118 | }
|
96 | 119 |
|
97 | 120 | }
|
0 commit comments