19
19
import javax .servlet .http .Cookie ;
20
20
21
21
import org .springframework .lang .Nullable ;
22
+ import org .springframework .util .Assert ;
22
23
23
24
/**
24
- * A {@code Cookie} subclass with the additional cookie directives as defined in the
25
+ * Extension of {@code Cookie} with extra directives, as defined in
25
26
* <a href="https://tools.ietf.org/html/rfc6265">RFC 6265</a>.
26
27
*
27
28
* @author Vedran Pavic
@@ -31,96 +32,84 @@ public class MockCookie extends Cookie {
31
32
32
33
private static final long serialVersionUID = 4312531139502726325L ;
33
34
35
+
34
36
@ Nullable
35
37
private String sameSite ;
36
38
39
+
37
40
/**
38
- * Constructs a {@code MockCookie} instance with the specified name and value.
39
- *
40
- * @param name the cookie name
41
- * @param value the cookie value
41
+ * Constructor with the cookie name and value.
42
+ * @param name the name
43
+ * @param value the value
42
44
* @see Cookie#Cookie(String, String)
43
45
*/
44
46
public MockCookie (String name , String value ) {
45
47
super (name , value );
46
48
}
47
49
50
+
51
+ /**
52
+ * Add the "SameSite" attribute to the cookie.
53
+ * <p>This limits the scope of the cookie such that it will only be attached
54
+ * to same site requests if {@code "Strict"} or cross-site requests if
55
+ * {@code "Lax"}.
56
+ * @see <a href="https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis#section-4.1.2.7">RFC6265 bis</a>
57
+ */
58
+ public void setSameSite (@ Nullable String sameSite ) {
59
+ this .sameSite = sameSite ;
60
+ }
61
+
62
+ /**
63
+ * Return the "SameSite" attribute, or {@code null} if not set.
64
+ */
65
+ @ Nullable
66
+ public String getSameSite () {
67
+ return this .sameSite ;
68
+ }
69
+
70
+
48
71
/**
49
- * Factory method create {@code MockCookie} instance from Set-Cookie header value.
50
- *
51
- * @param setCookieHeader the Set-Cookie header value
52
- * @return the created cookie instance
72
+ * Factory method that parses the value of a "Set-Cookie" header.
73
+ * @param setCookieHeader the "Set-Cookie" value
74
+ * @return the created cookie
53
75
*/
54
76
public static MockCookie parse (String setCookieHeader ) {
55
77
String [] cookieParts = setCookieHeader .split ("\\ s*=\\ s*" , 2 );
56
- if (cookieParts .length != 2 ) {
57
- throw new IllegalArgumentException ("Invalid Set-Cookie header value" );
58
- }
78
+ Assert .isTrue (cookieParts .length == 2 , "Invalid Set-Cookie header value" );
79
+
59
80
String name = cookieParts [0 ];
60
81
String [] valueAndDirectives = cookieParts [1 ].split ("\\ s*;\\ s*" , 2 );
61
82
String value = valueAndDirectives [0 ];
62
83
String [] directives = valueAndDirectives [1 ].split ("\\ s*;\\ s*" );
63
- String domain = null ;
64
- int maxAge = -1 ;
65
- String path = null ;
66
- boolean secure = false ;
67
- boolean httpOnly = false ;
68
- String sameSite = null ;
84
+
85
+ MockCookie cookie = new MockCookie (name , value );
69
86
for (String directive : directives ) {
70
87
if (directive .startsWith ("Domain" )) {
71
- domain = directive . split ( "=" )[ 1 ] ;
88
+ cookie . setDomain ( extractDirectiveValue ( directive )) ;
72
89
}
73
90
else if (directive .startsWith ("Max-Age" )) {
74
- maxAge = Integer .parseInt (directive . split ( "=" )[ 1 ] );
91
+ cookie . setMaxAge ( Integer .parseInt (extractDirectiveValue ( directive )) );
75
92
}
76
93
else if (directive .startsWith ("Path" )) {
77
- path = directive . split ( "=" )[ 1 ] ;
94
+ cookie . setPath ( extractDirectiveValue ( directive )) ;
78
95
}
79
96
else if (directive .startsWith ("Secure" )) {
80
- secure = true ;
97
+ cookie . setSecure ( true ) ;
81
98
}
82
99
else if (directive .startsWith ("HttpOnly" )) {
83
- httpOnly = true ;
100
+ cookie . setHttpOnly ( true ) ;
84
101
}
85
102
else if (directive .startsWith ("SameSite" )) {
86
- sameSite = directive . split ( "=" )[ 1 ] ;
103
+ cookie . setSameSite ( extractDirectiveValue ( directive )) ;
87
104
}
88
105
}
89
- MockCookie cookie = new MockCookie (name , value );
90
- if (domain != null ) {
91
- cookie .setDomain (domain );
92
- }
93
- cookie .setMaxAge (maxAge );
94
- cookie .setPath (path );
95
- cookie .setSecure (secure );
96
- cookie .setHttpOnly (httpOnly );
97
- cookie .setSameSite (sameSite );
98
106
return cookie ;
99
107
}
100
108
101
- /**
102
- * Return the cookie "SameSite" attribute, or {@code null} if not set.
103
- * <p>
104
- * This limits the scope of the cookie such that it will only be attached to same site
105
- * requests if {@code "Strict"} or cross-site requests if {@code "Lax"}.
106
- *
107
- * @see <a href="https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis#section-4.1.2.7">RFC6265 bis</a>
108
- */
109
- @ Nullable
110
- public String getSameSite () {
111
- return this .sameSite ;
112
- }
113
-
114
- /**
115
- * Add the "SameSite" attribute to the cookie.
116
- * <p>
117
- * This limits the scope of the cookie such that it will only be attached to same site
118
- * requests if {@code "Strict"} or cross-site requests if {@code "Lax"}.
119
- *
120
- * @see <a href="https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis#section-4.1.2.7">RFC6265 bis</a>
121
- */
122
- public void setSameSite (@ Nullable String sameSite ) {
123
- this .sameSite = sameSite ;
109
+ private static String extractDirectiveValue (String directive ) {
110
+ String [] nameAndValue = directive .split ("=" );
111
+ Assert .isTrue (nameAndValue .length == 2 , () -> "No value in directive: '" + directive + "'" );
112
+ return nameAndValue [1 ];
124
113
}
125
114
126
115
}
0 commit comments