Print cookies in human-readable form in Spring MVC Test
Prior to this commit, when rendering cookies via `andDo(print())` in
Spring MVC Test, the output for the `MockHttpServletResponse` would
look something like the following:
Cookies = [javax.servlet.http.Cookie@25084a1e]
The reason is that the Cookie class in javax.servlet-api-3.0.1.jar does
not implement toString(). Consequently, nothing about the cookie's
name, value, etc., is displayed, thereby making the debug output for
cookies next to useless.
This commit improves on this by implementing custom toString() logic
for cookies in debug output in Spring MVC Test. For example, the output
now looks like this (without the newlines):
Cookies = [[Cookie@47faa49c name = 'enigma', value = '42', \\
comment = [null], domain = [null], maxAge = -1, \\
path = [null], secure = false, version = 0, \\
httpOnly = false]]
In addition, this commit fixes a minor bug for FlashMap debug output if
the FlashMap is empty.
Issue: SPR-13168
This commit is contained in:
@@ -24,9 +24,10 @@ import org.springframework.util.CollectionUtils;
|
||||
* Static, factory methods for {@link ResultHandler}-based result actions.
|
||||
*
|
||||
* <p><strong>Eclipse users:</strong> consider adding this class as a Java editor
|
||||
* favorite. To navigate, open the Preferences and type "favorites".
|
||||
* favorite. To navigate to this setting, open the Preferences and type "favorites".
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Sam Brannen
|
||||
* @since 3.2
|
||||
*/
|
||||
public abstract class MockMvcResultHandlers {
|
||||
@@ -49,14 +50,14 @@ public abstract class MockMvcResultHandlers {
|
||||
@Override
|
||||
public void printHeading(String heading) {
|
||||
System.out.println();
|
||||
System.out.println(String.format("%20s:", heading));
|
||||
System.out.println(String.format("%s:", heading));
|
||||
}
|
||||
@Override
|
||||
public void printValue(String label, Object value) {
|
||||
if (value != null && value.getClass().isArray()) {
|
||||
value = CollectionUtils.arrayToList(value);
|
||||
}
|
||||
System.out.println(String.format("%20s = %s", label, value));
|
||||
System.out.println(String.format("%17s = %s", label, value));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -18,8 +18,11 @@ package org.springframework.test.web.servlet.result;
|
||||
|
||||
import java.util.Enumeration;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
@@ -27,6 +30,7 @@ import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.test.web.servlet.ResultHandler;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
@@ -37,10 +41,12 @@ import org.springframework.web.servlet.support.RequestContextUtils;
|
||||
|
||||
/**
|
||||
* Result handler that prints {@link MvcResult} details to the "standard" output
|
||||
* stream. An instance of this class is typically accessed via
|
||||
* stream.
|
||||
* <p>An instance of this class is typically accessed via
|
||||
* {@link MockMvcResultHandlers#print()}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Sam Brannen
|
||||
* @since 3.2
|
||||
*/
|
||||
public class PrintingResultHandler implements ResultHandler {
|
||||
@@ -57,7 +63,7 @@ public class PrintingResultHandler implements ResultHandler {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the result value printer.
|
||||
* @return the result value printer
|
||||
*/
|
||||
protected ResultValuePrinter getPrinter() {
|
||||
return this.printer;
|
||||
@@ -198,7 +204,7 @@ public class PrintingResultHandler implements ResultHandler {
|
||||
* Print "output" flash attributes.
|
||||
*/
|
||||
protected void printFlashMap(FlashMap flashMap) throws Exception {
|
||||
if (flashMap == null) {
|
||||
if (ObjectUtils.isEmpty(flashMap)) {
|
||||
this.printer.printValue("Attributes", null);
|
||||
}
|
||||
else {
|
||||
@@ -220,7 +226,31 @@ public class PrintingResultHandler implements ResultHandler {
|
||||
this.printer.printValue("Body", response.getContentAsString());
|
||||
this.printer.printValue("Forwarded URL", response.getForwardedUrl());
|
||||
this.printer.printValue("Redirected URL", response.getRedirectedUrl());
|
||||
this.printer.printValue("Cookies", response.getCookies());
|
||||
printCookies(response.getCookies());
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the supplied cookies in a human-readable form, assuming the
|
||||
* {@link Cookie} implementation does not provide its own {@code toString()}.
|
||||
* @since 4.2
|
||||
*/
|
||||
private void printCookies(Cookie[] cookies) {
|
||||
String[] cookieStrings = new String[cookies.length];
|
||||
for (int i = 0; i < cookies.length; i++) {
|
||||
Cookie cookie = cookies[i];
|
||||
cookieStrings[i] = new ToStringCreator(cookie)
|
||||
.append("name", cookie.getName())
|
||||
.append("value", cookie.getValue())
|
||||
.append("comment", cookie.getComment())
|
||||
.append("domain", cookie.getDomain())
|
||||
.append("maxAge", cookie.getMaxAge())
|
||||
.append("path", cookie.getPath())
|
||||
.append("secure", cookie.getSecure())
|
||||
.append("version", cookie.getVersion())
|
||||
.append("httpOnly", cookie.isHttpOnly())
|
||||
.toString();
|
||||
}
|
||||
this.printer.printValue("Cookies", cookieStrings);
|
||||
}
|
||||
|
||||
protected final HttpHeaders getResponseHeaders(MockHttpServletResponse response) {
|
||||
|
||||
Reference in New Issue
Block a user