Skip to content

Commit d4f7ad3

Browse files
sbrannenlxbzmy
authored andcommitted
Polish MockHttpServletResponseTests
See spring-projectsgh-26558
1 parent 6d35d01 commit d4f7ad3

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,6 @@ else if (expires != null) {
396396
buf.append(expires.format(DateTimeFormatter.RFC_1123_DATE_TIME));
397397
}
398398

399-
400399
if (cookie.getSecure()) {
401400
buf.append("; Secure");
402401
}

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

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.io.IOException;
2020
import java.nio.charset.StandardCharsets;
21+
import java.time.ZonedDateTime;
22+
import java.time.format.DateTimeFormatter;
2123
import java.util.Arrays;
2224
import java.util.Collection;
2325
import java.util.Locale;
@@ -415,12 +417,17 @@ void setCookieHeader() {
415417
* @since 5.1.11
416418
*/
417419
@Test
418-
void setCookieHeaderWithExpiresAttribute() {
419-
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=Tue, 8 Oct 2019 19:50:00 GMT; Secure; " +
420-
"HttpOnly; SameSite=Lax";
420+
void setCookieHeaderWithMaxAgeAndExpiresAttributes() {
421+
String expiryDate = "Tue, 8 Oct 2019 19:50:00 GMT";
422+
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=" + expiryDate + "; Secure; HttpOnly; SameSite=Lax";
421423
response.setHeader(SET_COOKIE, cookieValue);
422-
assertNumCookies(1);
423424
assertThat(response.getHeader(SET_COOKIE)).isEqualTo(cookieValue);
425+
426+
assertNumCookies(1);
427+
assertThat(response.getCookies()[0]).isInstanceOf(MockCookie.class);
428+
MockCookie mockCookie = (MockCookie) response.getCookies()[0];
429+
assertThat(mockCookie.getMaxAge()).isEqualTo(100);
430+
assertThat(mockCookie.getExpires()).isEqualTo(ZonedDateTime.parse(expiryDate, DateTimeFormatter.RFC_1123_DATE_TIME));
424431
}
425432

426433
/**
@@ -454,18 +461,24 @@ void addCookieHeader() {
454461
* @since 5.1.11
455462
*/
456463
@Test
457-
void addCookieHeaderWithExpiresAttribute() {
458-
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=Tue, 8 Oct 2019 19:50:00 GMT; Secure; " +
459-
"HttpOnly; SameSite=Lax";
464+
void addCookieHeaderWithMaxAgeAndExpiresAttributes() {
465+
String expiryDate = "Tue, 8 Oct 2019 19:50:00 GMT";
466+
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=" + expiryDate + "; Secure; HttpOnly; SameSite=Lax";
460467
response.addHeader(SET_COOKIE, cookieValue);
461468
assertThat(response.getHeader(SET_COOKIE)).isEqualTo(cookieValue);
469+
470+
assertNumCookies(1);
471+
assertThat(response.getCookies()[0]).isInstanceOf(MockCookie.class);
472+
MockCookie mockCookie = (MockCookie) response.getCookies()[0];
473+
assertThat(mockCookie.getMaxAge()).isEqualTo(100);
474+
assertThat(mockCookie.getExpires()).isEqualTo(ZonedDateTime.parse(expiryDate, DateTimeFormatter.RFC_1123_DATE_TIME));
462475
}
463476

464477
/**
465478
* @since 5.1.12
466479
*/
467480
@Test
468-
void addCookieHeaderWithZeroExpiresAttribute() {
481+
void addCookieHeaderWithMaxAgeAndZeroExpiresAttributes() {
469482
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=0";
470483
response.addHeader(SET_COOKIE, cookieValue);
471484
assertNumCookies(1);
@@ -476,14 +489,24 @@ void addCookieHeaderWithZeroExpiresAttribute() {
476489
}
477490

478491
/**
479-
* @since 5.1.12
492+
* @since 5.2.14
480493
*/
481494
@Test
482-
void addCookieHeaderWithOnlyExpiresAttribute() {
483-
String cookieValue = "SESSION=123; Path=/; Expires=Tue, 8 Oct 2019 19:50:00 GMT";
495+
void addCookieHeaderWithExpiresAttributeWithoutMaxAgeAttribute() {
496+
String expiryDate = "Tue, 8 Oct 2019 19:50:00 GMT";
497+
String cookieValue = "SESSION=123; Path=/; Expires=" + expiryDate;
484498
response.addHeader(SET_COOKIE, cookieValue);
485-
assertNumCookies(1);
499+
System.err.println(response.getCookie("SESSION"));
486500
assertThat(response.getHeader(SET_COOKIE)).isEqualTo(cookieValue);
501+
502+
assertNumCookies(1);
503+
assertThat(response.getCookies()[0]).isInstanceOf(MockCookie.class);
504+
MockCookie mockCookie = (MockCookie) response.getCookies()[0];
505+
assertThat(mockCookie.getName()).isEqualTo("SESSION");
506+
assertThat(mockCookie.getValue()).isEqualTo("123");
507+
assertThat(mockCookie.getPath()).isEqualTo("/");
508+
assertThat(mockCookie.getMaxAge()).isEqualTo(-1);
509+
assertThat(mockCookie.getExpires()).isEqualTo(ZonedDateTime.parse(expiryDate, DateTimeFormatter.RFC_1123_DATE_TIME));
487510
}
488511

489512
@Test

0 commit comments

Comments
 (0)