Print session attributes in PrintingResultHandler

Issue: SPR-15189
This commit is contained in:
Rossen Stoyanchev
2017-02-23 16:39:21 -05:00
parent 24dbfc8ace
commit aecdf738d3
2 changed files with 22 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,11 +16,13 @@
package org.springframework.test.web.servlet.result;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.core.style.ToStringCreator;
import org.springframework.http.HttpHeaders;
@@ -114,6 +116,7 @@ public class PrintingResultHandler implements ResultHandler {
this.printer.printValue("Parameters", getParamsMultiValueMap(request));
this.printer.printValue("Headers", getRequestHeaders(request));
this.printer.printValue("Body", body);
this.printer.printValue("Session Attrs", getSessionAttributes(request));
}
protected final HttpHeaders getRequestHeaders(MockHttpServletRequest request) {
@@ -142,6 +145,13 @@ public class PrintingResultHandler implements ResultHandler {
return multiValueMap;
}
protected final Map<String, Object> getSessionAttributes(MockHttpServletRequest request) {
HttpSession session = request.getSession(false);
return session == null ? Collections.emptyMap() :
Collections.list(session.getAttributeNames()).stream()
.collect(Collectors.toMap(n -> n, session::getAttribute));
}
protected void printAsyncResult(MvcResult result) throws Exception {
HttpServletRequest request = result.getRequest();
this.printer.printValue("Async started", request.isAsyncStarted());