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

@@ -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() {
}