Skip to content

Commit

Permalink
PrintingResultHandler defensively accesses session.getAttributeNames()
Browse files Browse the repository at this point in the history
Issue: SPR-16164
  • Loading branch information
jhoeller committed Nov 6, 2017
1 parent 899994e commit 4ec60f0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
Expand Up @@ -59,7 +59,6 @@ public class PrintingResultHandler implements ResultHandler {

private static final String MISSING_CHARACTER_ENCODING = "<no character encoding set>";


private final ResultValuePrinter printer;


Expand Down Expand Up @@ -148,9 +147,14 @@ protected final MultiValueMap<String, String> getParamsMultiValueMap(MockHttpSer

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));
if (session != null) {
Enumeration<String> attrNames = session.getAttributeNames();
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 {
Expand All @@ -169,7 +173,9 @@ protected void printAsyncResult(MvcResult result) throws Exception {
/**
* 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) {
this.printer.printValue("Type", null);
}
Expand Down
Expand Up @@ -22,8 +22,10 @@
import java.util.List;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;

import org.junit.Test;
import org.mockito.Mockito;

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
Expand All @@ -40,8 +42,7 @@
import org.springframework.web.servlet.FlashMap;
import org.springframework.web.servlet.ModelAndView;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

/**
* Unit tests for {@link PrintingResultHandler}.
Expand Down Expand Up @@ -93,6 +94,55 @@ public void printRequest() throws Exception {
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
@SuppressWarnings("deprecation")
public void printResponse() throws Exception {
Expand Down Expand Up @@ -325,6 +375,7 @@ public void printValue(String label, Object value) {
}
}


public void handle() {
}

Expand Down

0 comments on commit 4ec60f0

Please sign in to comment.