Skip to content

Commit b8528af

Browse files
Koos Gadellaalxbzmy
Koos Gadellaa
authored andcommitted
Support cookie w/ only Expires attribute in MockHttpServletResponse
Prior to this commit, MockHttpServletResponse only included the Expires attribute in the generated Cookie header if the Max-Age attribute had also been set. This commit supports including the Expires attribute in the generated Cookie Header even when the Max-Age attribute has not been set. Closes spring-projectsgh-26558
1 parent 62796dc commit b8528af

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,10 @@ private String getCookieHeader(Cookie cookie) {
378378
buf.append("; Domain=").append(cookie.getDomain());
379379
}
380380
int maxAge = cookie.getMaxAge();
381+
ZonedDateTime expires = (cookie instanceof MockCookie ? ((MockCookie) cookie).getExpires() : null);
381382
if (maxAge >= 0) {
382383
buf.append("; Max-Age=").append(maxAge);
383384
buf.append("; Expires=");
384-
ZonedDateTime expires = (cookie instanceof MockCookie ? ((MockCookie) cookie).getExpires() : null);
385385
if (expires != null) {
386386
buf.append(expires.format(DateTimeFormatter.RFC_1123_DATE_TIME));
387387
}
@@ -391,6 +391,11 @@ private String getCookieHeader(Cookie cookie) {
391391
buf.append(headers.getFirst(HttpHeaders.EXPIRES));
392392
}
393393
}
394+
else if (expires != null) {
395+
buf.append("; Expires=");
396+
buf.append(expires.format(DateTimeFormatter.RFC_1123_DATE_TIME));
397+
}
398+
394399

395400
if (cookie.getSecure()) {
396401
buf.append("; Secure");

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,17 @@ void addCookieHeaderWithZeroExpiresAttribute() {
475475
assertThat(header).startsWith("SESSION=123; Path=/; Max-Age=100; Expires=");
476476
}
477477

478+
/**
479+
* @since 5.1.12
480+
*/
481+
@Test
482+
void addCookieHeaderWithOnlyExpiresAttribute() {
483+
String cookieValue = "SESSION=123; Path=/; Expires=Tue, 8 Oct 2019 19:50:00 GMT";
484+
response.addHeader(SET_COOKIE, cookieValue);
485+
assertNumCookies(1);
486+
assertThat(response.getHeader(SET_COOKIE)).isEqualTo(cookieValue);
487+
}
488+
478489
@Test
479490
void addCookie() {
480491
MockCookie mockCookie = new MockCookie("SESSION", "123");

0 commit comments

Comments
 (0)