Preserve expires attribute in MockCookie

At present, MockCookie doesn't preserve expires attribute. This has a
consequence that a cookie value set using
MockHttpServletResponse#addHeader containing an expires attribute will
not match the cookie value obtained from
MockHttpServletResponse#getHeader, since the expires attribute will get
calculated based on current time.

This commit enhances MockCookie to preserve the expires attribute.

Closes gh-23769
This commit is contained in:
Vedran Pavic
2019-10-29 13:50:38 +01:00
committed by Sam Brannen
parent ceb881ab05
commit 3814f12b67
6 changed files with 87 additions and 11 deletions
@@ -20,6 +20,9 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import static org.junit.Assert.*;
/**
@@ -67,8 +70,8 @@ public class MockCookieTests {
@Test
public void parseHeaderWithAttributes() {
MockCookie cookie = MockCookie.parse(
"SESSION=123; Domain=example.com; Max-Age=60; Path=/; Secure; HttpOnly; SameSite=Lax");
MockCookie cookie = MockCookie.parse("SESSION=123; Domain=example.com; Max-Age=60; " +
"Expires=Tue, 8 Oct 2019 19:50:00 GMT; Path=/; Secure; HttpOnly; SameSite=Lax");
assertCookie(cookie, "SESSION", "123");
assertEquals("example.com", cookie.getDomain());
@@ -76,6 +79,8 @@ public class MockCookieTests {
assertEquals("/", cookie.getPath());
assertTrue(cookie.getSecure());
assertTrue(cookie.isHttpOnly());
assertEquals(ZonedDateTime.parse("Tue, 8 Oct 2019 19:50:00 GMT",
DateTimeFormatter.RFC_1123_DATE_TIME), cookie.getExpires());
assertEquals("Lax", cookie.getSameSite());
}
@@ -109,15 +114,17 @@ public class MockCookieTests {
@Test
public void parseHeaderWithAttributesCaseSensitivity() {
MockCookie cookie = MockCookie.parse(
"SESSION=123; domain=example.com; max-age=60; path=/; secure; httponly; samesite=Lax");
MockCookie cookie = MockCookie.parse("SESSION=123; domain=example.com; max-age=60; " +
"expires=Tue, 8 Oct 2019 19:50:00 GMT; path=/; secure; httponly; samesite=Lax");
assertCookie(cookie, "SESSION", "123");
assertEquals("example.com", cookie.getDomain());
assertEquals(60, cookie.getMaxAge());
assertEquals("/", cookie.getPath());
assertTrue(cookie.getSecure());
assertTrue(cookie.isHttpOnly());
assertEquals(ZonedDateTime.parse("Tue, 8 Oct 2019 19:50:00 GMT",
DateTimeFormatter.RFC_1123_DATE_TIME), cookie.getExpires());
assertEquals("Lax", cookie.getSameSite());
}
@@ -39,6 +39,7 @@ import static org.junit.Assert.*;
* @author Rob Winch
* @author Sam Brannen
* @author Brian Clozel
* @author Vedran Pavic
* @since 19.02.2006
*/
public class MockHttpServletResponseTests {
@@ -351,6 +352,14 @@ public class MockHttpServletResponseTests {
assertCookieValues("123", "999");
}
@Test
public void addCookieHeaderWithExpires() {
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=Tue, 8 Oct 2019 19:50:00 GMT; Secure; " +
"HttpOnly; SameSite=Lax";
response.addHeader(HttpHeaders.SET_COOKIE, cookieValue);
assertEquals(cookieValue, response.getHeader(HttpHeaders.SET_COOKIE));
}
@Test
public void addCookie() {
MockCookie mockCookie = new MockCookie("SESSION", "123");