pull/14817/merge
Brian Clozel 6 years ago
parent 9e5f171510
commit d5236b91ce

@ -148,17 +148,11 @@ public class WebFluxAutoConfiguration {
logger.debug("Default resource handling disabled");
return;
}
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
ResourceProperties.Cache.Cachecontrol cacheControl = this.resourceProperties
.getCache().getCachecontrol();
if (!registry.hasMappingForPattern("/webjars/**")) {
ResourceHandlerRegistration registration = registry
.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
if (cachePeriod != null && cacheControl.getMaxAge() == null) {
cacheControl.setMaxAge(cachePeriod);
}
registration.setCacheControl(cacheControl.toHttpCacheControl());
configureResourceCaching(registration);
customizeResourceHandlerRegistration(registration);
}
String staticPathPattern = this.webFluxProperties.getStaticPathPattern();
@ -166,14 +160,21 @@ public class WebFluxAutoConfiguration {
ResourceHandlerRegistration registration = registry
.addResourceHandler(staticPathPattern).addResourceLocations(
this.resourceProperties.getStaticLocations());
if (cachePeriod != null && cacheControl.getMaxAge() == null) {
cacheControl.setMaxAge(cachePeriod);
}
registration.setCacheControl(cacheControl.toHttpCacheControl());
configureResourceCaching(registration);
customizeResourceHandlerRegistration(registration);
}
}
private void configureResourceCaching(ResourceHandlerRegistration registration) {
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
ResourceProperties.Cache.Cachecontrol cacheControl = this.resourceProperties
.getCache().getCachecontrol();
if (cachePeriod != null && cacheControl.getMaxAge() == null) {
cacheControl.setMaxAge(cachePeriod);
}
registration.setCacheControl(cacheControl.toHttpCacheControl());
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
this.viewResolvers.orderedStream().forEach(registry::viewResolver);

@ -31,10 +31,10 @@ import org.springframework.beans.DirectFieldAccessor;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration;
import org.springframework.boot.autoconfigure.validation.ValidatorAdapter;
import org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext;
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
import org.springframework.boot.web.codec.CodecCustomizer;
import org.springframework.boot.web.reactive.filter.OrderedHiddenHttpMethodFilter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@ -415,7 +415,17 @@ public class WebFluxAutoConfigurationTests {
@Test
public void cachePeriod() {
this.contextRunner.withPropertyValues("spring.resources.cache.period:5")
.run(this::assertCachePeriod);
.run((context) -> {
Map<PathPattern, Object> handlerMap = getHandlerMap(context);
assertThat(handlerMap).hasSize(2);
for (Object handler : handlerMap.values()) {
if (handler instanceof ResourceWebHandler) {
assertThat(((ResourceWebHandler) handler).getCacheControl())
.isEqualToComparingFieldByField(
CacheControl.maxAge(5, TimeUnit.SECONDS));
}
}
});
}
@Test
@ -423,42 +433,29 @@ public class WebFluxAutoConfigurationTests {
this.contextRunner
.withPropertyValues("spring.resources.cache.cachecontrol.max-age:5",
"spring.resources.cache.cachecontrol.proxy-revalidate:true")
.run(this::assertCacheControl);
}
private void assertCachePeriod(AssertableReactiveWebApplicationContext context) {
Map<PathPattern, Object> handlerMap = getHandlerMap(
context.getBean("resourceHandlerMapping", HandlerMapping.class));
assertThat(handlerMap).hasSize(2);
for (Object handler : handlerMap.values()) {
if (handler instanceof ResourceWebHandler) {
assertThat(((ResourceWebHandler) handler).getCacheControl())
.isEqualToComparingFieldByField(
CacheControl.maxAge(5, TimeUnit.SECONDS));
}
}
.run((context) -> {
Map<PathPattern, Object> handlerMap = getHandlerMap(context);
assertThat(handlerMap).hasSize(2);
for (Object handler : handlerMap.values()) {
if (handler instanceof ResourceWebHandler) {
assertThat(((ResourceWebHandler) handler).getCacheControl())
.isEqualToComparingFieldByField(
CacheControl.maxAge(5, TimeUnit.SECONDS)
.proxyRevalidate());
}
}
});
}
private Map<PathPattern, Object> getHandlerMap(HandlerMapping mapping) {
private Map<PathPattern, Object> getHandlerMap(ApplicationContext context) {
HandlerMapping mapping = context.getBean("resourceHandlerMapping",
HandlerMapping.class);
if (mapping instanceof SimpleUrlHandlerMapping) {
return ((SimpleUrlHandlerMapping) mapping).getHandlerMap();
}
return Collections.emptyMap();
}
private void assertCacheControl(AssertableReactiveWebApplicationContext context) {
Map<PathPattern, Object> handlerMap = getHandlerMap(
context.getBean("resourceHandlerMapping", HandlerMapping.class));
assertThat(handlerMap).hasSize(2);
for (Object handler : handlerMap.values()) {
if (handler instanceof ResourceWebHandler) {
assertThat(((ResourceWebHandler) handler).getCacheControl())
.isEqualToComparingFieldByField(CacheControl
.maxAge(5, TimeUnit.SECONDS).proxyRevalidate());
}
}
}
@Configuration
protected static class CustomArgumentResolvers {

Loading…
Cancel
Save