|
30 | 30 |
|
31 | 31 | import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
32 | 32 | import org.springframework.context.ApplicationContext;
|
| 33 | +import org.springframework.context.annotation.Configuration; |
33 | 34 | import org.springframework.http.HttpMethod;
|
34 | 35 | import org.springframework.mock.web.MockHttpServletRequest;
|
35 | 36 | import org.springframework.security.config.MockServletContext;
|
36 | 37 | import org.springframework.security.config.TestMockHttpServletMappings;
|
37 | 38 | import org.springframework.security.config.annotation.ObjectPostProcessor;
|
38 | 39 | import org.springframework.security.config.annotation.web.AbstractRequestMatcherRegistry.DispatcherServletDelegatingRequestMatcher;
|
| 40 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 41 | +import org.springframework.security.config.test.SpringTestContext; |
39 | 42 | import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher;
|
40 | 43 | import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
41 | 44 | import org.springframework.security.web.util.matcher.DispatcherTypeRequestMatcher;
|
42 | 45 | import org.springframework.security.web.util.matcher.RegexRequestMatcher;
|
43 | 46 | import org.springframework.security.web.util.matcher.RequestMatcher;
|
| 47 | +import org.springframework.test.web.servlet.MockMvc; |
| 48 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
44 | 49 | import org.springframework.web.context.WebApplicationContext;
|
45 | 50 | import org.springframework.web.servlet.DispatcherServlet;
|
| 51 | +import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
46 | 52 |
|
47 | 53 | import static org.assertj.core.api.Assertions.assertThat;
|
48 | 54 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
| 55 | +import static org.assertj.core.api.InstanceOfAssertFactories.type; |
49 | 56 | import static org.mockito.BDDMockito.given;
|
50 | 57 | import static org.mockito.Mockito.mock;
|
51 | 58 | import static org.mockito.Mockito.verify;
|
52 | 59 | import static org.mockito.Mockito.verifyNoInteractions;
|
53 | 60 | import static org.mockito.Mockito.verifyNoMoreInteractions;
|
| 61 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
54 | 62 |
|
55 | 63 | /**
|
56 | 64 | * Tests for {@link AbstractRequestMatcherRegistry}.
|
@@ -206,18 +214,65 @@ public void requestMatchersWhenNoDispatcherServletThenAntPathRequestMatcherType(
|
206 | 214 | mockMvcIntrospector(true);
|
207 | 215 | MockServletContext servletContext = new MockServletContext();
|
208 | 216 | given(this.context.getServletContext()).willReturn(servletContext);
|
| 217 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 218 | + List<RequestMatcher> requestMatchers = this.matcherRegistry.requestMatchers("/**"); |
| 219 | + assertThat(requestMatchers).isNotEmpty(); |
| 220 | + assertThat(requestMatchers).hasSize(1); |
| 221 | + assertThat(requestMatchers.get(0)).asInstanceOf(type(DispatcherServletDelegatingRequestMatcher.class)) |
| 222 | + .extracting((matcher) -> matcher.requestMatcher(request)) |
| 223 | + .isInstanceOf(AntPathRequestMatcher.class); |
209 | 224 | servletContext.addServlet("servletOne", Servlet.class).addMapping("/one");
|
210 | 225 | servletContext.addServlet("servletTwo", Servlet.class).addMapping("/two");
|
211 |
| - List<RequestMatcher> requestMatchers = this.matcherRegistry.requestMatchers("/**"); |
| 226 | + requestMatchers = this.matcherRegistry.requestMatchers("/**"); |
212 | 227 | assertThat(requestMatchers).isNotEmpty();
|
213 | 228 | assertThat(requestMatchers).hasSize(1);
|
214 |
| - assertThat(requestMatchers.get(0)).isExactlyInstanceOf(AntPathRequestMatcher.class); |
| 229 | + assertThat(requestMatchers.get(0)).asInstanceOf(type(DispatcherServletDelegatingRequestMatcher.class)) |
| 230 | + .extracting((matcher) -> matcher.requestMatcher(request)) |
| 231 | + .isInstanceOf(AntPathRequestMatcher.class); |
215 | 232 | servletContext.addServlet("servletOne", Servlet.class);
|
216 | 233 | servletContext.addServlet("servletTwo", Servlet.class);
|
217 | 234 | requestMatchers = this.matcherRegistry.requestMatchers("/**");
|
218 | 235 | assertThat(requestMatchers).isNotEmpty();
|
219 | 236 | assertThat(requestMatchers).hasSize(1);
|
220 |
| - assertThat(requestMatchers.get(0)).isExactlyInstanceOf(AntPathRequestMatcher.class); |
| 237 | + assertThat(requestMatchers.get(0)).asInstanceOf(type(DispatcherServletDelegatingRequestMatcher.class)) |
| 238 | + .extracting((matcher) -> matcher.requestMatcher(request)) |
| 239 | + .isInstanceOf(AntPathRequestMatcher.class); |
| 240 | + } |
| 241 | + |
| 242 | + // gh-14418 |
| 243 | + @Test |
| 244 | + public void requestMatchersWhenNoDispatcherServletMockMvcThenMvcRequestMatcherType() throws Exception { |
| 245 | + MockServletContext servletContext = new MockServletContext(); |
| 246 | + try (SpringTestContext spring = new SpringTestContext(this)) { |
| 247 | + spring.register(MockMvcConfiguration.class) |
| 248 | + .postProcessor((context) -> context.setServletContext(servletContext)) |
| 249 | + .autowire(); |
| 250 | + this.matcherRegistry.setApplicationContext(spring.getContext()); |
| 251 | + MockMvc mvc = MockMvcBuilders.webAppContextSetup(spring.getContext()).build(); |
| 252 | + MockHttpServletRequest request = mvc.perform(get("/")).andReturn().getRequest(); |
| 253 | + List<RequestMatcher> requestMatchers = this.matcherRegistry.requestMatchers("/**"); |
| 254 | + assertThat(requestMatchers).isNotEmpty(); |
| 255 | + assertThat(requestMatchers).hasSize(1); |
| 256 | + assertThat(requestMatchers.get(0)).asInstanceOf(type(DispatcherServletDelegatingRequestMatcher.class)) |
| 257 | + .extracting((matcher) -> matcher.requestMatcher(request)) |
| 258 | + .isInstanceOf(MvcRequestMatcher.class); |
| 259 | + servletContext.addServlet("servletOne", Servlet.class).addMapping("/one"); |
| 260 | + servletContext.addServlet("servletTwo", Servlet.class).addMapping("/two"); |
| 261 | + requestMatchers = this.matcherRegistry.requestMatchers("/**"); |
| 262 | + assertThat(requestMatchers).isNotEmpty(); |
| 263 | + assertThat(requestMatchers).hasSize(1); |
| 264 | + assertThat(requestMatchers.get(0)).asInstanceOf(type(DispatcherServletDelegatingRequestMatcher.class)) |
| 265 | + .extracting((matcher) -> matcher.requestMatcher(request)) |
| 266 | + .isInstanceOf(MvcRequestMatcher.class); |
| 267 | + servletContext.addServlet("servletOne", Servlet.class); |
| 268 | + servletContext.addServlet("servletTwo", Servlet.class); |
| 269 | + requestMatchers = this.matcherRegistry.requestMatchers("/**"); |
| 270 | + assertThat(requestMatchers).isNotEmpty(); |
| 271 | + assertThat(requestMatchers).hasSize(1); |
| 272 | + assertThat(requestMatchers.get(0)).asInstanceOf(type(DispatcherServletDelegatingRequestMatcher.class)) |
| 273 | + .extracting((matcher) -> matcher.requestMatcher(request)) |
| 274 | + .isInstanceOf(MvcRequestMatcher.class); |
| 275 | + } |
221 | 276 | }
|
222 | 277 |
|
223 | 278 | @Test
|
@@ -398,4 +453,11 @@ private List<RequestMatcher> unwrap(List<RequestMatcher> wrappedMatchers) {
|
398 | 453 |
|
399 | 454 | }
|
400 | 455 |
|
| 456 | + @Configuration |
| 457 | + @EnableWebSecurity |
| 458 | + @EnableWebMvc |
| 459 | + static class MockMvcConfiguration { |
| 460 | + |
| 461 | + } |
| 462 | + |
401 | 463 | }
|
0 commit comments