Browse Source

Print session attributes in PrintingResultHandler

Issue: SPR-15189
pull/1337/head
Rossen Stoyanchev 8 years ago
parent
commit
aecdf738d3
  1. 14
      spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java
  2. 15
      spring-test/src/test/java/org/springframework/test/web/servlet/result/PrintingResultHandlerTests.java

14
spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java

@ -1,5 +1,5 @@ @@ -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 @@ @@ -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 { @@ -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 { @@ -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());

15
spring-test/src/test/java/org/springframework/test/web/servlet/result/PrintingResultHandlerTests.java

@ -1,5 +1,5 @@ @@ -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.
@ -17,6 +17,7 @@ @@ -17,6 +17,7 @@
package org.springframework.test.web.servlet.result;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -61,8 +62,8 @@ public class PrintingResultHandlerTests { @@ -61,8 +62,8 @@ public class PrintingResultHandlerTests {
private final MockHttpServletResponse response = new MockHttpServletResponse();
private final StubMvcResult mvcResult = new StubMvcResult(this.request, null, null,
null, null, null, this.response);
private final StubMvcResult mvcResult = new StubMvcResult(
this.request, null, null, null, null, null, this.response);
@Test
@ -73,6 +74,7 @@ public class PrintingResultHandlerTests { @@ -73,6 +74,7 @@ public class PrintingResultHandlerTests {
String palindrome = "ablE was I ere I saw Elba";
byte[] bytes = palindrome.getBytes("UTF-16");
this.request.setContent(bytes);
this.request.getSession().setAttribute("foo", "bar");
this.handler.handle(this.mvcResult);
@ -87,6 +89,7 @@ public class PrintingResultHandlerTests { @@ -87,6 +89,7 @@ public class PrintingResultHandlerTests {
assertValue("MockHttpServletRequest", "Parameters", params);
assertValue("MockHttpServletRequest", "Headers", headers);
assertValue("MockHttpServletRequest", "Body", palindrome);
assertValue("MockHttpServletRequest", "Session Attrs", Collections.singletonMap("foo", "bar"));
}
@Test
@ -134,7 +137,9 @@ public class PrintingResultHandlerTests { @@ -134,7 +137,9 @@ public class PrintingResultHandlerTests {
assertTrue(cookie1.contains("name = 'cookie', value = 'cookieValue'"));
assertTrue(cookie1.endsWith("]"));
assertTrue(cookie2.startsWith("[" + Cookie.class.getSimpleName()));
assertTrue(cookie2.contains("name = 'enigma', value = '42', comment = 'This is a comment', domain = '.example.com', maxAge = 1234, path = '/crumbs', secure = true, version = 0, httpOnly = true"));
assertTrue(cookie2.contains("name = 'enigma', value = '42', " +
"comment = 'This is a comment', domain = '.example.com', maxAge = 1234, " +
"path = '/crumbs', secure = true, version = 0, httpOnly = true"));
assertTrue(cookie2.endsWith("]"));
}
@ -281,7 +286,7 @@ public class PrintingResultHandlerTests { @@ -281,7 +286,7 @@ public class PrintingResultHandlerTests {
private static class TestPrintingResultHandler extends PrintingResultHandler {
public TestPrintingResultHandler() {
TestPrintingResultHandler() {
super(new TestResultValuePrinter());
}

Loading…
Cancel
Save