Skip to content

Preserve expires attribute in MockCookie #23769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.springframework.mock.web;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

import javax.servlet.http.Cookie;

import org.springframework.lang.Nullable;
Expand All @@ -35,6 +38,9 @@ public class MockCookie extends Cookie {
private static final long serialVersionUID = 4312531139502726325L;


@Nullable
private ZonedDateTime expires;

@Nullable
private String sameSite;

Expand All @@ -49,6 +55,20 @@ public MockCookie(String name, String value) {
super(name, value);
}

/**
* Add the "Expires" attribute to the cookie.
*/
public void setExpires(@Nullable ZonedDateTime expires) {
this.expires = expires;
}

/**
* Return the "Expires" attribute, or {@code null} if not set.
*/
@Nullable
public ZonedDateTime getExpires() {
return this.expires;
}

/**
* Add the "SameSite" attribute to the cookie.
Expand Down Expand Up @@ -94,6 +114,10 @@ public static MockCookie parse(String setCookieHeader) {
else if (StringUtils.startsWithIgnoreCase(attribute, "Max-Age")) {
cookie.setMaxAge(Integer.parseInt(extractAttributeValue(attribute, setCookieHeader)));
}
else if (StringUtils.startsWithIgnoreCase(attribute, "Expires")) {
cookie.setExpires(ZonedDateTime.parse(extractAttributeValue(attribute, setCookieHeader),
DateTimeFormatter.RFC_1123_DATE_TIME));
}
else if (StringUtils.startsWithIgnoreCase(attribute, "Path")) {
cookie.setPath(extractAttributeValue(attribute, setCookieHeader));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -374,9 +375,14 @@ private String getCookieHeader(Cookie cookie) {
if (maxAge >= 0) {
buf.append("; Max-Age=").append(maxAge);
buf.append("; Expires=");
HttpHeaders headers = new HttpHeaders();
headers.setExpires(maxAge > 0 ? System.currentTimeMillis() + 1000L * maxAge : 0);
buf.append(headers.getFirst(HttpHeaders.EXPIRES));
if (cookie instanceof MockCookie && ((MockCookie) cookie).getExpires() != null) {
buf.append(((MockCookie) cookie).getExpires().format(DateTimeFormatter.RFC_1123_DATE_TIME));
}
else {
HttpHeaders headers = new HttpHeaders();
headers.setExpires(maxAge > 0 ? System.currentTimeMillis() + 1000L * maxAge : 0);
buf.append(headers.getFirst(HttpHeaders.EXPIRES));
}
}

if (cookie.getSecure()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import org.junit.jupiter.api.Test;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

Expand Down Expand Up @@ -62,15 +65,17 @@ void parseHeaderWithoutAttributes() {

@Test
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");
assertThat(cookie.getDomain()).isEqualTo("example.com");
assertThat(cookie.getMaxAge()).isEqualTo(60);
assertThat(cookie.getPath()).isEqualTo("/");
assertThat(cookie.getSecure()).isTrue();
assertThat(cookie.isHttpOnly()).isTrue();
assertThat(cookie.getExpires()).isEqualTo(ZonedDateTime.parse("Tue, 8 Oct 2019 19:50:00 GMT",
DateTimeFormatter.RFC_1123_DATE_TIME));
assertThat(cookie.getSameSite()).isEqualTo("Lax");
}

Expand Down Expand Up @@ -104,15 +109,17 @@ void parseInvalidAttribute() {

@Test
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");
assertThat(cookie.getDomain()).isEqualTo("example.com");
assertThat(cookie.getMaxAge()).isEqualTo(60);
assertThat(cookie.getPath()).isEqualTo("/");
assertThat(cookie.getSecure()).isTrue();
assertThat(cookie.isHttpOnly()).isTrue();
assertThat(cookie.getExpires()).isEqualTo(ZonedDateTime.parse("Tue, 8 Oct 2019 19:50:00 GMT",
DateTimeFormatter.RFC_1123_DATE_TIME));
assertThat(cookie.getSameSite()).isEqualTo("Lax");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* @author Sam Brannen
* @author Brian Clozel
* @author Sebastien Deleuze
* @author Vedran Pavic
* @since 19.02.2006
*/
class MockHttpServletResponseTests {
Expand Down Expand Up @@ -362,6 +363,14 @@ void addCookieHeader() {
assertCookieValues("123", "999");
}

@Test
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);
assertThat(response.getHeader(HttpHeaders.SET_COOKIE)).isEqualTo(cookieValue);
}

@Test
void addCookie() {
MockCookie mockCookie = new MockCookie("SESSION", "123");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.springframework.mock.web.test;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

import javax.servlet.http.Cookie;

import org.springframework.lang.Nullable;
Expand All @@ -35,6 +38,9 @@ public class MockCookie extends Cookie {
private static final long serialVersionUID = 4312531139502726325L;


@Nullable
private ZonedDateTime expires;

@Nullable
private String sameSite;

Expand All @@ -49,6 +55,20 @@ public MockCookie(String name, String value) {
super(name, value);
}

/**
* Add the "Expires" attribute to the cookie.
*/
public void setExpires(@Nullable ZonedDateTime expires) {
this.expires = expires;
}

/**
* Return the "Expires" attribute, or {@code null} if not set.
*/
@Nullable
public ZonedDateTime getExpires() {
return this.expires;
}

/**
* Add the "SameSite" attribute to the cookie.
Expand Down Expand Up @@ -94,6 +114,10 @@ public static MockCookie parse(String setCookieHeader) {
else if (StringUtils.startsWithIgnoreCase(attribute, "Max-Age")) {
cookie.setMaxAge(Integer.parseInt(extractAttributeValue(attribute, setCookieHeader)));
}
else if (StringUtils.startsWithIgnoreCase(attribute, "Expires")) {
cookie.setExpires(ZonedDateTime.parse(extractAttributeValue(attribute, setCookieHeader),
DateTimeFormatter.RFC_1123_DATE_TIME));
}
else if (StringUtils.startsWithIgnoreCase(attribute, "Path")) {
cookie.setPath(extractAttributeValue(attribute, setCookieHeader));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -374,9 +375,14 @@ private String getCookieHeader(Cookie cookie) {
if (maxAge >= 0) {
buf.append("; Max-Age=").append(maxAge);
buf.append("; Expires=");
HttpHeaders headers = new HttpHeaders();
headers.setExpires(maxAge > 0 ? System.currentTimeMillis() + 1000L * maxAge : 0);
buf.append(headers.getFirst(HttpHeaders.EXPIRES));
if (cookie instanceof MockCookie && ((MockCookie) cookie).getExpires() != null) {
buf.append(((MockCookie) cookie).getExpires().format(DateTimeFormatter.RFC_1123_DATE_TIME));
}
else {
HttpHeaders headers = new HttpHeaders();
headers.setExpires(maxAge > 0 ? System.currentTimeMillis() + 1000L * maxAge : 0);
buf.append(headers.getFirst(HttpHeaders.EXPIRES));
}
}

if (cookie.getSecure()) {
Expand Down