|
16 | 16 |
|
17 | 17 | package org.springframework.test.web.servlet.result;
|
18 | 18 |
|
| 19 | +import java.util.Arrays; |
19 | 20 | import java.util.Enumeration;
|
| 21 | +import java.util.List; |
20 | 22 | import java.util.Map;
|
21 | 23 |
|
22 | 24 | import javax.servlet.http.Cookie;
|
|
29 | 31 | import org.springframework.test.web.servlet.MvcResult;
|
30 | 32 | import org.springframework.test.web.servlet.ResultHandler;
|
31 | 33 | import org.springframework.util.LinkedMultiValueMap;
|
| 34 | +import org.springframework.util.MimeType; |
| 35 | +import org.springframework.util.MimeTypeUtils; |
32 | 36 | import org.springframework.util.MultiValueMap;
|
33 | 37 | import org.springframework.util.ObjectUtils;
|
34 | 38 | import org.springframework.validation.BindingResult;
|
|
54 | 58 | */
|
55 | 59 | public class PrintingResultHandler implements ResultHandler {
|
56 | 60 |
|
| 61 | + private static final String NOT_PRINTABLE = "<content type is not printable text>"; |
| 62 | + |
| 63 | + private static final List<MimeType> printableMimeTypes = Arrays.asList( |
| 64 | + MimeTypeUtils.APPLICATION_JSON, MimeTypeUtils.APPLICATION_XML, |
| 65 | + new MimeType("text", "*"), new MimeType("application", "*+json"), |
| 66 | + new MimeType("application", "*+xml")); |
| 67 | + |
| 68 | + |
57 | 69 | private final ResultValuePrinter printer;
|
58 | 70 |
|
59 | 71 |
|
@@ -103,11 +115,14 @@ public final void handle(MvcResult result) throws Exception {
|
103 | 115 | * Print the request.
|
104 | 116 | */
|
105 | 117 | protected void printRequest(MockHttpServletRequest request) throws Exception {
|
| 118 | + String body = (isPrintableContentType(request.getContentType()) ? |
| 119 | + request.getContentAsString() : NOT_PRINTABLE); |
| 120 | + |
106 | 121 | this.printer.printValue("HTTP Method", request.getMethod());
|
107 | 122 | this.printer.printValue("Request URI", request.getRequestURI());
|
108 | 123 | this.printer.printValue("Parameters", getParamsMultiValueMap(request));
|
109 | 124 | this.printer.printValue("Headers", getRequestHeaders(request));
|
110 |
| - this.printer.printValue("Body", request.getContentAsString()); |
| 125 | + this.printer.printValue("Body", body); |
111 | 126 | }
|
112 | 127 |
|
113 | 128 | protected final HttpHeaders getRequestHeaders(MockHttpServletRequest request) {
|
@@ -223,11 +238,14 @@ protected void printFlashMap(FlashMap flashMap) throws Exception {
|
223 | 238 | * Print the response.
|
224 | 239 | */
|
225 | 240 | protected void printResponse(MockHttpServletResponse response) throws Exception {
|
| 241 | + String body = (isPrintableContentType(response.getContentType()) ? |
| 242 | + response.getContentAsString() : NOT_PRINTABLE); |
| 243 | + |
226 | 244 | this.printer.printValue("Status", response.getStatus());
|
227 | 245 | this.printer.printValue("Error message", response.getErrorMessage());
|
228 | 246 | this.printer.printValue("Headers", getResponseHeaders(response));
|
229 | 247 | this.printer.printValue("Content type", response.getContentType());
|
230 |
| - this.printer.printValue("Body", response.getContentAsString()); |
| 248 | + this.printer.printValue("Body", body); |
231 | 249 | this.printer.printValue("Forwarded URL", response.getForwardedUrl());
|
232 | 250 | this.printer.printValue("Redirected URL", response.getRedirectedUrl());
|
233 | 251 | printCookies(response.getCookies());
|
@@ -266,6 +284,23 @@ protected final HttpHeaders getResponseHeaders(MockHttpServletResponse response)
|
266 | 284 | }
|
267 | 285 |
|
268 | 286 |
|
| 287 | + /** |
| 288 | + * Determine if the supplied content type is <em>printable</em> (i.e., text-based). |
| 289 | + * <p>If the supplied content type is {@code null} (i.e., unknown), this method |
| 290 | + * assumes that the content is printable by default and returns {@code true}. |
| 291 | + * @param contentType the content type to check; {@code null} if unknown |
| 292 | + * @return {@code true} if the content type is known to be or assumed to be printable |
| 293 | + * @since 5.0 |
| 294 | + */ |
| 295 | + private static boolean isPrintableContentType(String contentType) { |
| 296 | + if (contentType == null) { |
| 297 | + return true; |
| 298 | + } |
| 299 | + MimeType mimeType = MimeType.valueOf(contentType); |
| 300 | + return printableMimeTypes.stream().anyMatch(printable -> printable.includes(mimeType)); |
| 301 | + } |
| 302 | + |
| 303 | + |
269 | 304 | /**
|
270 | 305 | * A contract for how to actually write result information.
|
271 | 306 | */
|
|
0 commit comments