Skip to content

Commit c2ed65c

Browse files
Fix failing tests
Issue gh-9159
1 parent 22ba358 commit c2ed65c

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

Diff for: config/src/test/java/org/springframework/security/config/annotation/web/configurers/HttpSecuritySecurityMatchersNoMvcTests.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.security.config.annotation.web.configurers;
1818

19+
import java.util.List;
20+
1921
import jakarta.servlet.http.HttpServletResponse;
2022
import org.junit.jupiter.api.AfterEach;
2123
import org.junit.jupiter.api.BeforeEach;
@@ -36,6 +38,8 @@
3638
import org.springframework.security.web.FilterChainProxy;
3739
import org.springframework.security.web.SecurityFilterChain;
3840
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
41+
import org.springframework.security.web.util.matcher.RequestMatcher;
42+
import org.springframework.test.util.ReflectionTestUtils;
3943
import org.springframework.web.bind.annotation.RequestMapping;
4044
import org.springframework.web.bind.annotation.RestController;
4145
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
@@ -88,10 +92,12 @@ public void securityMatcherWhenNoMvcThenAntMatcher() throws Exception {
8892
setup();
8993
this.request.setServletPath("/path/");
9094
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
95+
List<RequestMatcher> requestMatchers = this.springSecurityFilterChain.getFilterChains().stream()
96+
.map((chain) -> ((DefaultSecurityFilterChain) chain).getRequestMatcher())
97+
.map((matcher) -> ReflectionTestUtils.getField(matcher, "requestMatchers"))
98+
.map((matchers) -> (List<RequestMatcher>) matchers).findFirst().get();
9199
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
92-
assertThat(this.springSecurityFilterChain.getFilterChains())
93-
.extracting((c) -> ((DefaultSecurityFilterChain) c).getRequestMatcher())
94-
.hasOnlyElementsOfType(AntPathRequestMatcher.class);
100+
assertThat(requestMatchers).hasOnlyElementsOfType(AntPathRequestMatcher.class);
95101
}
96102

97103
public void loadConfig(Class<?>... configs) {

Diff for: config/src/test/java/org/springframework/security/config/annotation/web/configurers/HttpSecuritySecurityMatchersTests.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.security.config.annotation.web.configurers;
1818

19+
import java.util.List;
20+
1921
import jakarta.servlet.http.HttpServletResponse;
2022
import org.junit.jupiter.api.AfterEach;
2123
import org.junit.jupiter.api.BeforeEach;
@@ -41,6 +43,8 @@
4143
import org.springframework.security.web.FilterChainProxy;
4244
import org.springframework.security.web.SecurityFilterChain;
4345
import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher;
46+
import org.springframework.security.web.util.matcher.RequestMatcher;
47+
import org.springframework.test.util.ReflectionTestUtils;
4448
import org.springframework.web.bind.annotation.RequestMapping;
4549
import org.springframework.web.bind.annotation.RestController;
4650
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
@@ -119,10 +123,12 @@ public void securityMatchersWhenMvcThenMvcMatcher() throws Exception {
119123
setup();
120124
this.request.setServletPath("/path/");
121125
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
126+
List<RequestMatcher> requestMatchers = this.springSecurityFilterChain.getFilterChains().stream()
127+
.map((chain) -> ((DefaultSecurityFilterChain) chain).getRequestMatcher())
128+
.map((matcher) -> ReflectionTestUtils.getField(matcher, "requestMatchers"))
129+
.map((matchers) -> (List<RequestMatcher>) matchers).findFirst().get();
122130
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
123-
assertThat(this.springSecurityFilterChain.getFilterChains())
124-
.extracting((c) -> ((DefaultSecurityFilterChain) c).getRequestMatcher())
125-
.hasOnlyElementsOfType(MvcRequestMatcher.class);
131+
assertThat(requestMatchers).hasOnlyElementsOfType(MvcRequestMatcher.class);
126132
}
127133

128134
@Test

0 commit comments

Comments
 (0)