|
28 | 28 | import java.util.List;
|
29 | 29 | import java.util.Locale;
|
30 | 30 | import java.util.Map;
|
31 |
| -import java.util.Map.Entry; |
32 | 31 | import java.util.concurrent.Executor;
|
33 | 32 | import java.util.concurrent.TimeUnit;
|
34 | 33 | import java.util.function.Consumer;
|
@@ -754,26 +753,25 @@ void httpMessageConverterThatUsesConversionServiceDoesNotCreateACycle() {
|
754 | 753 | @ParameterizedTest
|
755 | 754 | @ValueSource(strings = { "spring.resources.", "spring.web.resources." })
|
756 | 755 | void cachePeriod(String prefix) {
|
757 |
| - this.contextRunner.withPropertyValues(prefix + "cache.period:5").run(this::assertCachePeriod); |
758 |
| - } |
759 |
| - |
760 |
| - private void assertCachePeriod(AssertableWebApplicationContext context) { |
761 |
| - Map<String, Object> handlerMap = getHandlerMap(context.getBean("resourceHandlerMapping", HandlerMapping.class)); |
762 |
| - assertThat(handlerMap).hasSize(2); |
763 |
| - for (Entry<String, Object> entry : handlerMap.entrySet()) { |
764 |
| - Object handler = entry.getValue(); |
765 |
| - if (handler instanceof ResourceHttpRequestHandler) { |
766 |
| - assertThat(((ResourceHttpRequestHandler) handler).getCacheSeconds()).isEqualTo(5); |
767 |
| - assertThat(((ResourceHttpRequestHandler) handler).getCacheControl()).isNull(); |
768 |
| - } |
769 |
| - } |
| 756 | + this.contextRunner.withPropertyValues(prefix + "cache.period:5").run((context) -> { |
| 757 | + assertResourceHttpRequestHandler((context), (handler) -> { |
| 758 | + assertThat(handler.getCacheSeconds()).isEqualTo(5); |
| 759 | + assertThat(handler.getCacheControl()).isNull(); |
| 760 | + }); |
| 761 | + }); |
770 | 762 | }
|
771 | 763 |
|
772 | 764 | @ParameterizedTest
|
773 | 765 | @ValueSource(strings = { "spring.resources.", "spring.web.resources." })
|
774 | 766 | void cacheControl(String prefix) {
|
775 |
| - this.contextRunner.withPropertyValues(prefix + "cache.cachecontrol.max-age:5", |
776 |
| - prefix + "cache.cachecontrol.proxy-revalidate:true").run(this::assertCacheControl); |
| 767 | + this.contextRunner |
| 768 | + .withPropertyValues(prefix + "cache.cachecontrol.max-age:5", |
| 769 | + prefix + "cache.cachecontrol.proxy-revalidate:true") |
| 770 | + .run((context) -> assertResourceHttpRequestHandler(context, (handler) -> { |
| 771 | + assertThat(handler.getCacheSeconds()).isEqualTo(-1); |
| 772 | + assertThat(handler.getCacheControl()).usingRecursiveComparison() |
| 773 | + .isEqualTo(CacheControl.maxAge(5, TimeUnit.SECONDS).proxyRevalidate()); |
| 774 | + })); |
777 | 775 | }
|
778 | 776 |
|
779 | 777 | @Test
|
@@ -939,14 +937,20 @@ void urlPathHelperDoesNotUseFullPathWithAdditionalUntypedDispatcherServlet() {
|
939 | 937 | });
|
940 | 938 | }
|
941 | 939 |
|
942 |
| - private void assertCacheControl(AssertableWebApplicationContext context) { |
| 940 | + @Test |
| 941 | + void lastModifiedNotUsedIfDisabled() { |
| 942 | + this.contextRunner.withPropertyValues("spring.web.resources.cache.use-last-modified=false") |
| 943 | + .run((context) -> assertResourceHttpRequestHandler(context, |
| 944 | + (handler) -> assertThat(handler.isUseLastModified()).isFalse())); |
| 945 | + } |
| 946 | + |
| 947 | + private void assertResourceHttpRequestHandler(AssertableWebApplicationContext context, |
| 948 | + Consumer<ResourceHttpRequestHandler> handlerConsumer) { |
943 | 949 | Map<String, Object> handlerMap = getHandlerMap(context.getBean("resourceHandlerMapping", HandlerMapping.class));
|
944 | 950 | assertThat(handlerMap).hasSize(2);
|
945 | 951 | for (Object handler : handlerMap.keySet()) {
|
946 | 952 | if (handler instanceof ResourceHttpRequestHandler) {
|
947 |
| - assertThat(((ResourceHttpRequestHandler) handler).getCacheSeconds()).isEqualTo(-1); |
948 |
| - assertThat(((ResourceHttpRequestHandler) handler).getCacheControl()).usingRecursiveComparison() |
949 |
| - .isEqualTo(CacheControl.maxAge(5, TimeUnit.SECONDS).proxyRevalidate()); |
| 953 | + handlerConsumer.accept((ResourceHttpRequestHandler) handler); |
950 | 954 | }
|
951 | 955 | }
|
952 | 956 | }
|
|
0 commit comments