Print only printable request/response body in MVC Test

Prior to this commit, PrintingResultHandler always printed the request
or response body regardless of its content type. For binary content,
however, the output was unreadable and therefore useless.

This commit addresses this issue by only printing the request or
response body if it is "printable" (i.e., if its content type is known
to be text-based such as plain text, HTML, XHTML, XML, JSON, etc.). If
the content type is unknown (e.g., unspecified for the HTTP request in
the test), it is assumed that the body is printable.

Issue: SPR-14776
This commit is contained in:
Sam Brannen
2016-10-11 19:03:58 +02:00
parent 7ae729480e
commit e65a1a4372
2 changed files with 85 additions and 3 deletions

View File

@@ -17,7 +17,9 @@
package org.springframework.test.web.servlet.result;
import java.net.URI;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.Cookie;
@@ -31,6 +33,7 @@ import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.StubMvcResult;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
@@ -50,6 +53,11 @@ import static org.junit.Assert.*;
*/
public class PrintingResultHandlerTests {
private static final List<String> textContentTypes = Arrays.asList(MimeTypeUtils.APPLICATION_JSON_VALUE,
MimeTypeUtils.APPLICATION_XML_VALUE, MimeTypeUtils.APPLICATION_XHTML_XML_VALUE,
MimeTypeUtils.TEXT_HTML_VALUE, MimeTypeUtils.TEXT_PLAIN_VALUE);
private final TestPrintingResultHandler handler = new TestPrintingResultHandler();
private final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/") {
@@ -82,7 +90,6 @@ public class PrintingResultHandlerTests {
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("param", "paramValue");
assertValue("MockHttpServletRequest", "HTTP Method", this.request.getMethod());
assertValue("MockHttpServletRequest", "Request URI", this.request.getRequestURI());
assertValue("MockHttpServletRequest", "Parameters", params);
@@ -139,6 +146,46 @@ public class PrintingResultHandlerTests {
assertTrue(cookie2.endsWith("]"));
}
@Test
public void printRequestWithTextContentTypes() throws Exception {
this.request.setContent("text".getBytes());
for (String contentType: textContentTypes) {
this.request.setContentType(contentType);
this.handler.handle(this.mvcResult);
assertValue("MockHttpServletRequest", "Body", "text");
}
}
@Test
public void printResponseWithTextContentTypes() throws Exception {
this.response.getWriter().print("text");
for (String contentType: textContentTypes) {
this.response.setContentType(contentType);
this.handler.handle(this.mvcResult);
assertValue("MockHttpServletResponse", "Body", "text");
}
}
@Test
public void printRequestWithBinaryContentType() throws Exception {
this.request.setContentType(MimeTypeUtils.IMAGE_JPEG_VALUE);
this.handler.handle(this.mvcResult);
assertValue("MockHttpServletRequest", "Body", "<content type is not printable text>");
}
@Test
public void printResponseWithBinaryContentType() throws Exception {
this.response.setContentType(MimeTypeUtils.IMAGE_JPEG_VALUE);
this.handler.handle(this.mvcResult);
assertValue("MockHttpServletResponse", "Body", "<content type is not printable text>");
}
@Test
public void printHandlerNull() throws Exception {
StubMvcResult mvcResult = new StubMvcResult(this.request, null, null, null, null, null, this.response);