@@ -43,9 +43,16 @@ public class MockCookie extends Cookie {
43
43
44
44
private static final long serialVersionUID = 4312531139502726325L ;
45
45
46
+ private static final String PATH = "Path" ;
47
+ private static final String DOMAIN = "Domain" ;
48
+ private static final String COMMENT = "Comment" ;
49
+ private static final String SECURE = "Secure" ;
50
+ private static final String HTTP_ONLY = "HttpOnly" ;
51
+ private static final String PARTITIONED = "Partitioned" ;
46
52
private static final String SAME_SITE = "SameSite" ;
53
+ private static final String MAX_AGE = "Max-Age" ;
47
54
private static final String EXPIRES = "Expires" ;
48
- private static final String PARTITIONED = "Partitioned" ;
55
+
49
56
50
57
@ Nullable
51
58
private ZonedDateTime expires ;
@@ -140,10 +147,10 @@ public static MockCookie parse(String setCookieHeader) {
140
147
141
148
MockCookie cookie = new MockCookie (name , value );
142
149
for (String attribute : attributes ) {
143
- if (StringUtils .startsWithIgnoreCase (attribute , "Domain" )) {
150
+ if (StringUtils .startsWithIgnoreCase (attribute , DOMAIN )) {
144
151
cookie .setDomain (extractAttributeValue (attribute , setCookieHeader ));
145
152
}
146
- else if (StringUtils .startsWithIgnoreCase (attribute , "Max-Age" )) {
153
+ else if (StringUtils .startsWithIgnoreCase (attribute , MAX_AGE )) {
147
154
cookie .setMaxAge (Integer .parseInt (extractAttributeValue (attribute , setCookieHeader )));
148
155
}
149
156
else if (StringUtils .startsWithIgnoreCase (attribute , EXPIRES )) {
@@ -155,23 +162,23 @@ else if (StringUtils.startsWithIgnoreCase(attribute, EXPIRES)) {
155
162
// ignore invalid date formats
156
163
}
157
164
}
158
- else if (StringUtils .startsWithIgnoreCase (attribute , "Path" )) {
165
+ else if (StringUtils .startsWithIgnoreCase (attribute , PATH )) {
159
166
cookie .setPath (extractAttributeValue (attribute , setCookieHeader ));
160
167
}
161
- else if (StringUtils .startsWithIgnoreCase (attribute , "Secure" )) {
168
+ else if (StringUtils .startsWithIgnoreCase (attribute , SECURE )) {
162
169
cookie .setSecure (true );
163
170
}
164
- else if (StringUtils .startsWithIgnoreCase (attribute , "HttpOnly" )) {
171
+ else if (StringUtils .startsWithIgnoreCase (attribute , HTTP_ONLY )) {
165
172
cookie .setHttpOnly (true );
166
173
}
167
174
else if (StringUtils .startsWithIgnoreCase (attribute , SAME_SITE )) {
168
175
cookie .setSameSite (extractAttributeValue (attribute , setCookieHeader ));
169
176
}
170
- else if (StringUtils .startsWithIgnoreCase (attribute , "Comment" )) {
177
+ else if (StringUtils .startsWithIgnoreCase (attribute , COMMENT )) {
171
178
cookie .setComment (extractAttributeValue (attribute , setCookieHeader ));
172
179
}
173
- else {
174
- cookie .setAttribute (attribute , extractAttributeValue (attribute , setCookieHeader ));
180
+ else if (! attribute . isEmpty ()) {
181
+ cookie .setAttribute (attribute , extractOptionalAttributeValue (attribute , setCookieHeader ));
175
182
}
176
183
}
177
184
return cookie ;
@@ -184,6 +191,11 @@ private static String extractAttributeValue(String attribute, String header) {
184
191
return nameAndValue [1 ];
185
192
}
186
193
194
+ private static String extractOptionalAttributeValue (String attribute , String header ) {
195
+ String [] nameAndValue = attribute .split ("=" );
196
+ return nameAndValue .length == 2 ? nameAndValue [1 ] : "" ;
197
+ }
198
+
187
199
@ Override
188
200
public void setAttribute (String name , @ Nullable String value ) {
189
201
if (EXPIRES .equalsIgnoreCase (name )) {
@@ -197,15 +209,15 @@ public String toString() {
197
209
return new ToStringCreator (this )
198
210
.append ("name" , getName ())
199
211
.append ("value" , getValue ())
200
- .append ("Path" , getPath ())
201
- .append ("Domain" , getDomain ())
212
+ .append (PATH , getPath ())
213
+ .append (DOMAIN , getDomain ())
202
214
.append ("Version" , getVersion ())
203
- .append ("Comment" , getComment ())
204
- .append ("Secure" , getSecure ())
205
- .append ("HttpOnly" , isHttpOnly ())
215
+ .append (COMMENT , getComment ())
216
+ .append (SECURE , getSecure ())
217
+ .append (HTTP_ONLY , isHttpOnly ())
206
218
.append (PARTITIONED , isPartitioned ())
207
219
.append (SAME_SITE , getSameSite ())
208
- .append ("Max-Age" , getMaxAge ())
220
+ .append (MAX_AGE , getMaxAge ())
209
221
.append (EXPIRES , getAttribute (EXPIRES ))
210
222
.toString ();
211
223
}
0 commit comments