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:
Sam Brannen
2016-10-12 21:32:37 +02:00
parent 69116c2acc
commit 8cf1933148
5 changed files with 71 additions and 79 deletions

View File

@@ -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