Skip to content

Commit 9fb61c5

Browse files
committed
Sync MockCookie implementations
See gh-30263
1 parent f9cb0eb commit 9fb61c5

File tree

1 file changed

+20
-13
lines changed
  • spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet

1 file changed

+20
-13
lines changed

spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockCookie.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,6 +31,8 @@
3131
* Extension of {@code Cookie} with extra attributes, as defined in
3232
* <a href="https://tools.ietf.org/html/rfc6265">RFC 6265</a>.
3333
*
34+
* <p>As of Spring 6.0, this set of mocks is designed on a Servlet 6.0 baseline.
35+
*
3436
* @author Vedran Pavic
3537
* @author Juergen Hoeller
3638
* @author Sam Brannen
@@ -41,13 +43,12 @@ public class MockCookie extends Cookie {
4143

4244
private static final long serialVersionUID = 4312531139502726325L;
4345

46+
private static final String SAME_SITE = "SameSite";
47+
private static final String EXPIRES = "Expires";
4448

4549
@Nullable
4650
private ZonedDateTime expires;
4751

48-
@Nullable
49-
private String sameSite;
50-
5152

5253
/**
5354
* Construct a new {@link MockCookie} with the supplied name and value.
@@ -64,7 +65,7 @@ public MockCookie(String name, String value) {
6465
* @since 5.1.11
6566
*/
6667
public void setExpires(@Nullable ZonedDateTime expires) {
67-
this.expires = expires;
68+
setAttribute(EXPIRES, (expires != null ? expires.format(DateTimeFormatter.RFC_1123_DATE_TIME) : null));
6869
}
6970

7071
/**
@@ -85,7 +86,7 @@ public ZonedDateTime getExpires() {
8586
* @see <a href="https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis#section-4.1.2.7">RFC6265 bis</a>
8687
*/
8788
public void setSameSite(@Nullable String sameSite) {
88-
this.sameSite = sameSite;
89+
setAttribute(SAME_SITE, sameSite);
8990
}
9091

9192
/**
@@ -94,10 +95,9 @@ public void setSameSite(@Nullable String sameSite) {
9495
*/
9596
@Nullable
9697
public String getSameSite() {
97-
return this.sameSite;
98+
return getAttribute(SAME_SITE);
9899
}
99100

100-
101101
/**
102102
* Factory method that parses the value of the supplied "Set-Cookie" header.
103103
* @param setCookieHeader the "Set-Cookie" value; never {@code null} or empty
@@ -122,7 +122,7 @@ public static MockCookie parse(String setCookieHeader) {
122122
else if (StringUtils.startsWithIgnoreCase(attribute, "Max-Age")) {
123123
cookie.setMaxAge(Integer.parseInt(extractAttributeValue(attribute, setCookieHeader)));
124124
}
125-
else if (StringUtils.startsWithIgnoreCase(attribute, "Expires")) {
125+
else if (StringUtils.startsWithIgnoreCase(attribute, EXPIRES)) {
126126
try {
127127
cookie.setExpires(ZonedDateTime.parse(extractAttributeValue(attribute, setCookieHeader),
128128
DateTimeFormatter.RFC_1123_DATE_TIME));
@@ -140,7 +140,7 @@ else if (StringUtils.startsWithIgnoreCase(attribute, "Secure")) {
140140
else if (StringUtils.startsWithIgnoreCase(attribute, "HttpOnly")) {
141141
cookie.setHttpOnly(true);
142142
}
143-
else if (StringUtils.startsWithIgnoreCase(attribute, "SameSite")) {
143+
else if (StringUtils.startsWithIgnoreCase(attribute, SAME_SITE)) {
144144
cookie.setSameSite(extractAttributeValue(attribute, setCookieHeader));
145145
}
146146
else if (StringUtils.startsWithIgnoreCase(attribute, "Comment")) {
@@ -157,6 +157,14 @@ private static String extractAttributeValue(String attribute, String header) {
157157
return nameAndValue[1];
158158
}
159159

160+
@Override
161+
public void setAttribute(String name, @Nullable String value) {
162+
if (EXPIRES.equalsIgnoreCase(name)) {
163+
this.expires = (value != null ? ZonedDateTime.parse(value, DateTimeFormatter.RFC_1123_DATE_TIME) : null);
164+
}
165+
super.setAttribute(name, value);
166+
}
167+
160168
@Override
161169
public String toString() {
162170
return new ToStringCreator(this)
@@ -168,10 +176,9 @@ public String toString() {
168176
.append("Comment", getComment())
169177
.append("Secure", getSecure())
170178
.append("HttpOnly", isHttpOnly())
171-
.append("SameSite", this.sameSite)
179+
.append(SAME_SITE, getSameSite())
172180
.append("Max-Age", getMaxAge())
173-
.append("Expires", (this.expires != null ?
174-
DateTimeFormatter.RFC_1123_DATE_TIME.format(this.expires) : null))
181+
.append(EXPIRES, getAttribute(EXPIRES))
175182
.toString();
176183
}
177184

0 commit comments

Comments
 (0)