Commit b7753a1f authored by Brian Clozel's avatar Brian Clozel

Polish

parent e2bc90b6
...@@ -309,14 +309,14 @@ public class WebMvcAutoConfiguration { ...@@ -309,14 +309,14 @@ public class WebMvcAutoConfiguration {
return; return;
} }
Duration cachePeriod = this.resourceProperties.getCachePeriod(); Duration cachePeriod = this.resourceProperties.getCachePeriod();
CacheControl cacheControl = this.resourceProperties.createCacheControl(); CacheControl cacheControl = this.resourceProperties.getCacheControl().toHttpCacheControl();
if (!registry.hasMappingForPattern("/webjars/**")) { if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration( customizeResourceHandlerRegistration(
registry.addResourceHandler("/webjars/**") registry.addResourceHandler("/webjars/**")
.addResourceLocations( .addResourceLocations(
"classpath:/META-INF/resources/webjars/") "classpath:/META-INF/resources/webjars/")
.setCachePeriod(getSeconds(cachePeriod)) .setCachePeriod(getSeconds(cachePeriod))
.setCacheControl(cacheControl)); .setCacheControl(cacheControl));
} }
String staticPathPattern = this.mvcProperties.getStaticPathPattern(); String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) { if (!registry.hasMappingForPattern(staticPathPattern)) {
...@@ -324,8 +324,8 @@ public class WebMvcAutoConfiguration { ...@@ -324,8 +324,8 @@ public class WebMvcAutoConfiguration {
registry.addResourceHandler(staticPathPattern) registry.addResourceHandler(staticPathPattern)
.addResourceLocations(getResourceLocations( .addResourceLocations(getResourceLocations(
this.resourceProperties.getStaticLocations())) this.resourceProperties.getStaticLocations()))
.setCachePeriod(getSeconds(cachePeriod)) .setCachePeriod(getSeconds(cachePeriod))
.setCacheControl(cacheControl)); .setCacheControl(cacheControl));
} }
} }
......
...@@ -16,7 +16,11 @@ ...@@ -16,7 +16,11 @@
package org.springframework.boot.autoconfigure.web; package org.springframework.boot.autoconfigure.web;
import java.time.Duration;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.boot.autoconfigure.web.ResourceProperties.CacheControlProperties; import org.springframework.boot.autoconfigure.web.ResourceProperties.CacheControlProperties;
import org.springframework.boot.testsupport.assertj.Matched; import org.springframework.boot.testsupport.assertj.Matched;
...@@ -34,6 +38,9 @@ public class ResourcePropertiesTests { ...@@ -34,6 +38,9 @@ public class ResourcePropertiesTests {
private final ResourceProperties properties = new ResourceProperties(); private final ResourceProperties properties = new ResourceProperties();
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test @Test
public void resourceChainNoCustomization() { public void resourceChainNoCustomization() {
assertThat(this.properties.getChain().getEnabled()).isNull(); assertThat(this.properties.getChain().getEnabled()).isNull();
...@@ -70,53 +77,43 @@ public class ResourcePropertiesTests { ...@@ -70,53 +77,43 @@ public class ResourcePropertiesTests {
} }
@Test @Test
public void cachePeriod() { public void emptyCacheControl() {
this.properties.setCachePeriod(5); CacheControlProperties cacheControl = new CacheControlProperties();
assertThat(this.properties.createCacheControl().getHeaderValue()) this.properties.setCacheControl(cacheControl);
.isEqualTo("max-age=5"); assertThat(this.properties.getCacheControl().toHttpCacheControl().getHeaderValue()).isNull();
} }
@Test @Test
public void cacheControlAllPropertiesSet() { public void cacheControlAllPropertiesSet() {
CacheControlProperties cacheControl = new CacheControlProperties(); CacheControlProperties cacheControl = new CacheControlProperties();
cacheControl.setMaxAge(Duration.ofSeconds(4));
cacheControl.setCachePrivate(true); cacheControl.setCachePrivate(true);
cacheControl.setCachePublic(true); cacheControl.setCachePublic(true);
cacheControl.setMaxAge(4L);
cacheControl.setMustRevalidate(true); cacheControl.setMustRevalidate(true);
cacheControl.setNoCache(true);
cacheControl.setNoCache(true);
cacheControl.setNoStore(true);
cacheControl.setNoTransform(true); cacheControl.setNoTransform(true);
cacheControl.setProxyRevalidate(true); cacheControl.setProxyRevalidate(true);
cacheControl.setsMaxAge(5L); cacheControl.setsMaxAge(Duration.ofSeconds(5));
cacheControl.setStaleIfError(6L); cacheControl.setStaleIfError(Duration.ofSeconds(6));
cacheControl.setStaleWhileRevalidate(7L); cacheControl.setStaleWhileRevalidate(Duration.ofSeconds(7));
this.properties.setCacheControl(cacheControl); this.properties.setCacheControl(cacheControl);
assertThat(this.properties.createCacheControl().getHeaderValue()).isEqualTo( assertThat(this.properties.getCacheControl().toHttpCacheControl().getHeaderValue()).isEqualTo(
"max-age=4, must-revalidate, no-transform, public, private, proxy-revalidate, s-maxage=5, stale-if-error=6, stale-while-revalidate=7"); "max-age=4, must-revalidate, no-transform, public, private, proxy-revalidate," +
" s-maxage=5, stale-if-error=6, stale-while-revalidate=7");
} }
@Test @Test
public void cacheControlNoPropertiesSet() { public void invalidCacheControlCombination() {
this.properties.setCacheControl(new CacheControlProperties());
assertThat(this.properties.createCacheControl().getHeaderValue()).isNull();
}
@Test
public void cacheControlAndCachePeriodSet() {
CacheControlProperties cacheControl = new CacheControlProperties(); CacheControlProperties cacheControl = new CacheControlProperties();
cacheControl.setMaxAge(12L); cacheControl.setMaxAge(Duration.ofSeconds(4));
cacheControl.setNoStore(true);
this.properties.setCacheControl(cacheControl); this.properties.setCacheControl(cacheControl);
this.properties.setCachePeriod(6); assertThat(this.properties.getCacheControl().toHttpCacheControl().getHeaderValue()).isEqualTo("no-store");
assertThat(this.properties.createCacheControl().getHeaderValue())
.isEqualTo("max-age=6");
} }
@Test @Test
public void cacheControlAndCachePeriodBothNotSet() { public void cacheControlNoPropertiesSet() {
this.properties.setCacheControl(null); this.properties.setCacheControl(new CacheControlProperties());
this.properties.setCachePeriod(null); assertThat(this.properties.getCacheControl().toHttpCacheControl().getHeaderValue()).isNull();
assertThat(this.properties.createCacheControl()).isNull();
} }
} }
...@@ -99,9 +99,7 @@ import org.springframework.web.servlet.resource.VersionStrategy; ...@@ -99,9 +99,7 @@ import org.springframework.web.servlet.resource.VersionStrategy;
import org.springframework.web.servlet.view.AbstractView; import org.springframework.web.servlet.view.AbstractView;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver; import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
...@@ -852,28 +850,6 @@ public class WebMvcAutoConfigurationTests { ...@@ -852,28 +850,6 @@ public class WebMvcAutoConfigurationTests {
} }
} }
@Test
public void invalidCacheConfig() throws Exception {
assertThatThrownBy(() -> this.contextRunner
.withPropertyValues("spring.resources.cache-control.max-age:5",
"spring.resources.cache-period:6")
.run((context) -> getHandlerMap(
context.getBean("resourceHandlerMapping", HandlerMapping.class))))
.hasRootCauseInstanceOf(IllegalStateException.class)
.hasStackTraceContaining("Only one of cache-period or cache-control may be set");
}
@Test
public void invalidCacheControl() throws Exception {
assertThatThrownBy(() -> this.contextRunner
.withPropertyValues("spring.resources.cache-control.max-age:5",
"spring.resources.cache-control.no-cache:true")
.run((context) -> getHandlerMap(
context.getBean("resourceHandlerMapping", HandlerMapping.class))))
.hasRootCauseInstanceOf(IllegalStateException.class)
.hasStackTraceContaining("no-cache may not be set if max-age is set");
}
protected Map<String, List<Resource>> getFaviconMappingLocations( protected Map<String, List<Resource>> getFaviconMappingLocations(
ApplicationContext context) { ApplicationContext context) {
return getMappingLocations( return getMappingLocations(
......
...@@ -398,6 +398,17 @@ content into your application; rather pick only the properties that you need. ...@@ -398,6 +398,17 @@ content into your application; rather pick only the properties that you need.
# SPRING RESOURCES HANDLING ({sc-spring-boot-autoconfigure}/web/ResourceProperties.{sc-ext}[ResourceProperties]) # SPRING RESOURCES HANDLING ({sc-spring-boot-autoconfigure}/web/ResourceProperties.{sc-ext}[ResourceProperties])
spring.resources.add-mappings=true # Enable default resource handling. spring.resources.add-mappings=true # Enable default resource handling.
spring.resources.cache-control.max-age= # Maximum time the response should be cached, in seconds if no duration suffix is not specified.
spring.resources.cache-control.no-cache= # Indicate that the cached response can be reused only if re-validated with the server.
spring.resources.cache-control.no-store= # Indicate to not cache the response in any case.
spring.resources.cache-control.must-revalidate= # Indicate that once it has become stale, a cache must not use the response without re-validating it with the server.
spring.resources.cache-control.no-transform= # Indicate intermediaries (caches and others) that they should not transform the response content.
spring.resources.cache-control.cache-public= # Indicate that any cache may store the response.
spring.resources.cache-control.cache-private= # Indicate that the response message is intended for a single user and must not be stored by a shared cache.
spring.resources.cache-control.proxy-revalidate= # Same meaning as the "must-revalidate" directive, except that it does not apply to private caches.
spring.resources.cache-control.stale-while-revalidate= # Maximum time the response can be served after it becomes stale, in seconds if no duration suffix is not specified.
spring.resources.cache-control.stale-if-error= # Maximum time the response may be used when errors are encountered, in seconds if no duration suffix is not specified.
spring.resources.cache-control.s-max-age= # Maximum time the response should be cached by shared caches, in seconds if no duration suffix is not specified.
spring.resources.cache-period= # Cache period for the resources served by the resource handler. If a duration suffix is not specified, seconds will be used. spring.resources.cache-period= # Cache period for the resources served by the resource handler. If a duration suffix is not specified, seconds will be used.
spring.resources.chain.cache=true # Enable caching in the Resource chain. spring.resources.chain.cache=true # Enable caching in the Resource chain.
spring.resources.chain.enabled= # Enable the Spring Resource Handling chain. Disabled by default unless at least one strategy has been enabled. spring.resources.chain.enabled= # Enable the Spring Resource Handling chain. Disabled by default unless at least one strategy has been enabled.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment