mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-22 05:49:04 +00:00
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:
+27
@@ -714,6 +714,33 @@ class MockHttpServletRequestTests {
|
||||
assertThat(listenerEvent.event.getAsyncContext()).isEqualTo(newAsyncContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
void requestedSessionIdValidShouldDefaultToFalse() {
|
||||
assertThat(request.getRequestedSessionId()).isNull();
|
||||
assertThat(request.isRequestedSessionIdValid()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void requestedSessionIdValidShouldReturnTrueWhenSessionValid() {
|
||||
MockHttpSession session = new MockHttpSession();
|
||||
request.setSession(session);
|
||||
request.setRequestedSessionId(session.getId());
|
||||
assertThat(request.getRequestedSessionId()).isEqualTo(session.getId());
|
||||
assertThat(request.isRequestedSessionIdValid()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void requestedSessionIdValidShouldReturnFalseWhenRotated() {
|
||||
MockHttpSession session = new MockHttpSession();
|
||||
request.setSession(session);
|
||||
request.setRequestedSessionId(session.getId());
|
||||
String previousRequestedId = request.getRequestedSessionId();
|
||||
request.changeSessionId();
|
||||
assertThat(session.getId()).isNotEqualTo(previousRequestedId);
|
||||
assertThat(request.getRequestedSessionId()).isEqualTo(previousRequestedId);
|
||||
assertThat(request.isRequestedSessionIdValid()).isFalse();
|
||||
}
|
||||
|
||||
private void assertEqualEnumerations(Enumeration<?> enum1, Enumeration<?> enum2) {
|
||||
int count = 0;
|
||||
while (enum1.hasMoreElements()) {
|
||||
|
||||
Reference in New Issue
Block a user