PrintingResultHandler defensively accesses session.getAttributeNames()
Issue: SPR-16164
This commit is contained in:
parent
899994e7c1
commit
4ec60f08ad
|
|
@ -59,7 +59,6 @@ public class PrintingResultHandler implements ResultHandler {
|
|||
|
||||
private static final String MISSING_CHARACTER_ENCODING = "<no character encoding set>";
|
||||
|
||||
|
||||
private final ResultValuePrinter printer;
|
||||
|
||||
|
||||
|
|
@ -148,9 +147,14 @@ public class PrintingResultHandler implements ResultHandler {
|
|||
|
||||
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 {
|
||||
|
|
@ -169,7 +173,9 @@ public class PrintingResultHandler implements ResultHandler {
|
|||
/**
|
||||
* 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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,8 +22,10 @@ import java.util.HashMap;
|
|||
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;
|
||||
|
|
@ -40,8 +42,7 @@ import org.springframework.web.servlet.DispatcherServlet;
|
|||
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}.
|
||||
|
|
@ -93,6 +94,55 @@ public class PrintingResultHandlerTests {
|
|||
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 {
|
||||
|
|
@ -325,6 +375,7 @@ public class PrintingResultHandlerTests {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public void handle() {
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue