@ -16,7 +16,9 @@
package org.springframework.test.web.servlet.result ;
package org.springframework.test.web.servlet.result ;
import java.util.Arrays ;
import java.util.Enumeration ;
import java.util.Enumeration ;
import java.util.List ;
import java.util.Map ;
import java.util.Map ;
import javax.servlet.http.Cookie ;
import javax.servlet.http.Cookie ;
@ -29,6 +31,8 @@ import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MvcResult ;
import org.springframework.test.web.servlet.MvcResult ;
import org.springframework.test.web.servlet.ResultHandler ;
import org.springframework.test.web.servlet.ResultHandler ;
import org.springframework.util.LinkedMultiValueMap ;
import org.springframework.util.LinkedMultiValueMap ;
import org.springframework.util.MimeType ;
import org.springframework.util.MimeTypeUtils ;
import org.springframework.util.MultiValueMap ;
import org.springframework.util.MultiValueMap ;
import org.springframework.util.ObjectUtils ;
import org.springframework.util.ObjectUtils ;
import org.springframework.validation.BindingResult ;
import org.springframework.validation.BindingResult ;
@ -54,6 +58,14 @@ import org.springframework.web.servlet.support.RequestContextUtils;
* /
* /
public class PrintingResultHandler implements ResultHandler {
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 final ResultValuePrinter printer ;
private final ResultValuePrinter printer ;
@ -103,11 +115,14 @@ public class PrintingResultHandler implements ResultHandler {
* Print the request .
* Print the request .
* /
* /
protected void printRequest ( MockHttpServletRequest request ) throws Exception {
protected void printRequest ( MockHttpServletRequest request ) throws Exception {
String body = ( isPrintableContentType ( request . getContentType ( ) ) ?
request . getContentAsString ( ) : NOT_PRINTABLE ) ;
this . printer . printValue ( "HTTP Method" , request . getMethod ( ) ) ;
this . printer . printValue ( "HTTP Method" , request . getMethod ( ) ) ;
this . printer . printValue ( "Request URI" , request . getRequestURI ( ) ) ;
this . printer . printValue ( "Request URI" , request . getRequestURI ( ) ) ;
this . printer . printValue ( "Parameters" , getParamsMultiValueMap ( request ) ) ;
this . printer . printValue ( "Parameters" , getParamsMultiValueMap ( request ) ) ;
this . printer . printValue ( "Headers" , getRequestHeaders ( request ) ) ;
this . printer . printValue ( "Headers" , getRequestHeaders ( request ) ) ;
this . printer . printValue ( "Body" , request . getContentAsString ( ) ) ;
this . printer . printValue ( "Body" , body ) ;
}
}
protected final HttpHeaders getRequestHeaders ( MockHttpServletRequest request ) {
protected final HttpHeaders getRequestHeaders ( MockHttpServletRequest request ) {
@ -223,11 +238,14 @@ public class PrintingResultHandler implements ResultHandler {
* Print the response .
* Print the response .
* /
* /
protected void printResponse ( MockHttpServletResponse response ) throws Exception {
protected void printResponse ( MockHttpServletResponse response ) throws Exception {
String body = ( isPrintableContentType ( response . getContentType ( ) ) ?
response . getContentAsString ( ) : NOT_PRINTABLE ) ;
this . printer . printValue ( "Status" , response . getStatus ( ) ) ;
this . printer . printValue ( "Status" , response . getStatus ( ) ) ;
this . printer . printValue ( "Error message" , response . getErrorMessage ( ) ) ;
this . printer . printValue ( "Error message" , response . getErrorMessage ( ) ) ;
this . printer . printValue ( "Headers" , getResponseHeaders ( response ) ) ;
this . printer . printValue ( "Headers" , getResponseHeaders ( response ) ) ;
this . printer . printValue ( "Content type" , response . getContentType ( ) ) ;
this . printer . printValue ( "Content type" , response . getContentType ( ) ) ;
this . printer . printValue ( "Body" , response . getContentAsString ( ) ) ;
this . printer . printValue ( "Body" , body ) ;
this . printer . printValue ( "Forwarded URL" , response . getForwardedUrl ( ) ) ;
this . printer . printValue ( "Forwarded URL" , response . getForwardedUrl ( ) ) ;
this . printer . printValue ( "Redirected URL" , response . getRedirectedUrl ( ) ) ;
this . printer . printValue ( "Redirected URL" , response . getRedirectedUrl ( ) ) ;
printCookies ( response . getCookies ( ) ) ;
printCookies ( response . getCookies ( ) ) ;
@ -266,6 +284,23 @@ 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 .
* A contract for how to actually write result information .
* /
* /