diff --git a/spring-web/src/main/java/org/springframework/http/CacheControl.java b/spring-web/src/main/java/org/springframework/http/CacheControl.java index a78ed947d54..a7b29acda41 100644 --- a/spring-web/src/main/java/org/springframework/http/CacheControl.java +++ b/spring-web/src/main/java/org/springframework/http/CacheControl.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -326,6 +326,8 @@ public class CacheControl { * Add an "immutable" directive. *
This directive indicates that the origin server will not update the * representation of that resource during the freshness lifetime of the response. + * Adding a {@link #maxAge(Duration) max-age} directive is strongly advised + * to enforce the actual freshness lifetime. * @return {@code this}, to facilitate method chaining * @see rfc8246 */ diff --git a/spring-web/src/test/java/org/springframework/http/CacheControlTests.java b/spring-web/src/test/java/org/springframework/http/CacheControlTests.java index 8dfdf91406c..c0066c4fd77 100644 --- a/spring-web/src/test/java/org/springframework/http/CacheControlTests.java +++ b/spring-web/src/test/java/org/springframework/http/CacheControlTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,84 +25,85 @@ import static org.assertj.core.api.Assertions.assertThat; /** + * Tests for {@link CacheControl}. * @author Brian Clozel */ -public class CacheControlTests { +class CacheControlTests { @Test - public void emptyCacheControl() throws Exception { + void emptyCacheControl() { CacheControl cc = CacheControl.empty(); assertThat(cc.getHeaderValue()).isNull(); } @Test - public void maxAge() throws Exception { + void maxAge() { CacheControl cc = CacheControl.maxAge(1, TimeUnit.HOURS); assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600"); } @Test - public void maxAge_duration() throws Exception { + void maxAge_duration() { CacheControl cc = CacheControl.maxAge(Duration.ofHours(1)); assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600"); } @Test - public void maxAgeAndDirectives() throws Exception { + void maxAgeAndDirectives() { CacheControl cc = CacheControl.maxAge(3600, TimeUnit.SECONDS).cachePublic().noTransform(); assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600, no-transform, public"); } @Test - public void maxAgeAndSMaxAge() throws Exception { + void maxAgeAndSMaxAge() { CacheControl cc = CacheControl.maxAge(1, TimeUnit.HOURS).sMaxAge(30, TimeUnit.MINUTES); assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600, s-maxage=1800"); } @Test - public void maxAgeAndSMaxAge_duration() throws Exception { + void maxAgeAndSMaxAge_duration() { CacheControl cc = CacheControl.maxAge(Duration.ofHours(1)).sMaxAge(Duration.ofMinutes(30)); assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600, s-maxage=1800"); } @Test - public void noCachePrivate() throws Exception { + void noCachePrivate() { CacheControl cc = CacheControl.noCache().cachePrivate(); assertThat(cc.getHeaderValue()).isEqualTo("no-cache, private"); } @Test - public void noStore() throws Exception { + void noStore() { CacheControl cc = CacheControl.noStore(); assertThat(cc.getHeaderValue()).isEqualTo("no-store"); } @Test - public void staleIfError() throws Exception { + void staleIfError() { CacheControl cc = CacheControl.maxAge(1, TimeUnit.HOURS).staleIfError(2, TimeUnit.HOURS); assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600, stale-if-error=7200"); } @Test - public void staleIfError_duration() throws Exception { + void staleIfError_duration() { CacheControl cc = CacheControl.maxAge(Duration.ofHours(1)).staleIfError(2, TimeUnit.HOURS); assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600, stale-if-error=7200"); } @Test - public void staleWhileRevalidate() throws Exception { + void staleWhileRevalidate() { CacheControl cc = CacheControl.maxAge(1, TimeUnit.HOURS).staleWhileRevalidate(2, TimeUnit.HOURS); assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600, stale-while-revalidate=7200"); } @Test - public void staleWhileRevalidate_duration() throws Exception { + void staleWhileRevalidate_duration() { CacheControl cc = CacheControl.maxAge(Duration.ofHours(1)).staleWhileRevalidate(2, TimeUnit.HOURS); assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600, stale-while-revalidate=7200"); } @Test - public void immutable() throws Exception { + void immutable() { CacheControl cc = CacheControl.maxAge(Duration.ofHours(1)).immutable(); assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600, immutable"); }