39
39
@ SuppressWarnings ("removal" )
40
40
public class MockCookie extends Cookie {
41
41
42
- private static final long serialVersionUID = 4312531139502726325L ;
42
+ private static final long serialVersionUID = 1198809317225300389L ;
43
43
44
+ private static final String SAME_SITE = "SameSite" ;
45
+ private static final String EXPIRES = "Expires" ;
44
46
45
47
@ Nullable
46
48
private ZonedDateTime expires ;
47
49
48
- @ Nullable
49
- private String sameSite ;
50
-
51
-
52
50
/**
53
51
* Construct a new {@link MockCookie} with the supplied name and value.
54
52
* @param name the name
@@ -64,7 +62,7 @@ public MockCookie(String name, String value) {
64
62
* @since 5.1.11
65
63
*/
66
64
public void setExpires (@ Nullable ZonedDateTime expires ) {
67
- this . expires = expires ;
65
+ setAttribute ( EXPIRES , expires != null ? expires . format ( DateTimeFormatter . RFC_1123_DATE_TIME ) : null ) ;
68
66
}
69
67
70
68
/**
@@ -85,7 +83,7 @@ public ZonedDateTime getExpires() {
85
83
* @see <a href="https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis#section-4.1.2.7">RFC6265 bis</a>
86
84
*/
87
85
public void setSameSite (@ Nullable String sameSite ) {
88
- this . sameSite = sameSite ;
86
+ setAttribute ( SAME_SITE , sameSite ) ;
89
87
}
90
88
91
89
/**
@@ -94,10 +92,9 @@ public void setSameSite(@Nullable String sameSite) {
94
92
*/
95
93
@ Nullable
96
94
public String getSameSite () {
97
- return this . sameSite ;
95
+ return getAttribute ( SAME_SITE ) ;
98
96
}
99
97
100
-
101
98
/**
102
99
* Factory method that parses the value of the supplied "Set-Cookie" header.
103
100
* @param setCookieHeader the "Set-Cookie" value; never {@code null} or empty
@@ -122,7 +119,7 @@ public static MockCookie parse(String setCookieHeader) {
122
119
else if (StringUtils .startsWithIgnoreCase (attribute , "Max-Age" )) {
123
120
cookie .setMaxAge (Integer .parseInt (extractAttributeValue (attribute , setCookieHeader )));
124
121
}
125
- else if (StringUtils .startsWithIgnoreCase (attribute , "Expires" )) {
122
+ else if (StringUtils .startsWithIgnoreCase (attribute , EXPIRES )) {
126
123
try {
127
124
cookie .setExpires (ZonedDateTime .parse (extractAttributeValue (attribute , setCookieHeader ),
128
125
DateTimeFormatter .RFC_1123_DATE_TIME ));
@@ -140,7 +137,7 @@ else if (StringUtils.startsWithIgnoreCase(attribute, "Secure")) {
140
137
else if (StringUtils .startsWithIgnoreCase (attribute , "HttpOnly" )) {
141
138
cookie .setHttpOnly (true );
142
139
}
143
- else if (StringUtils .startsWithIgnoreCase (attribute , "SameSite" )) {
140
+ else if (StringUtils .startsWithIgnoreCase (attribute , SAME_SITE )) {
144
141
cookie .setSameSite (extractAttributeValue (attribute , setCookieHeader ));
145
142
}
146
143
else if (StringUtils .startsWithIgnoreCase (attribute , "Comment" )) {
@@ -157,6 +154,14 @@ private static String extractAttributeValue(String attribute, String header) {
157
154
return nameAndValue [1 ];
158
155
}
159
156
157
+ @ Override
158
+ public void setAttribute (String name , @ Nullable String value ) {
159
+ if (EXPIRES .equalsIgnoreCase (name )) {
160
+ this .expires = value != null ? ZonedDateTime .parse (value , DateTimeFormatter .RFC_1123_DATE_TIME ) : null ;
161
+ }
162
+ super .setAttribute (name , value );
163
+ }
164
+
160
165
@ Override
161
166
public String toString () {
162
167
return new ToStringCreator (this )
@@ -168,9 +173,9 @@ public String toString() {
168
173
.append ("Comment" , getComment ())
169
174
.append ("Secure" , getSecure ())
170
175
.append ("HttpOnly" , isHttpOnly ())
171
- .append ("SameSite" , this . sameSite )
176
+ .append (SAME_SITE , getSameSite () )
172
177
.append ("Max-Age" , getMaxAge ())
173
- .append ("Expires" , (this .expires != null ?
178
+ .append (EXPIRES , (this .expires != null ?
174
179
DateTimeFormatter .RFC_1123_DATE_TIME .format (this .expires ) : null ))
175
180
.toString ();
176
181
}
0 commit comments