parent
9b2087618b
commit
ce0b012f43
|
@ -46,7 +46,7 @@ public class MockCookie extends Cookie {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor with the cookie name and value.
|
* Construct a new {@link MockCookie} with the supplied name and value.
|
||||||
* @param name the name
|
* @param name the name
|
||||||
* @param value the value
|
* @param value the value
|
||||||
* @see Cookie#Cookie(String, String)
|
* @see Cookie#Cookie(String, String)
|
||||||
|
@ -56,14 +56,17 @@ public class MockCookie extends Cookie {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add the "Expires" attribute to the cookie.
|
* Set the "Expires" attribute for this cookie.
|
||||||
|
* @since 5.1.11
|
||||||
*/
|
*/
|
||||||
public void setExpires(@Nullable ZonedDateTime expires) {
|
public void setExpires(@Nullable ZonedDateTime expires) {
|
||||||
this.expires = expires;
|
this.expires = expires;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the "Expires" attribute, or {@code null} if not set.
|
* Get the "Expires" attribute for this cookie.
|
||||||
|
* @since 5.1.11
|
||||||
|
* @return the "Expires" attribute for this cookie, or {@code null} if not set
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public ZonedDateTime getExpires() {
|
public ZonedDateTime getExpires() {
|
||||||
|
@ -71,10 +74,10 @@ public class MockCookie extends Cookie {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add the "SameSite" attribute to the cookie.
|
* Set the "SameSite" attribute for this cookie.
|
||||||
* <p>This limits the scope of the cookie such that it will only be attached
|
* <p>This limits the scope of the cookie such that it will only be attached
|
||||||
* to same site requests if {@code "Strict"} or cross-site requests if
|
* to same-site requests if the supplied value is {@code "Strict"} or cross-site
|
||||||
* {@code "Lax"}.
|
* requests if the supplied value is {@code "Lax"}.
|
||||||
* @see <a href="https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis#section-4.1.2.7">RFC6265 bis</a>
|
* @see <a href="https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis#section-4.1.2.7">RFC6265 bis</a>
|
||||||
*/
|
*/
|
||||||
public void setSameSite(@Nullable String sameSite) {
|
public void setSameSite(@Nullable String sameSite) {
|
||||||
|
@ -82,7 +85,8 @@ public class MockCookie extends Cookie {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the "SameSite" attribute, or {@code null} if not set.
|
* Get the "SameSite" attribute for this cookie.
|
||||||
|
* @return the "SameSite" attribute for this cookie, or {@code null} if not set
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public String getSameSite() {
|
public String getSameSite() {
|
||||||
|
@ -91,7 +95,7 @@ public class MockCookie extends Cookie {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Factory method that parses the value of a "Set-Cookie" header.
|
* Factory method that parses the value of the supplied "Set-Cookie" header.
|
||||||
* @param setCookieHeader the "Set-Cookie" value; never {@code null} or empty
|
* @param setCookieHeader the "Set-Cookie" value; never {@code null} or empty
|
||||||
* @return the created cookie
|
* @return the created cookie
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -16,11 +16,11 @@
|
||||||
|
|
||||||
package org.springframework.mock.web;
|
package org.springframework.mock.web;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||||
|
|
||||||
|
@ -86,15 +86,15 @@ class MockCookieTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void parseNullHeader() {
|
void parseNullHeader() {
|
||||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
assertThatIllegalArgumentException()
|
||||||
MockCookie.parse(null))
|
.isThrownBy(() -> MockCookie.parse(null))
|
||||||
.withMessageContaining("Set-Cookie header must not be null");
|
.withMessageContaining("Set-Cookie header must not be null");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void parseInvalidHeader() {
|
void parseInvalidHeader() {
|
||||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
assertThatIllegalArgumentException()
|
||||||
MockCookie.parse("BOOM"))
|
.isThrownBy(() -> MockCookie.parse("BOOM"))
|
||||||
.withMessageContaining("Invalid Set-Cookie header 'BOOM'");
|
.withMessageContaining("Invalid Set-Cookie header 'BOOM'");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,8 +102,8 @@ class MockCookieTests {
|
||||||
void parseInvalidAttribute() {
|
void parseInvalidAttribute() {
|
||||||
String header = "SESSION=123; Path=";
|
String header = "SESSION=123; Path=";
|
||||||
|
|
||||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
assertThatIllegalArgumentException()
|
||||||
MockCookie.parse(header))
|
.isThrownBy(() -> MockCookie.parse(header))
|
||||||
.withMessageContaining("No value in attribute 'Path' for Set-Cookie header '" + header + "'");
|
.withMessageContaining("No value in attribute 'Path' for Set-Cookie header '" + header + "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -350,6 +350,18 @@ class MockHttpServletResponseTests {
|
||||||
assertPrimarySessionCookie("999");
|
assertPrimarySessionCookie("999");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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";
|
||||||
|
response.setHeader(HttpHeaders.SET_COOKIE, cookieValue);
|
||||||
|
assertNumCookies(1);
|
||||||
|
assertThat(response.getHeader(HttpHeaders.SET_COOKIE)).isEqualTo(cookieValue);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void addCookieHeader() {
|
void addCookieHeader() {
|
||||||
response.addHeader(HttpHeaders.SET_COOKIE, "SESSION=123; Path=/; Secure; HttpOnly; SameSite=Lax");
|
response.addHeader(HttpHeaders.SET_COOKIE, "SESSION=123; Path=/; Secure; HttpOnly; SameSite=Lax");
|
||||||
|
@ -363,8 +375,11 @@ class MockHttpServletResponseTests {
|
||||||
assertCookieValues("123", "999");
|
assertCookieValues("123", "999");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 5.1.11
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
void addCookieHeaderWithExpires() {
|
void addCookieHeaderWithExpiresAttribute() {
|
||||||
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=Tue, 8 Oct 2019 19:50:00 GMT; Secure; " +
|
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=Tue, 8 Oct 2019 19:50:00 GMT; Secure; " +
|
||||||
"HttpOnly; SameSite=Lax";
|
"HttpOnly; SameSite=Lax";
|
||||||
response.addHeader(HttpHeaders.SET_COOKIE, cookieValue);
|
response.addHeader(HttpHeaders.SET_COOKIE, cookieValue);
|
||||||
|
|
|
@ -46,7 +46,7 @@ public class MockCookie extends Cookie {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor with the cookie name and value.
|
* Construct a new {@link MockCookie} with the supplied name and value.
|
||||||
* @param name the name
|
* @param name the name
|
||||||
* @param value the value
|
* @param value the value
|
||||||
* @see Cookie#Cookie(String, String)
|
* @see Cookie#Cookie(String, String)
|
||||||
|
@ -56,14 +56,17 @@ public class MockCookie extends Cookie {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add the "Expires" attribute to the cookie.
|
* Set the "Expires" attribute for this cookie.
|
||||||
|
* @since 5.1.11
|
||||||
*/
|
*/
|
||||||
public void setExpires(@Nullable ZonedDateTime expires) {
|
public void setExpires(@Nullable ZonedDateTime expires) {
|
||||||
this.expires = expires;
|
this.expires = expires;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the "Expires" attribute, or {@code null} if not set.
|
* Get the "Expires" attribute for this cookie.
|
||||||
|
* @since 5.1.11
|
||||||
|
* @return the "Expires" attribute for this cookie, or {@code null} if not set
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public ZonedDateTime getExpires() {
|
public ZonedDateTime getExpires() {
|
||||||
|
@ -71,10 +74,10 @@ public class MockCookie extends Cookie {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add the "SameSite" attribute to the cookie.
|
* Set the "SameSite" attribute for this cookie.
|
||||||
* <p>This limits the scope of the cookie such that it will only be attached
|
* <p>This limits the scope of the cookie such that it will only be attached
|
||||||
* to same site requests if {@code "Strict"} or cross-site requests if
|
* to same-site requests if the supplied value is {@code "Strict"} or cross-site
|
||||||
* {@code "Lax"}.
|
* requests if the supplied value is {@code "Lax"}.
|
||||||
* @see <a href="https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis#section-4.1.2.7">RFC6265 bis</a>
|
* @see <a href="https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis#section-4.1.2.7">RFC6265 bis</a>
|
||||||
*/
|
*/
|
||||||
public void setSameSite(@Nullable String sameSite) {
|
public void setSameSite(@Nullable String sameSite) {
|
||||||
|
@ -82,7 +85,8 @@ public class MockCookie extends Cookie {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the "SameSite" attribute, or {@code null} if not set.
|
* Get the "SameSite" attribute for this cookie.
|
||||||
|
* @return the "SameSite" attribute for this cookie, or {@code null} if not set
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public String getSameSite() {
|
public String getSameSite() {
|
||||||
|
@ -91,7 +95,7 @@ public class MockCookie extends Cookie {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Factory method that parses the value of a "Set-Cookie" header.
|
* Factory method that parses the value of the supplied "Set-Cookie" header.
|
||||||
* @param setCookieHeader the "Set-Cookie" value; never {@code null} or empty
|
* @param setCookieHeader the "Set-Cookie" value; never {@code null} or empty
|
||||||
* @return the created cookie
|
* @return the created cookie
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue