Support cookie comments in MockHttpServletResponse and MockCookie
Prior to this commit, if a cookie was added to MockHttpServletResponse, the comment attribute was not included in the generated Set-Cookie header. In addition, MockCookie.parse(String) did not support the Comment attribute. This commit addresses both of these issues. Closes gh-28730
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -22,6 +22,7 @@ import java.time.format.DateTimeFormatter;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -67,8 +68,8 @@ public class MockCookie extends Cookie {
|
||||
|
||||
/**
|
||||
* Get the "Expires" attribute for this cookie.
|
||||
* @since 5.1.11
|
||||
* @return the "Expires" attribute for this cookie, or {@code null} if not set
|
||||
* @since 5.1.11
|
||||
*/
|
||||
@Nullable
|
||||
public ZonedDateTime getExpires() {
|
||||
@@ -141,6 +142,9 @@ public class MockCookie extends Cookie {
|
||||
else if (StringUtils.startsWithIgnoreCase(attribute, "SameSite")) {
|
||||
cookie.setSameSite(extractAttributeValue(attribute, setCookieHeader));
|
||||
}
|
||||
else if (StringUtils.startsWithIgnoreCase(attribute, "Comment")) {
|
||||
cookie.setComment(extractAttributeValue(attribute, setCookieHeader));
|
||||
}
|
||||
}
|
||||
return cookie;
|
||||
}
|
||||
@@ -152,4 +156,22 @@ public class MockCookie extends Cookie {
|
||||
return nameAndValue[1];
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new ToStringCreator(this)
|
||||
.append("name", getName())
|
||||
.append("value", getValue())
|
||||
.append("Path", getPath())
|
||||
.append("Domain", getDomain())
|
||||
.append("Version", getVersion())
|
||||
.append("Comment", getComment())
|
||||
.append("Secure", getSecure())
|
||||
.append("HttpOnly", isHttpOnly())
|
||||
.append("SameSite", this.sameSite)
|
||||
.append("Max-Age", getMaxAge())
|
||||
.append("Expires", (this.expires != null ?
|
||||
DateTimeFormatter.RFC_1123_DATE_TIME.format(this.expires) : null))
|
||||
.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -450,6 +450,9 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
buf.append("; SameSite=").append(mockCookie.getSameSite());
|
||||
}
|
||||
}
|
||||
if (StringUtils.hasText(cookie.getComment())) {
|
||||
buf.append("; Comment=").append(cookie.getComment());
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user