|
16 | 16 |
|
17 | 17 | package org.springframework.security.web.authentication; |
18 | 18 |
|
| 19 | +import java.io.IOException; |
19 | 20 | import java.util.Collections; |
20 | 21 | import java.util.LinkedHashMap; |
21 | 22 | import java.util.List; |
22 | 23 |
|
| 24 | +import jakarta.servlet.ServletException; |
23 | 25 | import jakarta.servlet.http.HttpServletRequest; |
24 | 26 | import org.junit.jupiter.api.BeforeEach; |
25 | 27 | import org.junit.jupiter.api.Test; |
|
29 | 31 | import org.springframework.security.web.util.matcher.RequestMatcher; |
30 | 32 | import org.springframework.security.web.util.matcher.RequestMatcherEntry; |
31 | 33 |
|
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
32 | 35 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 36 | +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
| 37 | +import static org.mockito.ArgumentMatchers.any; |
33 | 38 | import static org.mockito.BDDMockito.given; |
34 | 39 | import static org.mockito.Mockito.mock; |
35 | 40 | import static org.mockito.Mockito.never; |
36 | 41 | import static org.mockito.Mockito.verify; |
| 42 | +import static org.mockito.Mockito.verifyNoInteractions; |
37 | 43 |
|
38 | 44 | /** |
39 | 45 | * Test class for {@link DelegatingAuthenticationEntryPoint} |
@@ -202,4 +208,49 @@ public void commenceWhenSecondMatchesThenDefaultNotInvoked() throws Exception { |
202 | 208 | verify(this.defaultEntryPoint, never()).commence(this.request, null, null); |
203 | 209 | } |
204 | 210 |
|
| 211 | + @Test |
| 212 | + void builderWhenDefaultNullAndSingleEntryPointThenReturnsSingle() { |
| 213 | + AuthenticationEntryPoint entryPoint = mock(AuthenticationEntryPoint.class); |
| 214 | + |
| 215 | + AuthenticationEntryPoint result = DelegatingAuthenticationEntryPoint.builder() |
| 216 | + .addEntryPointFor(entryPoint, mock(RequestMatcher.class)) |
| 217 | + .build(); |
| 218 | + |
| 219 | + assertThat(result).isEqualTo(entryPoint); |
| 220 | + } |
| 221 | + |
| 222 | + @Test |
| 223 | + void builderWhenDefaultNullThenFirstIsDefault() throws ServletException, IOException { |
| 224 | + AuthenticationEntryPoint firstEntryPoint = mock(AuthenticationEntryPoint.class); |
| 225 | + AuthenticationEntryPoint secondEntryPoint = mock(AuthenticationEntryPoint.class); |
| 226 | + RequestMatcher neverMatch = mock(RequestMatcher.class); |
| 227 | + given(neverMatch.matches(this.request)).willReturn(false); |
| 228 | + AuthenticationEntryPoint result = DelegatingAuthenticationEntryPoint.builder() |
| 229 | + .addEntryPointFor(firstEntryPoint, neverMatch) |
| 230 | + .addEntryPointFor(secondEntryPoint, neverMatch) |
| 231 | + .build(); |
| 232 | + |
| 233 | + result.commence(this.request, null, null); |
| 234 | + |
| 235 | + verify(firstEntryPoint).commence(any(), any(), any()); |
| 236 | + verifyNoInteractions(secondEntryPoint); |
| 237 | + } |
| 238 | + |
| 239 | + @Test |
| 240 | + void builderWhenDefaultAndEmptyEntryPointsThenReturnsDefault() { |
| 241 | + AuthenticationEntryPoint defaultEntryPoint = mock(AuthenticationEntryPoint.class); |
| 242 | + |
| 243 | + AuthenticationEntryPoint result = DelegatingAuthenticationEntryPoint.builder() |
| 244 | + .defaultEntryPoint(defaultEntryPoint) |
| 245 | + .build(); |
| 246 | + |
| 247 | + assertThat(result).isEqualTo(defaultEntryPoint); |
| 248 | + } |
| 249 | + |
| 250 | + @Test |
| 251 | + void builderWhenNoEntryPointsThenIllegalStateException() { |
| 252 | + DelegatingAuthenticationEntryPoint.Builder builder = DelegatingAuthenticationEntryPoint.builder(); |
| 253 | + assertThatIllegalStateException().isThrownBy(builder::build); |
| 254 | + } |
| 255 | + |
205 | 256 | } |
0 commit comments