Update MockCookie to use Servlet 6.0 APIs and semantics for "attributes"
Closes gh-30263
This commit is contained in:
@@ -39,16 +39,14 @@ import org.springframework.util.StringUtils;
|
||||
@SuppressWarnings("removal")
|
||||
public class MockCookie extends Cookie {
|
||||
|
||||
private static final long serialVersionUID = 4312531139502726325L;
|
||||
private static final long serialVersionUID = 1198809317225300389L;
|
||||
|
||||
private static final String SAME_SITE = "SameSite";
|
||||
private static final String EXPIRES = "Expires";
|
||||
|
||||
@Nullable
|
||||
private ZonedDateTime expires;
|
||||
|
||||
@Nullable
|
||||
private String sameSite;
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new {@link MockCookie} with the supplied name and value.
|
||||
* @param name the name
|
||||
@@ -64,7 +62,7 @@ public class MockCookie extends Cookie {
|
||||
* @since 5.1.11
|
||||
*/
|
||||
public void setExpires(@Nullable ZonedDateTime expires) {
|
||||
this.expires = expires;
|
||||
setAttribute(EXPIRES, expires != null ? expires.format(DateTimeFormatter.RFC_1123_DATE_TIME) : null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -85,7 +83,7 @@ public class MockCookie extends Cookie {
|
||||
* @see <a href="https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis#section-4.1.2.7">RFC6265 bis</a>
|
||||
*/
|
||||
public void setSameSite(@Nullable String sameSite) {
|
||||
this.sameSite = sameSite;
|
||||
setAttribute(SAME_SITE, sameSite);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,10 +92,9 @@ public class MockCookie extends Cookie {
|
||||
*/
|
||||
@Nullable
|
||||
public String getSameSite() {
|
||||
return this.sameSite;
|
||||
return getAttribute(SAME_SITE);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Factory method that parses the value of the supplied "Set-Cookie" header.
|
||||
* @param setCookieHeader the "Set-Cookie" value; never {@code null} or empty
|
||||
@@ -122,7 +119,7 @@ public class MockCookie extends Cookie {
|
||||
else if (StringUtils.startsWithIgnoreCase(attribute, "Max-Age")) {
|
||||
cookie.setMaxAge(Integer.parseInt(extractAttributeValue(attribute, setCookieHeader)));
|
||||
}
|
||||
else if (StringUtils.startsWithIgnoreCase(attribute, "Expires")) {
|
||||
else if (StringUtils.startsWithIgnoreCase(attribute, EXPIRES)) {
|
||||
try {
|
||||
cookie.setExpires(ZonedDateTime.parse(extractAttributeValue(attribute, setCookieHeader),
|
||||
DateTimeFormatter.RFC_1123_DATE_TIME));
|
||||
@@ -140,7 +137,7 @@ public class MockCookie extends Cookie {
|
||||
else if (StringUtils.startsWithIgnoreCase(attribute, "HttpOnly")) {
|
||||
cookie.setHttpOnly(true);
|
||||
}
|
||||
else if (StringUtils.startsWithIgnoreCase(attribute, "SameSite")) {
|
||||
else if (StringUtils.startsWithIgnoreCase(attribute, SAME_SITE)) {
|
||||
cookie.setSameSite(extractAttributeValue(attribute, setCookieHeader));
|
||||
}
|
||||
else if (StringUtils.startsWithIgnoreCase(attribute, "Comment")) {
|
||||
@@ -157,6 +154,14 @@ public class MockCookie extends Cookie {
|
||||
return nameAndValue[1];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, @Nullable String value) {
|
||||
if(EXPIRES.equalsIgnoreCase(name)) {
|
||||
this.expires = value != null ? ZonedDateTime.parse(value, DateTimeFormatter.RFC_1123_DATE_TIME) : null;
|
||||
}
|
||||
super.setAttribute(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new ToStringCreator(this)
|
||||
@@ -168,9 +173,9 @@ public class MockCookie extends Cookie {
|
||||
.append("Comment", getComment())
|
||||
.append("Secure", getSecure())
|
||||
.append("HttpOnly", isHttpOnly())
|
||||
.append("SameSite", this.sameSite)
|
||||
.append(SAME_SITE, getSameSite())
|
||||
.append("Max-Age", getMaxAge())
|
||||
.append("Expires", (this.expires != null ?
|
||||
.append(EXPIRES, (this.expires != null ?
|
||||
DateTimeFormatter.RFC_1123_DATE_TIME.format(this.expires) : null))
|
||||
.toString();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user