Fix mock servlet request behavior with session ids

Prior to this commit,
`MockHttpServletRequest.isRequestedSessionIdValid()` would return `true`
by default and could only be changed manually with a setter. This does
not align with the Servlet spec because of 1) its default value and 2)
it does not react to `changeSessionId()` calls.

This commit fixes that behavior while still allowing "manual" booleans
being set here.

Fixes gh-36631
This commit is contained in:
Brian Clozel
2026-08-27 14:34:49 +02:00
parent 495fd6b3a5
commit 3170dd5714
3 changed files with 47 additions and 4 deletions
@@ -251,7 +251,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
private @Nullable HttpSession session;
private boolean requestedSessionIdValid = true;
private @Nullable Boolean requestedSessionIdValid;
private boolean requestedSessionIdFromCookie = true;
@@ -1352,7 +1352,15 @@ public class MockHttpServletRequest implements HttpServletRequest {
@Override
public boolean isRequestedSessionIdValid() {
return this.requestedSessionIdValid;
if (this.requestedSessionIdValid != null) {
return this.requestedSessionIdValid;
}
String requestedId = getRequestedSessionId();
if (requestedId == null) {
return false;
}
HttpSession currentSession = getSession(false);
return (currentSession != null && requestedId.equals(currentSession.getId()));
}
public void setRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie) {