Skip to content

Commit efdbddd

Browse files
committed
Full alignment of spring-test vs spring-web MockCookie variants
Issue: SPR-17321
1 parent cf3635b commit efdbddd

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

spring-test/src/main/java/org/springframework/mock/web/MockCookie.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ public static MockCookie parse(String setCookieHeader) {
8181
String name = cookieParts[0];
8282
String[] valueAndAttributes = cookieParts[1].split("\\s*;\\s*", 2);
8383
String value = valueAndAttributes[0];
84-
String[] attributes = valueAndAttributes.length > 1 ? valueAndAttributes[1].split("\\s*;\\s*") : new String[0];
84+
String[] attributes =
85+
(valueAndAttributes.length > 1 ? valueAndAttributes[1].split("\\s*;\\s*") : new String[0]);
8586

8687
MockCookie cookie = new MockCookie(name, value);
8788
for (String attribute : attributes) {

spring-web/src/test/java/org/springframework/mock/web/test/MockCookie.java

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.springframework.util.Assert;
2323

2424
/**
25-
* Extension of {@code Cookie} with extra directives, as defined in
25+
* Extension of {@code Cookie} with extra attributes, as defined in
2626
* <a href="https://tools.ietf.org/html/rfc6265">RFC 6265</a>.
2727
*
2828
* @author Vedran Pavic
@@ -70,45 +70,48 @@ public String getSameSite() {
7070

7171
/**
7272
* Factory method that parses the value of a "Set-Cookie" header.
73-
* @param setCookieHeader the "Set-Cookie" value
73+
* @param setCookieHeader the "Set-Cookie" value; never {@code null} or empty
7474
* @return the created cookie
7575
*/
7676
public static MockCookie parse(String setCookieHeader) {
77+
Assert.notNull(setCookieHeader, "Set-Cookie header must not be null");
7778
String[] cookieParts = setCookieHeader.split("\\s*=\\s*", 2);
78-
Assert.isTrue(cookieParts.length == 2, "Invalid Set-Cookie header value");
79+
Assert.isTrue(cookieParts.length == 2, () -> "Invalid Set-Cookie header '" + setCookieHeader + "'");
7980

8081
String name = cookieParts[0];
81-
String[] valueAndDirectives = cookieParts[1].split("\\s*;\\s*", 2);
82-
String value = valueAndDirectives[0];
83-
String[] directives = valueAndDirectives[1].split("\\s*;\\s*");
82+
String[] valueAndAttributes = cookieParts[1].split("\\s*;\\s*", 2);
83+
String value = valueAndAttributes[0];
84+
String[] attributes =
85+
(valueAndAttributes.length > 1 ? valueAndAttributes[1].split("\\s*;\\s*") : new String[0]);
8486

8587
MockCookie cookie = new MockCookie(name, value);
86-
for (String directive : directives) {
87-
if (directive.startsWith("Domain")) {
88-
cookie.setDomain(extractDirectiveValue(directive));
88+
for (String attribute : attributes) {
89+
if (attribute.startsWith("Domain")) {
90+
cookie.setDomain(extractAttributeValue(attribute, setCookieHeader));
8991
}
90-
else if (directive.startsWith("Max-Age")) {
91-
cookie.setMaxAge(Integer.parseInt(extractDirectiveValue(directive)));
92+
else if (attribute.startsWith("Max-Age")) {
93+
cookie.setMaxAge(Integer.parseInt(extractAttributeValue(attribute, setCookieHeader)));
9294
}
93-
else if (directive.startsWith("Path")) {
94-
cookie.setPath(extractDirectiveValue(directive));
95+
else if (attribute.startsWith("Path")) {
96+
cookie.setPath(extractAttributeValue(attribute, setCookieHeader));
9597
}
96-
else if (directive.startsWith("Secure")) {
98+
else if (attribute.startsWith("Secure")) {
9799
cookie.setSecure(true);
98100
}
99-
else if (directive.startsWith("HttpOnly")) {
101+
else if (attribute.startsWith("HttpOnly")) {
100102
cookie.setHttpOnly(true);
101103
}
102-
else if (directive.startsWith("SameSite")) {
103-
cookie.setSameSite(extractDirectiveValue(directive));
104+
else if (attribute.startsWith("SameSite")) {
105+
cookie.setSameSite(extractAttributeValue(attribute, setCookieHeader));
104106
}
105107
}
106108
return cookie;
107109
}
108110

109-
private static String extractDirectiveValue(String directive) {
110-
String[] nameAndValue = directive.split("=");
111-
Assert.isTrue(nameAndValue.length == 2, () -> "No value in directive: '" + directive + "'");
111+
private static String extractAttributeValue(String attribute, String header) {
112+
String[] nameAndValue = attribute.split("=");
113+
Assert.isTrue(nameAndValue.length == 2,
114+
() -> "No value in attribute '" + nameAndValue[0] + "' for Set-Cookie header '" + header + "'");
112115
return nameAndValue[1];
113116
}
114117

0 commit comments

Comments
 (0)