Skip to content

Commit 8931b75

Browse files
committed
Polishing
1 parent 3ba9d35 commit 8931b75

File tree

4 files changed

+50
-31
lines changed

4 files changed

+50
-31
lines changed

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

+20-13
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,16 @@ public class MockCookie extends Cookie {
4343

4444
private static final long serialVersionUID = 4312531139502726325L;
4545

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";
4652
private static final String SAME_SITE = "SameSite";
53+
private static final String MAX_AGE = "Max-Age";
4754
private static final String EXPIRES = "Expires";
48-
private static final String PARTITIONED = "Partitioned";
55+
4956

5057
@Nullable
5158
private ZonedDateTime expires;
@@ -140,10 +147,10 @@ public static MockCookie parse(String setCookieHeader) {
140147

141148
MockCookie cookie = new MockCookie(name, value);
142149
for (String attribute : attributes) {
143-
if (StringUtils.startsWithIgnoreCase(attribute, "Domain")) {
150+
if (StringUtils.startsWithIgnoreCase(attribute, DOMAIN)) {
144151
cookie.setDomain(extractAttributeValue(attribute, setCookieHeader));
145152
}
146-
else if (StringUtils.startsWithIgnoreCase(attribute, "Max-Age")) {
153+
else if (StringUtils.startsWithIgnoreCase(attribute, MAX_AGE)) {
147154
cookie.setMaxAge(Integer.parseInt(extractAttributeValue(attribute, setCookieHeader)));
148155
}
149156
else if (StringUtils.startsWithIgnoreCase(attribute, EXPIRES)) {
@@ -155,19 +162,19 @@ else if (StringUtils.startsWithIgnoreCase(attribute, EXPIRES)) {
155162
// ignore invalid date formats
156163
}
157164
}
158-
else if (StringUtils.startsWithIgnoreCase(attribute, "Path")) {
165+
else if (StringUtils.startsWithIgnoreCase(attribute, PATH)) {
159166
cookie.setPath(extractAttributeValue(attribute, setCookieHeader));
160167
}
161-
else if (StringUtils.startsWithIgnoreCase(attribute, "Secure")) {
168+
else if (StringUtils.startsWithIgnoreCase(attribute, SECURE)) {
162169
cookie.setSecure(true);
163170
}
164-
else if (StringUtils.startsWithIgnoreCase(attribute, "HttpOnly")) {
171+
else if (StringUtils.startsWithIgnoreCase(attribute, HTTP_ONLY)) {
165172
cookie.setHttpOnly(true);
166173
}
167174
else if (StringUtils.startsWithIgnoreCase(attribute, SAME_SITE)) {
168175
cookie.setSameSite(extractAttributeValue(attribute, setCookieHeader));
169176
}
170-
else if (StringUtils.startsWithIgnoreCase(attribute, "Comment")) {
177+
else if (StringUtils.startsWithIgnoreCase(attribute, COMMENT)) {
171178
cookie.setComment(extractAttributeValue(attribute, setCookieHeader));
172179
}
173180
else if (!attribute.isEmpty()) {
@@ -202,15 +209,15 @@ public String toString() {
202209
return new ToStringCreator(this)
203210
.append("name", getName())
204211
.append("value", getValue())
205-
.append("Path", getPath())
206-
.append("Domain", getDomain())
212+
.append(PATH, getPath())
213+
.append(DOMAIN, getDomain())
207214
.append("Version", getVersion())
208-
.append("Comment", getComment())
209-
.append("Secure", getSecure())
210-
.append("HttpOnly", isHttpOnly())
215+
.append(COMMENT, getComment())
216+
.append(SECURE, getSecure())
217+
.append(HTTP_ONLY, isHttpOnly())
211218
.append(PARTITIONED, isPartitioned())
212219
.append(SAME_SITE, getSameSite())
213-
.append("Max-Age", getMaxAge())
220+
.append(MAX_AGE, getMaxAge())
214221
.append(EXPIRES, getAttribute(EXPIRES))
215222
.toString();
216223
}

spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonDecoderTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ private static class Deserializer extends StdDeserializer<TestObject> {
394394

395395
private static final long serialVersionUID = 1L;
396396

397-
protected Deserializer() {
397+
Deserializer() {
398398
super(TestObject.class);
399399
}
400400

spring-web/src/test/java/org/springframework/web/util/ContentCachingRequestWrapperTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ protected void handleContentOverflow(int contentCacheLimit) {
106106
}
107107
};
108108

109-
assertThatIllegalStateException().isThrownBy(() ->
110-
wrapper.getInputStream().readAllBytes())
109+
assertThatIllegalStateException()
110+
.isThrownBy(() -> wrapper.getInputStream().readAllBytes())
111111
.withMessage("3");
112112
}
113113

spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockCookie.java

+27-15
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,16 @@ public class MockCookie extends Cookie {
4343

4444
private static final long serialVersionUID = 4312531139502726325L;
4545

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";
4652
private static final String SAME_SITE = "SameSite";
53+
private static final String MAX_AGE = "Max-Age";
4754
private static final String EXPIRES = "Expires";
48-
private static final String PARTITIONED = "Partitioned";
55+
4956

5057
@Nullable
5158
private ZonedDateTime expires;
@@ -140,10 +147,10 @@ public static MockCookie parse(String setCookieHeader) {
140147

141148
MockCookie cookie = new MockCookie(name, value);
142149
for (String attribute : attributes) {
143-
if (StringUtils.startsWithIgnoreCase(attribute, "Domain")) {
150+
if (StringUtils.startsWithIgnoreCase(attribute, DOMAIN)) {
144151
cookie.setDomain(extractAttributeValue(attribute, setCookieHeader));
145152
}
146-
else if (StringUtils.startsWithIgnoreCase(attribute, "Max-Age")) {
153+
else if (StringUtils.startsWithIgnoreCase(attribute, MAX_AGE)) {
147154
cookie.setMaxAge(Integer.parseInt(extractAttributeValue(attribute, setCookieHeader)));
148155
}
149156
else if (StringUtils.startsWithIgnoreCase(attribute, EXPIRES)) {
@@ -155,23 +162,23 @@ else if (StringUtils.startsWithIgnoreCase(attribute, EXPIRES)) {
155162
// ignore invalid date formats
156163
}
157164
}
158-
else if (StringUtils.startsWithIgnoreCase(attribute, "Path")) {
165+
else if (StringUtils.startsWithIgnoreCase(attribute, PATH)) {
159166
cookie.setPath(extractAttributeValue(attribute, setCookieHeader));
160167
}
161-
else if (StringUtils.startsWithIgnoreCase(attribute, "Secure")) {
168+
else if (StringUtils.startsWithIgnoreCase(attribute, SECURE)) {
162169
cookie.setSecure(true);
163170
}
164-
else if (StringUtils.startsWithIgnoreCase(attribute, "HttpOnly")) {
171+
else if (StringUtils.startsWithIgnoreCase(attribute, HTTP_ONLY)) {
165172
cookie.setHttpOnly(true);
166173
}
167174
else if (StringUtils.startsWithIgnoreCase(attribute, SAME_SITE)) {
168175
cookie.setSameSite(extractAttributeValue(attribute, setCookieHeader));
169176
}
170-
else if (StringUtils.startsWithIgnoreCase(attribute, "Comment")) {
177+
else if (StringUtils.startsWithIgnoreCase(attribute, COMMENT)) {
171178
cookie.setComment(extractAttributeValue(attribute, setCookieHeader));
172179
}
173-
else {
174-
cookie.setAttribute(attribute, extractAttributeValue(attribute, setCookieHeader));
180+
else if (!attribute.isEmpty()) {
181+
cookie.setAttribute(attribute, extractOptionalAttributeValue(attribute, setCookieHeader));
175182
}
176183
}
177184
return cookie;
@@ -184,6 +191,11 @@ private static String extractAttributeValue(String attribute, String header) {
184191
return nameAndValue[1];
185192
}
186193

194+
private static String extractOptionalAttributeValue(String attribute, String header) {
195+
String[] nameAndValue = attribute.split("=");
196+
return nameAndValue.length == 2 ? nameAndValue[1] : "";
197+
}
198+
187199
@Override
188200
public void setAttribute(String name, @Nullable String value) {
189201
if (EXPIRES.equalsIgnoreCase(name)) {
@@ -197,15 +209,15 @@ public String toString() {
197209
return new ToStringCreator(this)
198210
.append("name", getName())
199211
.append("value", getValue())
200-
.append("Path", getPath())
201-
.append("Domain", getDomain())
212+
.append(PATH, getPath())
213+
.append(DOMAIN, getDomain())
202214
.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())
206218
.append(PARTITIONED, isPartitioned())
207219
.append(SAME_SITE, getSameSite())
208-
.append("Max-Age", getMaxAge())
220+
.append(MAX_AGE, getMaxAge())
209221
.append(EXPIRES, getAttribute(EXPIRES))
210222
.toString();
211223
}

0 commit comments

Comments
 (0)