Polishing

This commit is contained in:
Sam Brannen
2024-09-20 10:57:03 +02:00
parent 3ba9d35e22
commit 8931b75b95
4 changed files with 52 additions and 33 deletions

View File

@@ -43,9 +43,16 @@ public class MockCookie extends Cookie {
private static final long serialVersionUID = 4312531139502726325L;
private static final String SAME_SITE = "SameSite";
private static final String EXPIRES = "Expires";
private static final String PATH = "Path";
private static final String DOMAIN = "Domain";
private static final String COMMENT = "Comment";
private static final String SECURE = "Secure";
private static final String HTTP_ONLY = "HttpOnly";
private static final String PARTITIONED = "Partitioned";
private static final String SAME_SITE = "SameSite";
private static final String MAX_AGE = "Max-Age";
private static final String EXPIRES = "Expires";
@Nullable
private ZonedDateTime expires;
@@ -140,10 +147,10 @@ public class MockCookie extends Cookie {
MockCookie cookie = new MockCookie(name, value);
for (String attribute : attributes) {
if (StringUtils.startsWithIgnoreCase(attribute, "Domain")) {
if (StringUtils.startsWithIgnoreCase(attribute, DOMAIN)) {
cookie.setDomain(extractAttributeValue(attribute, setCookieHeader));
}
else if (StringUtils.startsWithIgnoreCase(attribute, "Max-Age")) {
else if (StringUtils.startsWithIgnoreCase(attribute, MAX_AGE)) {
cookie.setMaxAge(Integer.parseInt(extractAttributeValue(attribute, setCookieHeader)));
}
else if (StringUtils.startsWithIgnoreCase(attribute, EXPIRES)) {
@@ -155,19 +162,19 @@ public class MockCookie extends Cookie {
// ignore invalid date formats
}
}
else if (StringUtils.startsWithIgnoreCase(attribute, "Path")) {
else if (StringUtils.startsWithIgnoreCase(attribute, PATH)) {
cookie.setPath(extractAttributeValue(attribute, setCookieHeader));
}
else if (StringUtils.startsWithIgnoreCase(attribute, "Secure")) {
else if (StringUtils.startsWithIgnoreCase(attribute, SECURE)) {
cookie.setSecure(true);
}
else if (StringUtils.startsWithIgnoreCase(attribute, "HttpOnly")) {
else if (StringUtils.startsWithIgnoreCase(attribute, HTTP_ONLY)) {
cookie.setHttpOnly(true);
}
else if (StringUtils.startsWithIgnoreCase(attribute, SAME_SITE)) {
cookie.setSameSite(extractAttributeValue(attribute, setCookieHeader));
}
else if (StringUtils.startsWithIgnoreCase(attribute, "Comment")) {
else if (StringUtils.startsWithIgnoreCase(attribute, COMMENT)) {
cookie.setComment(extractAttributeValue(attribute, setCookieHeader));
}
else if (!attribute.isEmpty()) {
@@ -202,15 +209,15 @@ public class MockCookie extends Cookie {
return new ToStringCreator(this)
.append("name", getName())
.append("value", getValue())
.append("Path", getPath())
.append("Domain", getDomain())
.append(PATH, getPath())
.append(DOMAIN, getDomain())
.append("Version", getVersion())
.append("Comment", getComment())
.append("Secure", getSecure())
.append("HttpOnly", isHttpOnly())
.append(COMMENT, getComment())
.append(SECURE, getSecure())
.append(HTTP_ONLY, isHttpOnly())
.append(PARTITIONED, isPartitioned())
.append(SAME_SITE, getSameSite())
.append("Max-Age", getMaxAge())
.append(MAX_AGE, getMaxAge())
.append(EXPIRES, getAttribute(EXPIRES))
.toString();
}

View File

@@ -394,7 +394,7 @@ class Jackson2JsonDecoderTests extends AbstractDecoderTests<Jackson2JsonDecoder>
private static final long serialVersionUID = 1L;
protected Deserializer() {
Deserializer() {
super(TestObject.class);
}

View File

@@ -106,8 +106,8 @@ class ContentCachingRequestWrapperTests {
}
};
assertThatIllegalStateException().isThrownBy(() ->
wrapper.getInputStream().readAllBytes())
assertThatIllegalStateException()
.isThrownBy(() -> wrapper.getInputStream().readAllBytes())
.withMessage("3");
}

View File

@@ -43,9 +43,16 @@ public class MockCookie extends Cookie {
private static final long serialVersionUID = 4312531139502726325L;
private static final String SAME_SITE = "SameSite";
private static final String EXPIRES = "Expires";
private static final String PATH = "Path";
private static final String DOMAIN = "Domain";
private static final String COMMENT = "Comment";
private static final String SECURE = "Secure";
private static final String HTTP_ONLY = "HttpOnly";
private static final String PARTITIONED = "Partitioned";
private static final String SAME_SITE = "SameSite";
private static final String MAX_AGE = "Max-Age";
private static final String EXPIRES = "Expires";
@Nullable
private ZonedDateTime expires;
@@ -140,10 +147,10 @@ public class MockCookie extends Cookie {
MockCookie cookie = new MockCookie(name, value);
for (String attribute : attributes) {
if (StringUtils.startsWithIgnoreCase(attribute, "Domain")) {
if (StringUtils.startsWithIgnoreCase(attribute, DOMAIN)) {
cookie.setDomain(extractAttributeValue(attribute, setCookieHeader));
}
else if (StringUtils.startsWithIgnoreCase(attribute, "Max-Age")) {
else if (StringUtils.startsWithIgnoreCase(attribute, MAX_AGE)) {
cookie.setMaxAge(Integer.parseInt(extractAttributeValue(attribute, setCookieHeader)));
}
else if (StringUtils.startsWithIgnoreCase(attribute, EXPIRES)) {
@@ -155,23 +162,23 @@ public class MockCookie extends Cookie {
// ignore invalid date formats
}
}
else if (StringUtils.startsWithIgnoreCase(attribute, "Path")) {
else if (StringUtils.startsWithIgnoreCase(attribute, PATH)) {
cookie.setPath(extractAttributeValue(attribute, setCookieHeader));
}
else if (StringUtils.startsWithIgnoreCase(attribute, "Secure")) {
else if (StringUtils.startsWithIgnoreCase(attribute, SECURE)) {
cookie.setSecure(true);
}
else if (StringUtils.startsWithIgnoreCase(attribute, "HttpOnly")) {
else if (StringUtils.startsWithIgnoreCase(attribute, HTTP_ONLY)) {
cookie.setHttpOnly(true);
}
else if (StringUtils.startsWithIgnoreCase(attribute, SAME_SITE)) {
cookie.setSameSite(extractAttributeValue(attribute, setCookieHeader));
}
else if (StringUtils.startsWithIgnoreCase(attribute, "Comment")) {
else if (StringUtils.startsWithIgnoreCase(attribute, COMMENT)) {
cookie.setComment(extractAttributeValue(attribute, setCookieHeader));
}
else {
cookie.setAttribute(attribute, extractAttributeValue(attribute, setCookieHeader));
else if (!attribute.isEmpty()) {
cookie.setAttribute(attribute, extractOptionalAttributeValue(attribute, setCookieHeader));
}
}
return cookie;
@@ -184,6 +191,11 @@ public class MockCookie extends Cookie {
return nameAndValue[1];
}
private static String extractOptionalAttributeValue(String attribute, String header) {
String[] nameAndValue = attribute.split("=");
return nameAndValue.length == 2 ? nameAndValue[1] : "";
}
@Override
public void setAttribute(String name, @Nullable String value) {
if (EXPIRES.equalsIgnoreCase(name)) {
@@ -197,15 +209,15 @@ public class MockCookie extends Cookie {
return new ToStringCreator(this)
.append("name", getName())
.append("value", getValue())
.append("Path", getPath())
.append("Domain", getDomain())
.append(PATH, getPath())
.append(DOMAIN, getDomain())
.append("Version", getVersion())
.append("Comment", getComment())
.append("Secure", getSecure())
.append("HttpOnly", isHttpOnly())
.append(COMMENT, getComment())
.append(SECURE, getSecure())
.append(HTTP_ONLY, isHttpOnly())
.append(PARTITIONED, isPartitioned())
.append(SAME_SITE, getSameSite())
.append("Max-Age", getMaxAge())
.append(MAX_AGE, getMaxAge())
.append(EXPIRES, getAttribute(EXPIRES))
.toString();
}