diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockCookie.java b/spring-test/src/main/java/org/springframework/mock/web/MockCookie.java index a7565f180e7..17c3f2c027c 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockCookie.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockCookie.java @@ -46,7 +46,7 @@ public class MockCookie extends Cookie { /** - * Constructor with the cookie name and value. + * Construct a new {@link MockCookie} with the supplied name and value. * @param name the name * @param value the value * @see Cookie#Cookie(String, String) @@ -56,14 +56,17 @@ public class MockCookie extends Cookie { } /** - * Add the "Expires" attribute to the cookie. + * Set the "Expires" attribute for this cookie. + * @since 5.1.11 */ public void setExpires(@Nullable ZonedDateTime expires) { this.expires = expires; } /** - * Return the "Expires" attribute, or {@code null} if not set. + * Get the "Expires" attribute for this cookie. + * @since 5.1.11 + * @return the "Expires" attribute for this cookie, or {@code null} if not set */ @Nullable public ZonedDateTime getExpires() { @@ -71,10 +74,10 @@ public class MockCookie extends Cookie { } /** - * Add the "SameSite" attribute to the cookie. + * Set the "SameSite" attribute for this cookie. *
This limits the scope of the cookie such that it will only be attached - * to same site requests if {@code "Strict"} or cross-site requests if - * {@code "Lax"}. + * to same-site requests if the supplied value is {@code "Strict"} or cross-site + * requests if the supplied value is {@code "Lax"}. * @see RFC6265 bis */ public void setSameSite(@Nullable String sameSite) { @@ -82,7 +85,8 @@ public class MockCookie extends Cookie { } /** - * Return the "SameSite" attribute, or {@code null} if not set. + * Get the "SameSite" attribute for this cookie. + * @return the "SameSite" attribute for this cookie, or {@code null} if not set */ @Nullable public String getSameSite() { @@ -91,7 +95,7 @@ public class MockCookie extends Cookie { /** - * Factory method that parses the value of a "Set-Cookie" header. + * Factory method that parses the value of the supplied "Set-Cookie" header. * @param setCookieHeader the "Set-Cookie" value; never {@code null} or empty * @return the created cookie */ diff --git a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java index 4218a79dc27..70f6f002a04 100644 --- a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java +++ b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java @@ -339,6 +339,18 @@ public class MockHttpServletResponseTests { assertPrimarySessionCookie("999"); } + /** + * @since 5.1.11 + */ + @Test + public void setCookieHeaderWithExpiresAttribute() { + String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=Tue, 8 Oct 2019 19:50:00 GMT; Secure; " + + "HttpOnly; SameSite=Lax"; + response.setHeader(HttpHeaders.SET_COOKIE, cookieValue); + assertNumCookies(1); + assertEquals(cookieValue, response.getHeader(HttpHeaders.SET_COOKIE)); + } + @Test public void addCookieHeader() { response.addHeader(HttpHeaders.SET_COOKIE, "SESSION=123; Path=/; Secure; HttpOnly; SameSite=Lax"); @@ -352,8 +364,11 @@ public class MockHttpServletResponseTests { assertCookieValues("123", "999"); } + /** + * @since 5.1.11 + */ @Test - public void addCookieHeaderWithExpires() { + public void addCookieHeaderWithExpiresAttribute() { String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=Tue, 8 Oct 2019 19:50:00 GMT; Secure; " + "HttpOnly; SameSite=Lax"; response.addHeader(HttpHeaders.SET_COOKIE, cookieValue); diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockCookie.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockCookie.java index 9b6ba944855..3bea81f01d4 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockCookie.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockCookie.java @@ -46,7 +46,7 @@ public class MockCookie extends Cookie { /** - * Constructor with the cookie name and value. + * Construct a new {@link MockCookie} with the supplied name and value. * @param name the name * @param value the value * @see Cookie#Cookie(String, String) @@ -56,14 +56,17 @@ public class MockCookie extends Cookie { } /** - * Add the "Expires" attribute to the cookie. + * Set the "Expires" attribute for this cookie. + * @since 5.1.11 */ public void setExpires(@Nullable ZonedDateTime expires) { this.expires = expires; } /** - * Return the "Expires" attribute, or {@code null} if not set. + * Get the "Expires" attribute for this cookie. + * @since 5.1.11 + * @return the "Expires" attribute for this cookie, or {@code null} if not set */ @Nullable public ZonedDateTime getExpires() { @@ -71,10 +74,10 @@ public class MockCookie extends Cookie { } /** - * Add the "SameSite" attribute to the cookie. + * Set the "SameSite" attribute for this cookie. *
This limits the scope of the cookie such that it will only be attached - * to same site requests if {@code "Strict"} or cross-site requests if - * {@code "Lax"}. + * to same-site requests if the supplied value is {@code "Strict"} or cross-site + * requests if the supplied value is {@code "Lax"}. * @see RFC6265 bis */ public void setSameSite(@Nullable String sameSite) { @@ -82,7 +85,8 @@ public class MockCookie extends Cookie { } /** - * Return the "SameSite" attribute, or {@code null} if not set. + * Get the "SameSite" attribute for this cookie. + * @return the "SameSite" attribute for this cookie, or {@code null} if not set */ @Nullable public String getSameSite() { @@ -91,7 +95,7 @@ public class MockCookie extends Cookie { /** - * Factory method that parses the value of a "Set-Cookie" header. + * Factory method that parses the value of the supplied "Set-Cookie" header. * @param setCookieHeader the "Set-Cookie" value; never {@code null} or empty * @return the created cookie */