Skip to content

Commit

Permalink
Fixes issue #1247 - Copy attributes from the session manager to the s…
Browse files Browse the repository at this point in the history
…ession cookie (#1248)
  • Loading branch information
Thihup authored Nov 3, 2020
1 parent 8130c84 commit 818e36f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
5 changes: 3 additions & 2 deletions test/snoop/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<groupId>cloud.piranha.servlet</groupId>
<artifactId>piranha-servlet-api</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
5 changes: 3 additions & 2 deletions test/vaadin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<groupId>cloud.piranha.servlet</groupId>
<artifactId>piranha-servlet-api</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public DefaultHttpSessionManager() {
name = "JSESSIONID";
sessionListeners = new ArrayList<>(1);
sessionTimeout = 10;
maxAge = -1;
sessions = new ConcurrentHashMap<>();
}

Expand All @@ -162,6 +163,12 @@ public synchronized HttpSession createSession(HttpServletRequest request) {
cookie.setPath("".equals(webApplication.getContextPath()) ? "/" : webApplication.getContextPath());
}

cookie.setComment(comment);
cookie.setDomain(domain);
cookie.setHttpOnly(httpOnly);
cookie.setMaxAge(maxAge);
cookie.setSecure(secure);

response.addCookie(cookie);

sessionListeners.stream().forEach(sessionListener -> sessionListener.sessionCreated(new HttpSessionEvent(session)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package cloud.piranha.webapp.impl;

import javax.servlet.SessionTrackingMode;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -83,7 +84,7 @@ void testGetMaxAge() {
DefaultWebApplication webApplication = new DefaultWebApplication();
DefaultHttpSessionManager sessionManager = new DefaultHttpSessionManager();
sessionManager.setWebApplication(webApplication);
assertEquals(0, sessionManager.getMaxAge());
assertEquals(-1, sessionManager.getMaxAge());
sessionManager.setMaxAge(60);
assertEquals(60, sessionManager.getMaxAge());
}
Expand Down Expand Up @@ -218,4 +219,34 @@ void testSetSSLTrackingModeWithOtherMethod() {
EnumSet<SessionTrackingMode> sslAndCookie = EnumSet.of(SessionTrackingMode.COOKIE, SessionTrackingMode.SSL);
assertThrows(IllegalArgumentException.class, () -> sessionManager.setSessionTrackingModes(sslAndCookie));
}

@Test
void testSetCookieAttributes() {
DefaultWebApplication webApp = new DefaultWebApplication();
DefaultHttpSessionManager sessionManager = new DefaultHttpSessionManager();
sessionManager.setWebApplication(webApp);
TestWebApplicationRequest request = new TestWebApplicationRequest();
TestWebApplicationResponse response = new TestWebApplicationResponse();
webApp.linkRequestAndResponse(request, response);

sessionManager.setComment("Comment");
sessionManager.setDomain("SessionCookie");
sessionManager.setHttpOnly(true);
sessionManager.setName("SessionCookie");
sessionManager.setMaxAge(100);
sessionManager.setPath("/context");
sessionManager.setSecure(true);

sessionManager.createSession(request);

Cookie sessionCookie = response.getCookies().stream().filter(cookie -> "SessionCookie".equals(cookie.getName())).findFirst().orElse(null);
assertNotNull(sessionCookie);

assertEquals(sessionManager.getComment(), sessionCookie.getComment());
assertEquals(sessionManager.getDomain(), sessionCookie.getDomain());
assertTrue(sessionCookie.isHttpOnly());
assertEquals(sessionManager.getMaxAge(), sessionCookie.getMaxAge());
assertEquals(sessionManager.getPath(), sessionCookie.getPath());
assertTrue(sessionCookie.getSecure());
}
}

0 comments on commit 818e36f

Please sign in to comment.