Skip to content

Commit

Permalink
Polish MockHttpServletResponseTests
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Feb 17, 2021
1 parent 5b97c47 commit 49ccd7a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,6 @@ else if (expires != null) {
buf.append(expires.format(DateTimeFormatter.RFC_1123_DATE_TIME));
}


if (cookie.getSecure()) {
buf.append("; Secure");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;
Expand Down Expand Up @@ -415,12 +417,17 @@ void setCookieHeader() {
* @since 5.1.11
*/
@Test
void setCookieHeaderWithExpiresAttribute() {
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=Tue, 8 Oct 2019 19:50:00 GMT; Secure; " +
"HttpOnly; SameSite=Lax";
void setCookieHeaderWithMaxAgeAndExpiresAttributes() {
String expiryDate = "Tue, 8 Oct 2019 19:50:00 GMT";
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=" + expiryDate + "; Secure; HttpOnly; SameSite=Lax";
response.setHeader(SET_COOKIE, cookieValue);
assertNumCookies(1);
assertThat(response.getHeader(SET_COOKIE)).isEqualTo(cookieValue);

assertNumCookies(1);
assertThat(response.getCookies()[0]).isInstanceOf(MockCookie.class);
MockCookie mockCookie = (MockCookie) response.getCookies()[0];
assertThat(mockCookie.getMaxAge()).isEqualTo(100);
assertThat(mockCookie.getExpires()).isEqualTo(ZonedDateTime.parse(expiryDate, DateTimeFormatter.RFC_1123_DATE_TIME));
}

/**
Expand Down Expand Up @@ -454,18 +461,24 @@ void addCookieHeader() {
* @since 5.1.11
*/
@Test
void addCookieHeaderWithExpiresAttribute() {
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=Tue, 8 Oct 2019 19:50:00 GMT; Secure; " +
"HttpOnly; SameSite=Lax";
void addCookieHeaderWithMaxAgeAndExpiresAttributes() {
String expiryDate = "Tue, 8 Oct 2019 19:50:00 GMT";
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=" + expiryDate + "; Secure; HttpOnly; SameSite=Lax";
response.addHeader(SET_COOKIE, cookieValue);
assertThat(response.getHeader(SET_COOKIE)).isEqualTo(cookieValue);

assertNumCookies(1);
assertThat(response.getCookies()[0]).isInstanceOf(MockCookie.class);
MockCookie mockCookie = (MockCookie) response.getCookies()[0];
assertThat(mockCookie.getMaxAge()).isEqualTo(100);
assertThat(mockCookie.getExpires()).isEqualTo(ZonedDateTime.parse(expiryDate, DateTimeFormatter.RFC_1123_DATE_TIME));
}

/**
* @since 5.1.12
*/
@Test
void addCookieHeaderWithZeroExpiresAttribute() {
void addCookieHeaderWithMaxAgeAndZeroExpiresAttributes() {
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=0";
response.addHeader(SET_COOKIE, cookieValue);
assertNumCookies(1);
Expand All @@ -476,14 +489,24 @@ void addCookieHeaderWithZeroExpiresAttribute() {
}

/**
* @since 5.1.12
* @since 5.2.14
*/
@Test
void addCookieHeaderWithOnlyExpiresAttribute() {
String cookieValue = "SESSION=123; Path=/; Expires=Tue, 8 Oct 2019 19:50:00 GMT";
void addCookieHeaderWithExpiresAttributeWithoutMaxAgeAttribute() {
String expiryDate = "Tue, 8 Oct 2019 19:50:00 GMT";
String cookieValue = "SESSION=123; Path=/; Expires=" + expiryDate;
response.addHeader(SET_COOKIE, cookieValue);
assertNumCookies(1);
System.err.println(response.getCookie("SESSION"));
assertThat(response.getHeader(SET_COOKIE)).isEqualTo(cookieValue);

assertNumCookies(1);
assertThat(response.getCookies()[0]).isInstanceOf(MockCookie.class);
MockCookie mockCookie = (MockCookie) response.getCookies()[0];
assertThat(mockCookie.getName()).isEqualTo("SESSION");
assertThat(mockCookie.getValue()).isEqualTo("123");
assertThat(mockCookie.getPath()).isEqualTo("/");
assertThat(mockCookie.getMaxAge()).isEqualTo(-1);
assertThat(mockCookie.getExpires()).isEqualTo(ZonedDateTime.parse(expiryDate, DateTimeFormatter.RFC_1123_DATE_TIME));
}

@Test
Expand Down

0 comments on commit 49ccd7a

Please sign in to comment.