Introduce sessionAttributeDoesNotExist in RequestResultMatchers

Analogous to the attributeDoesNotExist() method in ModelResultMatchers,
this commit introduces a new sessionAttributeDoesNotExist() method in
RequestResultMatchers which asserts that the given attributes are null
in the HttpSession.

Closes gh-23756
This commit is contained in:
Drummond Dawson
2019-10-03 20:38:23 -04:00
committed by Sam Brannen
parent e6f92ae2af
commit e73344fc71
2 changed files with 20 additions and 0 deletions

View File

@@ -31,6 +31,7 @@ import org.springframework.web.context.request.async.WebAsyncTask;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.springframework.test.util.AssertionErrors.assertEquals;
import static org.springframework.test.util.AssertionErrors.assertTrue;
/**
* Factory for assertions on the request.
@@ -150,6 +151,19 @@ public class RequestResultMatchers {
};
}
/**
* Assert the given session attributes do not exist.
*/
public <T> ResultMatcher sessionAttributeDoesNotExist(String... names) {
return result -> {
HttpSession session = result.getRequest().getSession();
Assert.state(session != null, "No HttpSession");
for (String name : names) {
assertTrue("Session attribute '" + name + "' exists", session.getAttribute(name) == null);
}
};
}
private static void assertAsyncStarted(HttpServletRequest request) {
assertEquals("Async started", true, request.isAsyncStarted());
}