|
26 | 26 |
|
27 | 27 | import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
28 | 28 | import org.springframework.context.ApplicationContext;
|
| 29 | +import org.springframework.context.annotation.Configuration; |
29 | 30 | import org.springframework.http.HttpMethod;
|
30 | 31 | import org.springframework.mock.web.MockHttpServletRequest;
|
31 | 32 | import org.springframework.security.config.MockServletContext;
|
32 | 33 | import org.springframework.security.config.TestMockHttpServletMappings;
|
33 | 34 | import org.springframework.security.config.annotation.ObjectPostProcessor;
|
34 | 35 | import org.springframework.security.config.annotation.web.AbstractRequestMatcherRegistry.DispatcherServletDelegatingRequestMatcher;
|
| 36 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 37 | +import org.springframework.security.config.test.SpringTestContext; |
35 | 38 | import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher;
|
36 | 39 | import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
37 | 40 | import org.springframework.security.web.util.matcher.DispatcherTypeRequestMatcher;
|
38 | 41 | import org.springframework.security.web.util.matcher.RegexRequestMatcher;
|
39 | 42 | import org.springframework.security.web.util.matcher.RequestMatcher;
|
| 43 | +import org.springframework.test.web.servlet.MockMvc; |
| 44 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
40 | 45 | import org.springframework.web.context.WebApplicationContext;
|
41 | 46 | import org.springframework.web.servlet.DispatcherServlet;
|
| 47 | +import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
42 | 48 |
|
43 | 49 | import static org.assertj.core.api.Assertions.assertThat;
|
44 | 50 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
| 51 | +import static org.assertj.core.api.InstanceOfAssertFactories.type; |
45 | 52 | import static org.mockito.BDDMockito.given;
|
46 | 53 | import static org.mockito.Mockito.mock;
|
47 | 54 | import static org.mockito.Mockito.verify;
|
48 | 55 | import static org.mockito.Mockito.verifyNoInteractions;
|
49 | 56 | import static org.mockito.Mockito.verifyNoMoreInteractions;
|
| 57 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
50 | 58 |
|
51 | 59 | /**
|
52 | 60 | * Tests for {@link AbstractRequestMatcherRegistry}.
|
@@ -167,18 +175,65 @@ public void requestMatchersWhenNoDispatcherServletThenAntPathRequestMatcherType(
|
167 | 175 | mockMvcIntrospector(true);
|
168 | 176 | MockServletContext servletContext = new MockServletContext();
|
169 | 177 | given(this.context.getServletContext()).willReturn(servletContext);
|
| 178 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 179 | + List<RequestMatcher> requestMatchers = this.matcherRegistry.requestMatchers("/**"); |
| 180 | + assertThat(requestMatchers).isNotEmpty(); |
| 181 | + assertThat(requestMatchers).hasSize(1); |
| 182 | + assertThat(requestMatchers.get(0)).asInstanceOf(type(DispatcherServletDelegatingRequestMatcher.class)) |
| 183 | + .extracting((matcher) -> matcher.requestMatcher(request)) |
| 184 | + .isInstanceOf(AntPathRequestMatcher.class); |
170 | 185 | servletContext.addServlet("servletOne", Servlet.class).addMapping("/one");
|
171 | 186 | servletContext.addServlet("servletTwo", Servlet.class).addMapping("/two");
|
172 |
| - List<RequestMatcher> requestMatchers = this.matcherRegistry.requestMatchers("/**"); |
| 187 | + requestMatchers = this.matcherRegistry.requestMatchers("/**"); |
173 | 188 | assertThat(requestMatchers).isNotEmpty();
|
174 | 189 | assertThat(requestMatchers).hasSize(1);
|
175 |
| - assertThat(requestMatchers.get(0)).isExactlyInstanceOf(AntPathRequestMatcher.class); |
| 190 | + assertThat(requestMatchers.get(0)).asInstanceOf(type(DispatcherServletDelegatingRequestMatcher.class)) |
| 191 | + .extracting((matcher) -> matcher.requestMatcher(request)) |
| 192 | + .isInstanceOf(AntPathRequestMatcher.class); |
176 | 193 | servletContext.addServlet("servletOne", Servlet.class);
|
177 | 194 | servletContext.addServlet("servletTwo", Servlet.class);
|
178 | 195 | requestMatchers = this.matcherRegistry.requestMatchers("/**");
|
179 | 196 | assertThat(requestMatchers).isNotEmpty();
|
180 | 197 | assertThat(requestMatchers).hasSize(1);
|
181 |
| - assertThat(requestMatchers.get(0)).isExactlyInstanceOf(AntPathRequestMatcher.class); |
| 198 | + assertThat(requestMatchers.get(0)).asInstanceOf(type(DispatcherServletDelegatingRequestMatcher.class)) |
| 199 | + .extracting((matcher) -> matcher.requestMatcher(request)) |
| 200 | + .isInstanceOf(AntPathRequestMatcher.class); |
| 201 | + } |
| 202 | + |
| 203 | + // gh-14418 |
| 204 | + @Test |
| 205 | + public void requestMatchersWhenNoDispatcherServletMockMvcThenMvcRequestMatcherType() throws Exception { |
| 206 | + MockServletContext servletContext = new MockServletContext(); |
| 207 | + try (SpringTestContext spring = new SpringTestContext(this)) { |
| 208 | + spring.register(MockMvcConfiguration.class) |
| 209 | + .postProcessor((context) -> context.setServletContext(servletContext)) |
| 210 | + .autowire(); |
| 211 | + this.matcherRegistry.setApplicationContext(spring.getContext()); |
| 212 | + MockMvc mvc = MockMvcBuilders.webAppContextSetup(spring.getContext()).build(); |
| 213 | + MockHttpServletRequest request = mvc.perform(get("/")).andReturn().getRequest(); |
| 214 | + List<RequestMatcher> requestMatchers = this.matcherRegistry.requestMatchers("/**"); |
| 215 | + assertThat(requestMatchers).isNotEmpty(); |
| 216 | + assertThat(requestMatchers).hasSize(1); |
| 217 | + assertThat(requestMatchers.get(0)).asInstanceOf(type(DispatcherServletDelegatingRequestMatcher.class)) |
| 218 | + .extracting((matcher) -> matcher.requestMatcher(request)) |
| 219 | + .isInstanceOf(MvcRequestMatcher.class); |
| 220 | + servletContext.addServlet("servletOne", Servlet.class).addMapping("/one"); |
| 221 | + servletContext.addServlet("servletTwo", Servlet.class).addMapping("/two"); |
| 222 | + requestMatchers = this.matcherRegistry.requestMatchers("/**"); |
| 223 | + assertThat(requestMatchers).isNotEmpty(); |
| 224 | + assertThat(requestMatchers).hasSize(1); |
| 225 | + assertThat(requestMatchers.get(0)).asInstanceOf(type(DispatcherServletDelegatingRequestMatcher.class)) |
| 226 | + .extracting((matcher) -> matcher.requestMatcher(request)) |
| 227 | + .isInstanceOf(MvcRequestMatcher.class); |
| 228 | + servletContext.addServlet("servletOne", Servlet.class); |
| 229 | + servletContext.addServlet("servletTwo", Servlet.class); |
| 230 | + requestMatchers = this.matcherRegistry.requestMatchers("/**"); |
| 231 | + assertThat(requestMatchers).isNotEmpty(); |
| 232 | + assertThat(requestMatchers).hasSize(1); |
| 233 | + assertThat(requestMatchers.get(0)).asInstanceOf(type(DispatcherServletDelegatingRequestMatcher.class)) |
| 234 | + .extracting((matcher) -> matcher.requestMatcher(request)) |
| 235 | + .isInstanceOf(MvcRequestMatcher.class); |
| 236 | + } |
182 | 237 | }
|
183 | 238 |
|
184 | 239 | @Test
|
@@ -320,4 +375,11 @@ private List<RequestMatcher> unwrap(List<RequestMatcher> wrappedMatchers) {
|
320 | 375 |
|
321 | 376 | }
|
322 | 377 |
|
| 378 | + @Configuration |
| 379 | + @EnableWebSecurity |
| 380 | + @EnableWebMvc |
| 381 | + static class MockMvcConfiguration { |
| 382 | + |
| 383 | + } |
| 384 | + |
323 | 385 | }
|
0 commit comments