Skip to content

Commit 7926560

Browse files
committed
Polish MockHttpServletResponseTests
See spring-projectsgh-26558
1 parent 33add33 commit 7926560

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
@@ -392,7 +392,6 @@ else if (expires != null) {
392392
buf.append(expires.format(DateTimeFormatter.RFC_1123_DATE_TIME));
393393
}
394394

395-
396395
if (cookie.getSecure()) {
397396
buf.append("; Secure");
398397
}

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

@@ -357,12 +359,17 @@ void setCookieHeader() {
357359
* @since 5.1.11
358360
*/
359361
@Test
360-
void setCookieHeaderWithExpiresAttribute() {
361-
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=Tue, 8 Oct 2019 19:50:00 GMT; Secure; " +
362-
"HttpOnly; SameSite=Lax";
362+
void setCookieHeaderWithMaxAgeAndExpiresAttributes() {
363+
String expiryDate = "Tue, 8 Oct 2019 19:50:00 GMT";
364+
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=" + expiryDate + "; Secure; HttpOnly; SameSite=Lax";
363365
response.setHeader(SET_COOKIE, cookieValue);
364-
assertNumCookies(1);
365366
assertThat(response.getHeader(SET_COOKIE)).isEqualTo(cookieValue);
367+
368+
assertNumCookies(1);
369+
assertThat(response.getCookies()[0]).isInstanceOf(MockCookie.class);
370+
MockCookie mockCookie = (MockCookie) response.getCookies()[0];
371+
assertThat(mockCookie.getMaxAge()).isEqualTo(100);
372+
assertThat(mockCookie.getExpires()).isEqualTo(ZonedDateTime.parse(expiryDate, DateTimeFormatter.RFC_1123_DATE_TIME));
366373
}
367374

368375
/**
@@ -396,18 +403,24 @@ void addCookieHeader() {
396403
* @since 5.1.11
397404
*/
398405
@Test
399-
void addCookieHeaderWithExpiresAttribute() {
400-
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=Tue, 8 Oct 2019 19:50:00 GMT; Secure; " +
401-
"HttpOnly; SameSite=Lax";
406+
void addCookieHeaderWithMaxAgeAndExpiresAttributes() {
407+
String expiryDate = "Tue, 8 Oct 2019 19:50:00 GMT";
408+
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=" + expiryDate + "; Secure; HttpOnly; SameSite=Lax";
402409
response.addHeader(SET_COOKIE, cookieValue);
403410
assertThat(response.getHeader(SET_COOKIE)).isEqualTo(cookieValue);
411+
412+
assertNumCookies(1);
413+
assertThat(response.getCookies()[0]).isInstanceOf(MockCookie.class);
414+
MockCookie mockCookie = (MockCookie) response.getCookies()[0];
415+
assertThat(mockCookie.getMaxAge()).isEqualTo(100);
416+
assertThat(mockCookie.getExpires()).isEqualTo(ZonedDateTime.parse(expiryDate, DateTimeFormatter.RFC_1123_DATE_TIME));
404417
}
405418

406419
/**
407420
* @since 5.1.12
408421
*/
409422
@Test
410-
void addCookieHeaderWithZeroExpiresAttribute() {
423+
void addCookieHeaderWithMaxAgeAndZeroExpiresAttributes() {
411424
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=0";
412425
response.addHeader(SET_COOKIE, cookieValue);
413426
assertNumCookies(1);
@@ -418,14 +431,24 @@ void addCookieHeaderWithZeroExpiresAttribute() {
418431
}
419432

420433
/**
421-
* @since 5.1.12
434+
* @since 5.2.14
422435
*/
423436
@Test
424-
void addCookieHeaderWithOnlyExpiresAttribute() {
425-
String cookieValue = "SESSION=123; Path=/; Expires=Tue, 8 Oct 2019 19:50:00 GMT";
437+
void addCookieHeaderWithExpiresAttributeWithoutMaxAgeAttribute() {
438+
String expiryDate = "Tue, 8 Oct 2019 19:50:00 GMT";
439+
String cookieValue = "SESSION=123; Path=/; Expires=" + expiryDate;
426440
response.addHeader(SET_COOKIE, cookieValue);
427-
assertNumCookies(1);
441+
System.err.println(response.getCookie("SESSION"));
428442
assertThat(response.getHeader(SET_COOKIE)).isEqualTo(cookieValue);
443+
444+
assertNumCookies(1);
445+
assertThat(response.getCookies()[0]).isInstanceOf(MockCookie.class);
446+
MockCookie mockCookie = (MockCookie) response.getCookies()[0];
447+
assertThat(mockCookie.getName()).isEqualTo("SESSION");
448+
assertThat(mockCookie.getValue()).isEqualTo("123");
449+
assertThat(mockCookie.getPath()).isEqualTo("/");
450+
assertThat(mockCookie.getMaxAge()).isEqualTo(-1);
451+
assertThat(mockCookie.getExpires()).isEqualTo(ZonedDateTime.parse(expiryDate, DateTimeFormatter.RFC_1123_DATE_TIME));
429452
}
430453

431454
@Test

0 commit comments

Comments
 (0)