Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,16 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
return;
}
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache()
.getCachecontrol().toHttpCacheControl();
if (!registry.hasMappingForPattern("/webjars/**")) {
ResourceHandlerRegistration registration = registry
.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
if (cachePeriod != null) {
registration.setCacheControl(CacheControl
.maxAge(cachePeriod.toMillis(), TimeUnit.MILLISECONDS));
cacheControl.sMaxAge(cachePeriod.toMillis(), TimeUnit.MILLISECONDS);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sMaxAge looks like a typo here - if the current tests are not breaking with that, they're probably not testing it right.

}
registration.setCacheControl(cacheControl);
customizeResourceHandlerRegistration(registration);
}
String staticPathPattern = this.webFluxProperties.getStaticPathPattern();
Expand All @@ -165,9 +167,9 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
.addResourceHandler(staticPathPattern).addResourceLocations(
this.resourceProperties.getStaticLocations());
if (cachePeriod != null) {
registration.setCacheControl(CacheControl
.maxAge(cachePeriod.toMillis(), TimeUnit.MILLISECONDS));
cacheControl.sMaxAge(cachePeriod.toMillis(), TimeUnit.MILLISECONDS);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same typo

}
registration.setCacheControl(cacheControl);
customizeResourceHandlerRegistration(registration);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

package org.springframework.boot.autoconfigure.web.reactive;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.validation.ValidatorFactory;

Expand All @@ -28,6 +31,7 @@
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;
Expand All @@ -38,6 +42,7 @@
import org.springframework.core.annotation.Order;
import org.springframework.core.io.ClassPathResource;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.http.CacheControl;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.test.util.ReflectionTestUtils;
Expand All @@ -58,6 +63,8 @@
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.reactive.result.view.ViewResolutionResultHandler;
import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
import org.springframework.web.util.pattern.PathPattern;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -398,6 +405,57 @@ public void multipleWebFluxRegistrations() {
});
}

@Test
public void cachePeriod() {
this.contextRunner.withPropertyValues("spring.resources.cache.period:5")
.run(this::assertCachePeriod);
}

@Test
public void cacheControl() {
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.keySet()) {
if (handler instanceof ResourceHttpRequestHandler) {
assertThat(((ResourceHttpRequestHandler) handler).getCacheSeconds())
.isEqualTo(-1);
assertThat(((ResourceHttpRequestHandler) handler).getCacheControl())
.isEqualToComparingFieldByField(
CacheControl.maxAge(5, TimeUnit.SECONDS));
}
}
}

private Map<PathPattern, Object> getHandlerMap(HandlerMapping mapping) {
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.keySet()) {
if (handler instanceof ResourceHttpRequestHandler) {
assertThat(((ResourceHttpRequestHandler) handler).getCacheSeconds())
.isEqualTo(-1);
assertThat(((ResourceHttpRequestHandler) handler).getCacheControl())
.isEqualToComparingFieldByField(CacheControl
.maxAge(5, TimeUnit.SECONDS).proxyRevalidate());
}
}
}

@Configuration
protected static class CustomArgumentResolvers {

Expand Down