|
22 | 22 | import org.springframework.util.Assert;
|
23 | 23 |
|
24 | 24 | /**
|
25 |
| - * Extension of {@code Cookie} with extra directives, as defined in |
| 25 | + * Extension of {@code Cookie} with extra attributes, as defined in |
26 | 26 | * <a href="https://tools.ietf.org/html/rfc6265">RFC 6265</a>.
|
27 | 27 | *
|
28 | 28 | * @author Vedran Pavic
|
@@ -70,45 +70,48 @@ public String getSameSite() {
|
70 | 70 |
|
71 | 71 | /**
|
72 | 72 | * 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 |
74 | 74 | * @return the created cookie
|
75 | 75 | */
|
76 | 76 | public static MockCookie parse(String setCookieHeader) {
|
| 77 | + Assert.notNull(setCookieHeader, "Set-Cookie header must not be null"); |
77 | 78 | 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 + "'"); |
79 | 80 |
|
80 | 81 | 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]); |
84 | 86 |
|
85 | 87 | 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)); |
89 | 91 | }
|
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))); |
92 | 94 | }
|
93 |
| - else if (directive.startsWith("Path")) { |
94 |
| - cookie.setPath(extractDirectiveValue(directive)); |
| 95 | + else if (attribute.startsWith("Path")) { |
| 96 | + cookie.setPath(extractAttributeValue(attribute, setCookieHeader)); |
95 | 97 | }
|
96 |
| - else if (directive.startsWith("Secure")) { |
| 98 | + else if (attribute.startsWith("Secure")) { |
97 | 99 | cookie.setSecure(true);
|
98 | 100 | }
|
99 |
| - else if (directive.startsWith("HttpOnly")) { |
| 101 | + else if (attribute.startsWith("HttpOnly")) { |
100 | 102 | cookie.setHttpOnly(true);
|
101 | 103 | }
|
102 |
| - else if (directive.startsWith("SameSite")) { |
103 |
| - cookie.setSameSite(extractDirectiveValue(directive)); |
| 104 | + else if (attribute.startsWith("SameSite")) { |
| 105 | + cookie.setSameSite(extractAttributeValue(attribute, setCookieHeader)); |
104 | 106 | }
|
105 | 107 | }
|
106 | 108 | return cookie;
|
107 | 109 | }
|
108 | 110 |
|
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 + "'"); |
112 | 115 | return nameAndValue[1];
|
113 | 116 | }
|
114 | 117 |
|
|
0 commit comments