Browse Source

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 gh-26558
pull/27107/head
Koos Gadellaa 5 years ago committed by Sam Brannen
parent
commit
8f2010d669
  1. 7
      spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java
  2. 11
      spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java

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

@ -374,10 +374,10 @@ public class MockHttpServletResponse implements HttpServletResponse { @@ -374,10 +374,10 @@ public class MockHttpServletResponse implements HttpServletResponse {
buf.append("; Domain=").append(cookie.getDomain());
}
int maxAge = cookie.getMaxAge();
ZonedDateTime expires = (cookie instanceof MockCookie ? ((MockCookie) cookie).getExpires() : null);
if (maxAge >= 0) {
buf.append("; Max-Age=").append(maxAge);
buf.append("; Expires=");
ZonedDateTime expires = (cookie instanceof MockCookie ? ((MockCookie) cookie).getExpires() : null);
if (expires != null) {
buf.append(expires.format(DateTimeFormatter.RFC_1123_DATE_TIME));
}
@ -387,6 +387,11 @@ public class MockHttpServletResponse implements HttpServletResponse { @@ -387,6 +387,11 @@ public class MockHttpServletResponse implements HttpServletResponse {
buf.append(headers.getFirst(HttpHeaders.EXPIRES));
}
}
else if (expires != null) {
buf.append("; Expires=");
buf.append(expires.format(DateTimeFormatter.RFC_1123_DATE_TIME));
}
if (cookie.getSecure()) {
buf.append("; Secure");

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

@ -417,6 +417,17 @@ class MockHttpServletResponseTests { @@ -417,6 +417,17 @@ class MockHttpServletResponseTests {
assertThat(header).startsWith("SESSION=123; Path=/; Max-Age=100; Expires=");
}
/**
* @since 5.1.12
*/
@Test
void addCookieHeaderWithOnlyExpiresAttribute() {
String cookieValue = "SESSION=123; Path=/; Expires=Tue, 8 Oct 2019 19:50:00 GMT";
response.addHeader(SET_COOKIE, cookieValue);
assertNumCookies(1);
assertThat(response.getHeader(SET_COOKIE)).isEqualTo(cookieValue);
}
@Test
void addCookie() {
MockCookie mockCookie = new MockCookie("SESSION", "123");

Loading…
Cancel
Save