Only print request/response body if char enc is present in MVC Test
Commit e65a1a4372 introduced support in PrintingResultHandler for only
printing the request or response body in the Spring MVC Test framework
if the content type is known to be text-based (e.g., plain text, HTML,
XHTML, XML, JSON, etc.). For unknown content types the body is assumed
to be text-based and is therefore always printed. The latter behavior,
however, is undesirable since the content may in fact not be text-based.
This commit addresses this issue by making the printing of the request
or response body an opt-in feature. Specifically, if a character
encoding has been set, the request or response body will be printed by
the PrintingResultHandler. Note, however, that the character encoding
is set to ISO-8859-1 in MockHttpServletResponse by default.
In addition, MockHttpServletRequest's getContentAsString() method now
throws an IllegalStateException if the character encoding has not been
set.
Issue: SPR-14776
This commit is contained in:
@@ -373,6 +373,10 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
|
||||
/**
|
||||
* Set the content of the request body as a byte array.
|
||||
* <p>If the supplied byte array represents text such as XML or JSON, the
|
||||
* {@link #setCharacterEncoding character encoding} should typically be
|
||||
* set as well.
|
||||
* @see #setCharacterEncoding(String)
|
||||
* @see #getContentAsByteArray()
|
||||
* @see #getContentAsString()
|
||||
*/
|
||||
@@ -382,6 +386,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
|
||||
/**
|
||||
* Get the content of the request body as a byte array.
|
||||
* @return the content as a byte array, potentially {@code null}
|
||||
* @since 5.0
|
||||
* @see #setContent(byte[])
|
||||
* @see #getContentAsString()
|
||||
@@ -392,19 +397,24 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
|
||||
/**
|
||||
* Get the content of the request body as a {@code String}, using the configured
|
||||
* {@linkplain #getCharacterEncoding character encoding} if present.
|
||||
* {@linkplain #getCharacterEncoding character encoding}.
|
||||
* @return the content as a {@code String}, potentially {@code null}
|
||||
* @throws IllegalStateException if the character encoding has not been set
|
||||
* @throws UnsupportedEncodingException if the character encoding is not supported
|
||||
* @since 5.0
|
||||
* @see #setContent(byte[])
|
||||
* @see #getContentAsByteArray()
|
||||
* @see #setCharacterEncoding(String)
|
||||
* @see #getContentAsByteArray()
|
||||
*/
|
||||
public String getContentAsString() throws UnsupportedEncodingException {
|
||||
public String getContentAsString() throws IllegalStateException, UnsupportedEncodingException {
|
||||
Assert.state(this.characterEncoding != null,
|
||||
"Cannot get content as a String for a null character encoding. " +
|
||||
"Consider setting the characterEncoding in the request.");
|
||||
|
||||
if (this.content == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (this.characterEncoding != null ?
|
||||
new String(this.content, this.characterEncoding) : new String(this.content));
|
||||
return new String(this.content, this.characterEncoding);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -16,9 +16,7 @@
|
||||
|
||||
package org.springframework.test.web.servlet.result;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
@@ -31,8 +29,6 @@ import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.test.web.servlet.ResultHandler;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MimeType;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.validation.BindingResult;
|
||||
@@ -58,12 +54,7 @@ import org.springframework.web.servlet.support.RequestContextUtils;
|
||||
*/
|
||||
public class PrintingResultHandler implements ResultHandler {
|
||||
|
||||
private static final String NOT_PRINTABLE = "<content type is not printable text>";
|
||||
|
||||
private static final List<MimeType> printableMimeTypes = Arrays.asList(
|
||||
MimeTypeUtils.APPLICATION_JSON, MimeTypeUtils.APPLICATION_XML,
|
||||
new MimeType("text", "*"), new MimeType("application", "*+json"),
|
||||
new MimeType("application", "*+xml"));
|
||||
private static final String MISSING_CHARACTER_ENCODING = "<no character encoding set>";
|
||||
|
||||
|
||||
private final ResultValuePrinter printer;
|
||||
@@ -115,8 +106,8 @@ public class PrintingResultHandler implements ResultHandler {
|
||||
* Print the request.
|
||||
*/
|
||||
protected void printRequest(MockHttpServletRequest request) throws Exception {
|
||||
String body = (isPrintableContentType(request.getContentType()) ?
|
||||
request.getContentAsString() : NOT_PRINTABLE);
|
||||
String body = (request.getCharacterEncoding() != null ?
|
||||
request.getContentAsString() : MISSING_CHARACTER_ENCODING);
|
||||
|
||||
this.printer.printValue("HTTP Method", request.getMethod());
|
||||
this.printer.printValue("Request URI", request.getRequestURI());
|
||||
@@ -238,8 +229,8 @@ public class PrintingResultHandler implements ResultHandler {
|
||||
* Print the response.
|
||||
*/
|
||||
protected void printResponse(MockHttpServletResponse response) throws Exception {
|
||||
String body = (isPrintableContentType(response.getContentType()) ?
|
||||
response.getContentAsString() : NOT_PRINTABLE);
|
||||
String body = (response.getCharacterEncoding() != null ?
|
||||
response.getContentAsString() : MISSING_CHARACTER_ENCODING);
|
||||
|
||||
this.printer.printValue("Status", response.getStatus());
|
||||
this.printer.printValue("Error message", response.getErrorMessage());
|
||||
@@ -284,23 +275,6 @@ public class PrintingResultHandler implements ResultHandler {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine if the supplied content type is <em>printable</em> (i.e., text-based).
|
||||
* <p>If the supplied content type is {@code null} (i.e., unknown), this method
|
||||
* assumes that the content is printable by default and returns {@code true}.
|
||||
* @param contentType the content type to check; {@code null} if unknown
|
||||
* @return {@code true} if the content type is known to be or assumed to be printable
|
||||
* @since 5.0
|
||||
*/
|
||||
private static boolean isPrintableContentType(String contentType) {
|
||||
if (contentType == null) {
|
||||
return true;
|
||||
}
|
||||
MimeType mimeType = MimeType.valueOf(contentType);
|
||||
return printableMimeTypes.stream().anyMatch(printable -> printable.includes(mimeType));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A contract for how to actually write result information.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user