Add Cookie attributes + SameSite CookieResultMatchers in MockMvc

This commit adds assertions to MockMvc's CookieresultMatchers:
 - `attribute` for arbitrary attributes
 - `sameSite` for the SameSite well-known attribute

Note that the `sameSite` methods delegate to their `attribute`
counterparts. Note also that Jakarta's `Cookie#getAttribute` method is
case-insensitive, which is reflected in the documentation of the
`attribute` assertion method and the tests.

Closes gh-30285
This commit is contained in:
Simon Baslé
2023-04-05 17:02:38 +02:00
parent 842490beeb
commit d6460e0d57
3 changed files with 141 additions and 0 deletions

View File

@@ -146,6 +146,24 @@ public class CookieResultMatchers {
};
}
/**
* Assert a cookie's SameSite attribute with a Hamcrest {@link Matcher}.
* @since 6.0.8
* @see #attribute(String, String, Matcher)
*/
public ResultMatcher sameSite(String name, Matcher<? super String> matcher) {
return attribute(name, "SameSite", matcher);
}
/**
* Assert a cookie's SameSite attribute.
* @since 6.0.8
* @see #attribute(String, String, String)
*/
public ResultMatcher sameSite(String name, String sameSite) {
return attribute(name, "SameSite", sameSite);
}
/**
* Assert a cookie's comment with a Hamcrest {@link Matcher}.
*/
@@ -211,6 +229,34 @@ public class CookieResultMatchers {
};
}
/**
* Assert a cookie's specified attribute with a Hamcrest {@link Matcher}.
* @param cookieAttribute the name of the Cookie attribute (case-insensitive)
* @since 6.0.8
*/
public ResultMatcher attribute(String cookieName, String cookieAttribute, Matcher<? super String> matcher) {
return result -> {
Cookie cookie = getCookie(result, cookieName);
String attribute = cookie.getAttribute(cookieAttribute);
assertNotNull("Response cookie '" + cookieName + "' doesn't have attribute '" + cookieAttribute + "'", attribute);
assertThat("Response cookie '" + cookieName + "' attribute '" + cookieAttribute + "'",
attribute, matcher);
};
}
/**
* Assert a cookie's specified attribute.
* @param cookieAttribute the name of the Cookie attribute (case-insensitive)
* @since 6.0.8
*/
public ResultMatcher attribute(String cookieName, String cookieAttribute, String attributeValue) {
return result -> {
Cookie cookie = getCookie(result, cookieName);
assertEquals("Response cookie '" + cookieName + "' attribute '" + cookieAttribute + "'",
attributeValue, cookie.getAttribute(cookieAttribute));
};
}
private static Cookie getCookie(MvcResult result, String name) {
Cookie cookie = result.getResponse().getCookie(name);

View File

@@ -99,6 +99,20 @@ class CookieResultMatchersDsl internal constructor (private val actions: ResultA
actions.andExpect(matchers.domain(name, domain))
}
/**
* @see CookieResultMatchers.sameSite
*/
fun sameSite(name: String, matcher: Matcher<String>) {
actions.andExpect(matchers.sameSite(name, matcher))
}
/**
* @see CookieResultMatchers.sameSite
*/
fun sameSite(name: String, sameSite: String) {
actions.andExpect(matchers.sameSite(name, sameSite))
}
/**
* @see CookieResultMatchers.comment
*/
@@ -140,4 +154,18 @@ class CookieResultMatchersDsl internal constructor (private val actions: ResultA
fun httpOnly(name: String, httpOnly: Boolean) {
actions.andExpect(matchers.httpOnly(name, httpOnly))
}
/**
* @see CookieResultMatchers.attribute
*/
fun attribute(name: String, attributeName: String, matcher: Matcher<String>) {
actions.andExpect(matchers.attribute(name, attributeName, matcher))
}
/**
* @see CookieResultMatchers.attribute
*/
fun attribute(name: String, attributeName: String, attributeValue: String) {
actions.andExpect(matchers.attribute(name, attributeName, attributeValue))
}
}