PrintingResultHandler defensively accesses session.getAttributeNames()

Issue: SPR-16164
This commit is contained in:
Juergen Hoeller
2017-11-06 21:19:44 +01:00
parent 899994e7c1
commit 4ec60f08ad
2 changed files with 64 additions and 7 deletions

View File

@@ -59,7 +59,6 @@ public class PrintingResultHandler implements ResultHandler {
private static final String MISSING_CHARACTER_ENCODING = "<no character encoding set>"; private static final String MISSING_CHARACTER_ENCODING = "<no character encoding set>";
private final ResultValuePrinter printer; private final ResultValuePrinter printer;
@@ -148,9 +147,14 @@ public class PrintingResultHandler implements ResultHandler {
protected final Map<String, Object> getSessionAttributes(MockHttpServletRequest request) { protected final Map<String, Object> getSessionAttributes(MockHttpServletRequest request) {
HttpSession session = request.getSession(false); HttpSession session = request.getSession(false);
return session == null ? Collections.emptyMap() : if (session != null) {
Collections.list(session.getAttributeNames()).stream() Enumeration<String> attrNames = session.getAttributeNames();
.collect(Collectors.toMap(n -> n, session::getAttribute)); if (attrNames != null) {
return Collections.list(attrNames).stream().
collect(Collectors.toMap(n -> n, session::getAttribute));
}
}
return Collections.emptyMap();
} }
protected void printAsyncResult(MvcResult result) throws Exception { protected void printAsyncResult(MvcResult result) throws Exception {
@@ -169,7 +173,9 @@ public class PrintingResultHandler implements ResultHandler {
/** /**
* Print the handler. * Print the handler.
*/ */
protected void printHandler(@Nullable Object handler, @Nullable HandlerInterceptor[] interceptors) throws Exception { protected void printHandler(@Nullable Object handler, @Nullable HandlerInterceptor[] interceptors)
throws Exception {
if (handler == null) { if (handler == null) {
this.printer.printValue("Type", null); this.printer.printValue("Type", null);
} }

View File

@@ -22,8 +22,10 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@@ -40,8 +42,7 @@ import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.FlashMap; import org.springframework.web.servlet.FlashMap;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
/** /**
* Unit tests for {@link PrintingResultHandler}. * Unit tests for {@link PrintingResultHandler}.
@@ -93,6 +94,55 @@ public class PrintingResultHandlerTests {
assertValue("MockHttpServletRequest", "Session Attrs", Collections.singletonMap("foo", "bar")); assertValue("MockHttpServletRequest", "Session Attrs", Collections.singletonMap("foo", "bar"));
} }
@Test
public void printRequestWithoutSession() throws Exception {
this.request.addParameter("param", "paramValue");
this.request.addHeader("header", "headerValue");
this.request.setCharacterEncoding("UTF-16");
String palindrome = "ablE was I ere I saw Elba";
byte[] bytes = palindrome.getBytes("UTF-16");
this.request.setContent(bytes);
this.handler.handle(this.mvcResult);
HttpHeaders headers = new HttpHeaders();
headers.set("header", "headerValue");
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("param", "paramValue");
assertValue("MockHttpServletRequest", "HTTP Method", this.request.getMethod());
assertValue("MockHttpServletRequest", "Request URI", this.request.getRequestURI());
assertValue("MockHttpServletRequest", "Parameters", params);
assertValue("MockHttpServletRequest", "Headers", headers);
assertValue("MockHttpServletRequest", "Body", palindrome);
}
@Test
public void printRequestWithEmptySessionMock() throws Exception {
this.request.addParameter("param", "paramValue");
this.request.addHeader("header", "headerValue");
this.request.setCharacterEncoding("UTF-16");
String palindrome = "ablE was I ere I saw Elba";
byte[] bytes = palindrome.getBytes("UTF-16");
this.request.setContent(bytes);
this.request.setSession(Mockito.mock(HttpSession.class));
this.handler.handle(this.mvcResult);
HttpHeaders headers = new HttpHeaders();
headers.set("header", "headerValue");
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("param", "paramValue");
assertValue("MockHttpServletRequest", "HTTP Method", this.request.getMethod());
assertValue("MockHttpServletRequest", "Request URI", this.request.getRequestURI());
assertValue("MockHttpServletRequest", "Parameters", params);
assertValue("MockHttpServletRequest", "Headers", headers);
assertValue("MockHttpServletRequest", "Body", palindrome);
}
@Test @Test
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public void printResponse() throws Exception { public void printResponse() throws Exception {
@@ -325,6 +375,7 @@ public class PrintingResultHandlerTests {
} }
} }
public void handle() { public void handle() {
} }