Fix 'spring.resources.cache.period' for WebMvc

Before this change it got overwritten by forwarding an empty
CacheControl to Spring. Spring itself sets CacheSeconds already
correctly in absence (=null) of a CacheControl.

Also:
* Fixes bug in WebMvcAutoConfigurationTests.cachePeriod which
prevented it to assert anything

See gh-16488
Closes gh-16730
This commit is contained in:
Marcus Eisele
2019-05-06 21:17:28 +02:00
committed by Brian Clozel
parent 0b4934d140
commit 94a9748ce0
3 changed files with 8 additions and 5 deletions

View File

@@ -476,6 +476,10 @@ public class ResourceProperties {
.staleIfError(duration.getSeconds(), TimeUnit.SECONDS));
map.from(this::getSMaxAge).whenNonNull().to((duration) -> control
.sMaxAge(duration.getSeconds(), TimeUnit.SECONDS));
// check if cacheControl remained untouched
if (control.getHeaderValue() == null) {
return null;
}
return control;
}

View File

@@ -76,7 +76,7 @@ public class ResourcePropertiesTests {
public void emptyCacheControl() {
CacheControl cacheControl = this.properties.getCache().getCachecontrol()
.toHttpCacheControl();
assertThat(cacheControl.getHeaderValue()).isNull();
assertThat(cacheControl).isNull();
}
@Test

View File

@@ -803,13 +803,12 @@ public class WebMvcAutoConfigurationTests {
Map<String, Object> handlerMap = getHandlerMap(
context.getBean("resourceHandlerMapping", HandlerMapping.class));
assertThat(handlerMap).hasSize(2);
for (Object handler : handlerMap.keySet()) {
for (Object handler : handlerMap.values()) {
if (handler instanceof ResourceHttpRequestHandler) {
assertThat(((ResourceHttpRequestHandler) handler).getCacheSeconds())
.isEqualTo(-1);
.isEqualTo(5);
assertThat(((ResourceHttpRequestHandler) handler).getCacheControl())
.isEqualToComparingFieldByField(
CacheControl.maxAge(5, TimeUnit.SECONDS));
.isNull();
}
}
}