Skip to content

Commit ce0b012

Browse files
committed
Polish contribution
See gh-23769
1 parent 9b20876 commit ce0b012

File tree

4 files changed

+48
-25
lines changed

4 files changed

+48
-25
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class MockCookie extends Cookie {
4646

4747

4848
/**
49-
* Constructor with the cookie name and value.
49+
* Construct a new {@link MockCookie} with the supplied name and value.
5050
* @param name the name
5151
* @param value the value
5252
* @see Cookie#Cookie(String, String)
@@ -56,33 +56,37 @@ public MockCookie(String name, String value) {
5656
}
5757

5858
/**
59-
* Add the "Expires" attribute to the cookie.
59+
* Set the "Expires" attribute for this cookie.
60+
* @since 5.1.11
6061
*/
6162
public void setExpires(@Nullable ZonedDateTime expires) {
6263
this.expires = expires;
6364
}
6465

6566
/**
66-
* Return the "Expires" attribute, or {@code null} if not set.
67+
* Get the "Expires" attribute for this cookie.
68+
* @since 5.1.11
69+
* @return the "Expires" attribute for this cookie, or {@code null} if not set
6770
*/
6871
@Nullable
6972
public ZonedDateTime getExpires() {
7073
return this.expires;
7174
}
7275

7376
/**
74-
* Add the "SameSite" attribute to the cookie.
77+
* Set the "SameSite" attribute for this cookie.
7578
* <p>This limits the scope of the cookie such that it will only be attached
76-
* to same site requests if {@code "Strict"} or cross-site requests if
77-
* {@code "Lax"}.
79+
* to same-site requests if the supplied value is {@code "Strict"} or cross-site
80+
* requests if the supplied value is {@code "Lax"}.
7881
* @see <a href="https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis#section-4.1.2.7">RFC6265 bis</a>
7982
*/
8083
public void setSameSite(@Nullable String sameSite) {
8184
this.sameSite = sameSite;
8285
}
8386

8487
/**
85-
* Return the "SameSite" attribute, or {@code null} if not set.
88+
* Get the "SameSite" attribute for this cookie.
89+
* @return the "SameSite" attribute for this cookie, or {@code null} if not set
8690
*/
8791
@Nullable
8892
public String getSameSite() {
@@ -91,7 +95,7 @@ public String getSameSite() {
9195

9296

9397
/**
94-
* Factory method that parses the value of a "Set-Cookie" header.
98+
* Factory method that parses the value of the supplied "Set-Cookie" header.
9599
* @param setCookieHeader the "Set-Cookie" value; never {@code null} or empty
96100
* @return the created cookie
97101
*/

spring-test/src/test/java/org/springframework/mock/web/MockCookieTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
package org.springframework.mock.web;
1818

19-
import org.junit.jupiter.api.Test;
20-
2119
import java.time.ZonedDateTime;
2220
import java.time.format.DateTimeFormatter;
2321

22+
import org.junit.jupiter.api.Test;
23+
2424
import static org.assertj.core.api.Assertions.assertThat;
2525
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
2626

@@ -86,24 +86,24 @@ private void assertCookie(MockCookie cookie, String name, String value) {
8686

8787
@Test
8888
void parseNullHeader() {
89-
assertThatIllegalArgumentException().isThrownBy(() ->
90-
MockCookie.parse(null))
89+
assertThatIllegalArgumentException()
90+
.isThrownBy(() -> MockCookie.parse(null))
9191
.withMessageContaining("Set-Cookie header must not be null");
9292
}
9393

9494
@Test
9595
void parseInvalidHeader() {
96-
assertThatIllegalArgumentException().isThrownBy(() ->
97-
MockCookie.parse("BOOM"))
96+
assertThatIllegalArgumentException()
97+
.isThrownBy(() -> MockCookie.parse("BOOM"))
9898
.withMessageContaining("Invalid Set-Cookie header 'BOOM'");
9999
}
100100

101101
@Test
102102
void parseInvalidAttribute() {
103103
String header = "SESSION=123; Path=";
104104

105-
assertThatIllegalArgumentException().isThrownBy(() ->
106-
MockCookie.parse(header))
105+
assertThatIllegalArgumentException()
106+
.isThrownBy(() -> MockCookie.parse(header))
107107
.withMessageContaining("No value in attribute 'Path' for Set-Cookie header '" + header + "'");
108108
}
109109

spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,18 @@ void setCookieHeader() {
350350
assertPrimarySessionCookie("999");
351351
}
352352

353+
/**
354+
* @since 5.1.11
355+
*/
356+
@Test
357+
void setCookieHeaderWithExpiresAttribute() {
358+
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=Tue, 8 Oct 2019 19:50:00 GMT; Secure; " +
359+
"HttpOnly; SameSite=Lax";
360+
response.setHeader(HttpHeaders.SET_COOKIE, cookieValue);
361+
assertNumCookies(1);
362+
assertThat(response.getHeader(HttpHeaders.SET_COOKIE)).isEqualTo(cookieValue);
363+
}
364+
353365
@Test
354366
void addCookieHeader() {
355367
response.addHeader(HttpHeaders.SET_COOKIE, "SESSION=123; Path=/; Secure; HttpOnly; SameSite=Lax");
@@ -363,8 +375,11 @@ void addCookieHeader() {
363375
assertCookieValues("123", "999");
364376
}
365377

378+
/**
379+
* @since 5.1.11
380+
*/
366381
@Test
367-
void addCookieHeaderWithExpires() {
382+
void addCookieHeaderWithExpiresAttribute() {
368383
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=Tue, 8 Oct 2019 19:50:00 GMT; Secure; " +
369384
"HttpOnly; SameSite=Lax";
370385
response.addHeader(HttpHeaders.SET_COOKIE, cookieValue);

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class MockCookie extends Cookie {
4646

4747

4848
/**
49-
* Constructor with the cookie name and value.
49+
* Construct a new {@link MockCookie} with the supplied name and value.
5050
* @param name the name
5151
* @param value the value
5252
* @see Cookie#Cookie(String, String)
@@ -56,33 +56,37 @@ public MockCookie(String name, String value) {
5656
}
5757

5858
/**
59-
* Add the "Expires" attribute to the cookie.
59+
* Set the "Expires" attribute for this cookie.
60+
* @since 5.1.11
6061
*/
6162
public void setExpires(@Nullable ZonedDateTime expires) {
6263
this.expires = expires;
6364
}
6465

6566
/**
66-
* Return the "Expires" attribute, or {@code null} if not set.
67+
* Get the "Expires" attribute for this cookie.
68+
* @since 5.1.11
69+
* @return the "Expires" attribute for this cookie, or {@code null} if not set
6770
*/
6871
@Nullable
6972
public ZonedDateTime getExpires() {
7073
return this.expires;
7174
}
7275

7376
/**
74-
* Add the "SameSite" attribute to the cookie.
77+
* Set the "SameSite" attribute for this cookie.
7578
* <p>This limits the scope of the cookie such that it will only be attached
76-
* to same site requests if {@code "Strict"} or cross-site requests if
77-
* {@code "Lax"}.
79+
* to same-site requests if the supplied value is {@code "Strict"} or cross-site
80+
* requests if the supplied value is {@code "Lax"}.
7881
* @see <a href="https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis#section-4.1.2.7">RFC6265 bis</a>
7982
*/
8083
public void setSameSite(@Nullable String sameSite) {
8184
this.sameSite = sameSite;
8285
}
8386

8487
/**
85-
* Return the "SameSite" attribute, or {@code null} if not set.
88+
* Get the "SameSite" attribute for this cookie.
89+
* @return the "SameSite" attribute for this cookie, or {@code null} if not set
8690
*/
8791
@Nullable
8892
public String getSameSite() {
@@ -91,7 +95,7 @@ public String getSameSite() {
9195

9296

9397
/**
94-
* Factory method that parses the value of a "Set-Cookie" header.
98+
* Factory method that parses the value of the supplied "Set-Cookie" header.
9599
* @param setCookieHeader the "Set-Cookie" value; never {@code null} or empty
96100
* @return the created cookie
97101
*/

0 commit comments

Comments
 (0)