1
1
/*
2
- * Copyright 2002-2022 the original author or authors.
2
+ * Copyright 2002-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
31
31
* Extension of {@code Cookie} with extra attributes, as defined in
32
32
* <a href="https://tools.ietf.org/html/rfc6265">RFC 6265</a>.
33
33
*
34
+ * <p>As of Spring 6.0, this set of mocks is designed on a Servlet 6.0 baseline.
35
+ *
34
36
* @author Vedran Pavic
35
37
* @author Juergen Hoeller
36
38
* @author Sam Brannen
@@ -41,13 +43,12 @@ public class MockCookie extends Cookie {
41
43
42
44
private static final long serialVersionUID = 4312531139502726325L ;
43
45
46
+ private static final String SAME_SITE = "SameSite" ;
47
+ private static final String EXPIRES = "Expires" ;
44
48
45
49
@ Nullable
46
50
private ZonedDateTime expires ;
47
51
48
- @ Nullable
49
- private String sameSite ;
50
-
51
52
52
53
/**
53
54
* Construct a new {@link MockCookie} with the supplied name and value.
@@ -64,7 +65,7 @@ public MockCookie(String name, String value) {
64
65
* @since 5.1.11
65
66
*/
66
67
public void setExpires (@ Nullable ZonedDateTime expires ) {
67
- this . expires = expires ;
68
+ setAttribute ( EXPIRES , ( expires != null ? expires . format ( DateTimeFormatter . RFC_1123_DATE_TIME ) : null )) ;
68
69
}
69
70
70
71
/**
@@ -85,7 +86,7 @@ public ZonedDateTime getExpires() {
85
86
* @see <a href="https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis#section-4.1.2.7">RFC6265 bis</a>
86
87
*/
87
88
public void setSameSite (@ Nullable String sameSite ) {
88
- this . sameSite = sameSite ;
89
+ setAttribute ( SAME_SITE , sameSite ) ;
89
90
}
90
91
91
92
/**
@@ -94,10 +95,9 @@ public void setSameSite(@Nullable String sameSite) {
94
95
*/
95
96
@ Nullable
96
97
public String getSameSite () {
97
- return this . sameSite ;
98
+ return getAttribute ( SAME_SITE ) ;
98
99
}
99
100
100
-
101
101
/**
102
102
* Factory method that parses the value of the supplied "Set-Cookie" header.
103
103
* @param setCookieHeader the "Set-Cookie" value; never {@code null} or empty
@@ -122,7 +122,7 @@ public static MockCookie parse(String setCookieHeader) {
122
122
else if (StringUtils .startsWithIgnoreCase (attribute , "Max-Age" )) {
123
123
cookie .setMaxAge (Integer .parseInt (extractAttributeValue (attribute , setCookieHeader )));
124
124
}
125
- else if (StringUtils .startsWithIgnoreCase (attribute , "Expires" )) {
125
+ else if (StringUtils .startsWithIgnoreCase (attribute , EXPIRES )) {
126
126
try {
127
127
cookie .setExpires (ZonedDateTime .parse (extractAttributeValue (attribute , setCookieHeader ),
128
128
DateTimeFormatter .RFC_1123_DATE_TIME ));
@@ -140,7 +140,7 @@ else if (StringUtils.startsWithIgnoreCase(attribute, "Secure")) {
140
140
else if (StringUtils .startsWithIgnoreCase (attribute , "HttpOnly" )) {
141
141
cookie .setHttpOnly (true );
142
142
}
143
- else if (StringUtils .startsWithIgnoreCase (attribute , "SameSite" )) {
143
+ else if (StringUtils .startsWithIgnoreCase (attribute , SAME_SITE )) {
144
144
cookie .setSameSite (extractAttributeValue (attribute , setCookieHeader ));
145
145
}
146
146
else if (StringUtils .startsWithIgnoreCase (attribute , "Comment" )) {
@@ -157,6 +157,14 @@ private static String extractAttributeValue(String attribute, String header) {
157
157
return nameAndValue [1 ];
158
158
}
159
159
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
+
160
168
@ Override
161
169
public String toString () {
162
170
return new ToStringCreator (this )
@@ -168,10 +176,9 @@ public String toString() {
168
176
.append ("Comment" , getComment ())
169
177
.append ("Secure" , getSecure ())
170
178
.append ("HttpOnly" , isHttpOnly ())
171
- .append ("SameSite" , this . sameSite )
179
+ .append (SAME_SITE , getSameSite () )
172
180
.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 ))
175
182
.toString ();
176
183
}
177
184
0 commit comments