From c2ed65c67a324c9e4a5cc835c24f1db40a0050bb Mon Sep 17 00:00:00 2001 From: Marcus Da Coregio Date: Wed, 5 Oct 2022 14:59:33 -0300 Subject: [PATCH] Fix failing tests Issue gh-9159 --- .../HttpSecuritySecurityMatchersNoMvcTests.java | 12 +++++++++--- .../HttpSecuritySecurityMatchersTests.java | 12 +++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/config/src/test/java/org/springframework/security/config/annotation/web/configurers/HttpSecuritySecurityMatchersNoMvcTests.java b/config/src/test/java/org/springframework/security/config/annotation/web/configurers/HttpSecuritySecurityMatchersNoMvcTests.java index 5e1e0cf96b..c2566fe0ab 100644 --- a/config/src/test/java/org/springframework/security/config/annotation/web/configurers/HttpSecuritySecurityMatchersNoMvcTests.java +++ b/config/src/test/java/org/springframework/security/config/annotation/web/configurers/HttpSecuritySecurityMatchersNoMvcTests.java @@ -16,6 +16,8 @@ package org.springframework.security.config.annotation.web.configurers; +import java.util.List; + import jakarta.servlet.http.HttpServletResponse; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -36,6 +38,8 @@ import org.springframework.security.web.DefaultSecurityFilterChain; import org.springframework.security.web.FilterChainProxy; import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; +import org.springframework.security.web.util.matcher.RequestMatcher; +import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; @@ -88,10 +92,12 @@ public class HttpSecuritySecurityMatchersNoMvcTests { setup(); this.request.setServletPath("/path/"); this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain); + List requestMatchers = this.springSecurityFilterChain.getFilterChains().stream() + .map((chain) -> ((DefaultSecurityFilterChain) chain).getRequestMatcher()) + .map((matcher) -> ReflectionTestUtils.getField(matcher, "requestMatchers")) + .map((matchers) -> (List) matchers).findFirst().get(); assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_OK); - assertThat(this.springSecurityFilterChain.getFilterChains()) - .extracting((c) -> ((DefaultSecurityFilterChain) c).getRequestMatcher()) - .hasOnlyElementsOfType(AntPathRequestMatcher.class); + assertThat(requestMatchers).hasOnlyElementsOfType(AntPathRequestMatcher.class); } public void loadConfig(Class... configs) { diff --git a/config/src/test/java/org/springframework/security/config/annotation/web/configurers/HttpSecuritySecurityMatchersTests.java b/config/src/test/java/org/springframework/security/config/annotation/web/configurers/HttpSecuritySecurityMatchersTests.java index 18fda34732..0f1fdd15f2 100644 --- a/config/src/test/java/org/springframework/security/config/annotation/web/configurers/HttpSecuritySecurityMatchersTests.java +++ b/config/src/test/java/org/springframework/security/config/annotation/web/configurers/HttpSecuritySecurityMatchersTests.java @@ -16,6 +16,8 @@ package org.springframework.security.config.annotation.web.configurers; +import java.util.List; + import jakarta.servlet.http.HttpServletResponse; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -41,6 +43,8 @@ import org.springframework.security.web.DefaultSecurityFilterChain; import org.springframework.security.web.FilterChainProxy; import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher; +import org.springframework.security.web.util.matcher.RequestMatcher; +import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; @@ -119,10 +123,12 @@ public class HttpSecuritySecurityMatchersTests { setup(); this.request.setServletPath("/path/"); this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain); + List requestMatchers = this.springSecurityFilterChain.getFilterChains().stream() + .map((chain) -> ((DefaultSecurityFilterChain) chain).getRequestMatcher()) + .map((matcher) -> ReflectionTestUtils.getField(matcher, "requestMatchers")) + .map((matchers) -> (List) matchers).findFirst().get(); assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED); - assertThat(this.springSecurityFilterChain.getFilterChains()) - .extracting((c) -> ((DefaultSecurityFilterChain) c).getRequestMatcher()) - .hasOnlyElementsOfType(MvcRequestMatcher.class); + assertThat(requestMatchers).hasOnlyElementsOfType(MvcRequestMatcher.class); } @Test