|
119 | 119 | import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer; |
120 | 120 | import org.springframework.web.servlet.config.annotation.CorsRegistry; |
121 | 121 | import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
| 122 | +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration; |
122 | 123 | import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; |
123 | 124 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
124 | 125 | import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver; |
|
154 | 155 | import static org.assertj.core.api.Assertions.assertThat; |
155 | 156 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
156 | 157 | import static org.mockito.ArgumentMatchers.any; |
| 158 | +import static org.mockito.BDDMockito.then; |
157 | 159 | import static org.mockito.Mockito.inOrder; |
158 | 160 | import static org.mockito.Mockito.mock; |
| 161 | +import static org.mockito.Mockito.times; |
159 | 162 |
|
160 | 163 | /** |
161 | 164 | * Tests for {@link WebMvcAutoConfiguration}. |
@@ -265,6 +268,31 @@ void resourceHandlerChainEnabled() { |
265 | 268 | }); |
266 | 269 | } |
267 | 270 |
|
| 271 | + @Test |
| 272 | + void shouldCustomizeResources() { |
| 273 | + this.contextRunner.withUserConfiguration(ResourceHandlerRegistrationCustomizers.class).run((context) -> { |
| 274 | + ResourceHandlerRegistrationCustomizer customizer1 = context |
| 275 | + .getBean("firstResourceHandlerRegistrationCustomizer", ResourceHandlerRegistrationCustomizer.class); |
| 276 | + ResourceHandlerRegistrationCustomizer customizer2 = context |
| 277 | + .getBean("secondResourceHandlerRegistrationCustomizer", ResourceHandlerRegistrationCustomizer.class); |
| 278 | + then(customizer1).should(times(2)).customize(any(ResourceHandlerRegistration.class)); |
| 279 | + then(customizer2).should(times(2)).customize(any(ResourceHandlerRegistration.class)); |
| 280 | + }); |
| 281 | + } |
| 282 | + |
| 283 | + @Test |
| 284 | + void shouldCustomizeResourcesWhenResourceChainIsEnabled() { |
| 285 | + this.contextRunner.withUserConfiguration(ResourceHandlerRegistrationCustomizerConfiguration.class) |
| 286 | + .withPropertyValues("spring.web.resources.chain.enabled:true") |
| 287 | + .run((context) -> { |
| 288 | + ResourceHandlerRegistrationCustomizer customizer = context.getBean( |
| 289 | + "customResourceHandlerRegistrationCustomizer", ResourceHandlerRegistrationCustomizer.class); |
| 290 | + then(customizer).should(times(2)).customize(any(ResourceHandlerRegistration.class)); |
| 291 | + assertThat(getResourceResolvers(context, "/**")).extractingResultOf("getClass") |
| 292 | + .containsOnly(CachingResourceResolver.class, PathResourceResolver.class); |
| 293 | + }); |
| 294 | + } |
| 295 | + |
268 | 296 | @Test |
269 | 297 | void resourceHandlerFixedStrategyEnabled() { |
270 | 298 | this.contextRunner |
@@ -1267,6 +1295,31 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) { |
1267 | 1295 |
|
1268 | 1296 | } |
1269 | 1297 |
|
| 1298 | + @Configuration(proxyBeanMethods = false) |
| 1299 | + static class ResourceHandlerRegistrationCustomizers { |
| 1300 | + |
| 1301 | + @Bean |
| 1302 | + ResourceHandlerRegistrationCustomizer firstResourceHandlerRegistrationCustomizer() { |
| 1303 | + return mock(ResourceHandlerRegistrationCustomizer.class); |
| 1304 | + } |
| 1305 | + |
| 1306 | + @Bean |
| 1307 | + ResourceHandlerRegistrationCustomizer secondResourceHandlerRegistrationCustomizer() { |
| 1308 | + return mock(ResourceHandlerRegistrationCustomizer.class); |
| 1309 | + } |
| 1310 | + |
| 1311 | + } |
| 1312 | + |
| 1313 | + @Configuration(proxyBeanMethods = false) |
| 1314 | + static class ResourceHandlerRegistrationCustomizerConfiguration { |
| 1315 | + |
| 1316 | + @Bean |
| 1317 | + ResourceHandlerRegistrationCustomizer customResourceHandlerRegistrationCustomizer() { |
| 1318 | + return mock(ResourceHandlerRegistrationCustomizer.class); |
| 1319 | + } |
| 1320 | + |
| 1321 | + } |
| 1322 | + |
1270 | 1323 | @Configuration(proxyBeanMethods = false) |
1271 | 1324 | static class Config { |
1272 | 1325 |
|
|
0 commit comments