Merge branch '5.1.x'
This commit is contained in:
@@ -333,23 +333,36 @@ class MockHttpServletResponseTests {
|
||||
assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_NOT_FOUND);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 5.1.10
|
||||
*/
|
||||
@Test
|
||||
void setCookieHeaderValid() {
|
||||
response.addHeader(HttpHeaders.SET_COOKIE, "SESSION=123; Path=/; Secure; HttpOnly; SameSite=Lax");
|
||||
Cookie cookie = response.getCookie("SESSION");
|
||||
assertThat(cookie).isNotNull();
|
||||
boolean condition = cookie instanceof MockCookie;
|
||||
assertThat(condition).isTrue();
|
||||
assertThat(cookie.getName()).isEqualTo("SESSION");
|
||||
assertThat(cookie.getValue()).isEqualTo("123");
|
||||
assertThat(cookie.getPath()).isEqualTo("/");
|
||||
assertThat(cookie.getSecure()).isTrue();
|
||||
assertThat(cookie.isHttpOnly()).isTrue();
|
||||
assertThat(((MockCookie) cookie).getSameSite()).isEqualTo("Lax");
|
||||
void setCookieHeader() {
|
||||
response.setHeader(HttpHeaders.SET_COOKIE, "SESSION=123; Path=/; Secure; HttpOnly; SameSite=Lax");
|
||||
assertNumCookies(1);
|
||||
assertPrimarySessionCookie("123");
|
||||
|
||||
// Setting the Set-Cookie header a 2nd time should overwrite the previous value
|
||||
response.setHeader(HttpHeaders.SET_COOKIE, "SESSION=999; Path=/; Secure; HttpOnly; SameSite=Lax");
|
||||
assertNumCookies(1);
|
||||
assertPrimarySessionCookie("999");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addMockCookie() {
|
||||
void addCookieHeader() {
|
||||
response.addHeader(HttpHeaders.SET_COOKIE, "SESSION=123; Path=/; Secure; HttpOnly; SameSite=Lax");
|
||||
assertNumCookies(1);
|
||||
assertPrimarySessionCookie("123");
|
||||
|
||||
// Adding a 2nd cookie header should result in 2 cookies.
|
||||
response.addHeader(HttpHeaders.SET_COOKIE, "SESSION=999; Path=/; Secure; HttpOnly; SameSite=Lax");
|
||||
assertNumCookies(2);
|
||||
assertPrimarySessionCookie("123");
|
||||
assertCookieValues("123", "999");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addCookie() {
|
||||
MockCookie mockCookie = new MockCookie("SESSION", "123");
|
||||
mockCookie.setPath("/");
|
||||
mockCookie.setDomain("example.com");
|
||||
@@ -360,8 +373,35 @@ class MockHttpServletResponseTests {
|
||||
|
||||
response.addCookie(mockCookie);
|
||||
|
||||
assertNumCookies(1);
|
||||
assertThat(response.getHeader(HttpHeaders.SET_COOKIE)).isEqualTo(("SESSION=123; Path=/; Domain=example.com; Max-Age=0; " +
|
||||
"Expires=Thu, 01 Jan 1970 00:00:00 GMT; Secure; HttpOnly; SameSite=Lax"));
|
||||
|
||||
// Adding a 2nd Cookie should result in 2 Cookies.
|
||||
response.addCookie(new MockCookie("SESSION", "999"));
|
||||
assertNumCookies(2);
|
||||
assertCookieValues("123", "999");
|
||||
}
|
||||
|
||||
private void assertNumCookies(int expected) {
|
||||
assertThat(this.response.getCookies()).hasSize(expected);
|
||||
}
|
||||
|
||||
private void assertCookieValues(String... expected) {
|
||||
assertThat(response.getCookies()).extracting(Cookie::getValue).containsExactly(expected);
|
||||
}
|
||||
|
||||
private void assertPrimarySessionCookie(String expectedValue) {
|
||||
Cookie cookie = this.response.getCookie("SESSION");
|
||||
assertThat(cookie).isNotNull();
|
||||
boolean condition = cookie instanceof MockCookie;
|
||||
assertThat(condition).isTrue();
|
||||
assertThat(cookie.getName()).isEqualTo("SESSION");
|
||||
assertThat(cookie.getValue()).isEqualTo(expectedValue);
|
||||
assertThat(cookie.getPath()).isEqualTo("/");
|
||||
assertThat(cookie.getSecure()).isTrue();
|
||||
assertThat(cookie.isHttpOnly()).isTrue();
|
||||
assertThat(((MockCookie) cookie).getSameSite()).isEqualTo("Lax");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user