|
16 | 16 |
|
17 | 17 | package org.springframework.security.web.session; |
18 | 18 |
|
| 19 | +import java.util.List; |
| 20 | + |
19 | 21 | import jakarta.servlet.http.HttpServletRequest; |
20 | 22 | import jakarta.servlet.http.HttpSession; |
21 | 23 | import org.junit.jupiter.api.Test; |
@@ -75,14 +77,22 @@ public void newSessionIsCreatedIfSessionAlreadyExistsWithEventPublisher() { |
75 | 77 | assertThat(oldSessionId.equals(request.getSession().getId())).isFalse(); |
76 | 78 | assertThat(request.getSession().getAttribute("blah")).isNotNull(); |
77 | 79 | assertThat(request.getSession().getAttribute("SPRING_SECURITY_SAVED_REQUEST_KEY")).isNotNull(); |
78 | | - assertThat(eventArgumentCaptor.getAllValues().get(0)).isInstanceOf(SessionFixationProtectionEvent.class); |
79 | | - SessionFixationProtectionEvent fixationEvent = (SessionFixationProtectionEvent) eventArgumentCaptor.getAllValues() |
80 | | - .get(0); |
| 80 | + List<ApplicationEvent> events = eventArgumentCaptor.getAllValues(); |
| 81 | + assertThat(events).hasAtLeastOneElementOfType(SessionFixationProtectionEvent.class); |
| 82 | + SessionFixationProtectionEvent fixationEvent = events.stream() |
| 83 | + .filter(SessionFixationProtectionEvent.class::isInstance) |
| 84 | + .map(SessionFixationProtectionEvent.class::cast) |
| 85 | + .findFirst() |
| 86 | + .orElseThrow(); |
81 | 87 | assertThat(fixationEvent.getOldSessionId()).isEqualTo(oldSessionId); |
82 | 88 | assertThat(fixationEvent.getNewSessionId()).isEqualTo(request.getSession().getId()); |
83 | 89 | assertThat(fixationEvent.getAuthentication()).isSameAs(mockAuthentication); |
84 | | - assertThat(eventArgumentCaptor.getAllValues().get(1)).isInstanceOf(SessionIdChangedEvent.class); |
85 | | - SessionIdChangedEvent idChangedEvent = (SessionIdChangedEvent) eventArgumentCaptor.getAllValues().get(1); |
| 90 | + assertThat(events).hasAtLeastOneElementOfType(SessionIdChangedEvent.class); |
| 91 | + SessionIdChangedEvent idChangedEvent = events.stream() |
| 92 | + .filter(SessionIdChangedEvent.class::isInstance) |
| 93 | + .map(SessionIdChangedEvent.class::cast) |
| 94 | + .findFirst() |
| 95 | + .orElseThrow(); |
86 | 96 | assertThat(idChangedEvent.getOldSessionId()).isEqualTo(oldSessionId); |
87 | 97 | assertThat(idChangedEvent.getNewSessionId()).isEqualTo(request.getSession().getId()); |
88 | 98 | } |
@@ -119,14 +129,22 @@ public void onlySavedRequestAttributeIsMigratedIfMigrateAttributesIsFalseWithEve |
119 | 129 | verify(eventPublisher, times(2)).publishEvent(eventArgumentCaptor.capture()); |
120 | 130 | assertThat(request.getSession().getAttribute("blah")).isNull(); |
121 | 131 | assertThat(request.getSession().getAttribute("SPRING_SECURITY_SAVED_REQUEST_KEY")).isNotNull(); |
122 | | - assertThat(eventArgumentCaptor.getAllValues().get(0)).isInstanceOf(SessionFixationProtectionEvent.class); |
123 | | - SessionFixationProtectionEvent fixationEvent = (SessionFixationProtectionEvent) eventArgumentCaptor.getAllValues() |
124 | | - .get(0); |
| 132 | + List<ApplicationEvent> events = eventArgumentCaptor.getAllValues(); |
| 133 | + assertThat(events).hasAtLeastOneElementOfType(SessionFixationProtectionEvent.class); |
| 134 | + SessionFixationProtectionEvent fixationEvent = events.stream() |
| 135 | + .filter(SessionFixationProtectionEvent.class::isInstance) |
| 136 | + .map(SessionFixationProtectionEvent.class::cast) |
| 137 | + .findFirst() |
| 138 | + .orElseThrow(); |
125 | 139 | assertThat(fixationEvent.getOldSessionId()).isEqualTo(oldSessionId); |
126 | 140 | assertThat(fixationEvent.getNewSessionId()).isEqualTo(request.getSession().getId()); |
127 | 141 | assertThat(fixationEvent.getAuthentication()).isSameAs(mockAuthentication); |
128 | | - assertThat(eventArgumentCaptor.getAllValues().get(1)).isInstanceOf(SessionIdChangedEvent.class); |
129 | | - SessionIdChangedEvent idChangedEvent = (SessionIdChangedEvent) eventArgumentCaptor.getAllValues().get(1); |
| 142 | + assertThat(events).hasAtLeastOneElementOfType(SessionIdChangedEvent.class); |
| 143 | + SessionIdChangedEvent idChangedEvent = events.stream() |
| 144 | + .filter(SessionIdChangedEvent.class::isInstance) |
| 145 | + .map(SessionIdChangedEvent.class::cast) |
| 146 | + .findFirst() |
| 147 | + .orElseThrow(); |
130 | 148 | assertThat(idChangedEvent.getOldSessionId()).isEqualTo(oldSessionId); |
131 | 149 | assertThat(idChangedEvent.getNewSessionId()).isEqualTo(request.getSession().getId()); |
132 | 150 | } |
|
0 commit comments