Polishing CacheControl behavior

This commit builds on the previous commit and ensures that
"must-understand" is only used with "no-store". This check is performed
at runtime as a staged interface/builder would be a major breaking
change for a behavior that is highlighted as "SHOULD" in the
specification.

This commit also performs similar runtime checks for:
* cache-public + cache-private
* cache-public + no-store

See gh-36918
This commit is contained in:
Brian Clozel
2026-08-28 15:49:57 +02:00
parent d9e240b37d
commit 5d6a56fe4a
2 changed files with 31 additions and 2 deletions
@@ -22,6 +22,7 @@ import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
@@ -84,6 +85,22 @@ class CacheControlTests {
assertThat(cc.getHeaderValue()).isEqualTo("no-store, must-understand");
}
@Test
void mustUnderstandWithoutNoStoreRejected() {
assertThatIllegalStateException().isThrownBy(() -> CacheControl.maxAge(1, TimeUnit.HOURS).mustUnderstand());
}
@Test
void cachePublicAndCachePrivateRejected() {
assertThatIllegalStateException().isThrownBy(() -> CacheControl.empty().cachePrivate().cachePublic());
assertThatIllegalStateException().isThrownBy(() -> CacheControl.empty().cachePublic().cachePrivate());
}
@Test
void cachePublicWithNoStoreRejected() {
assertThatIllegalStateException().isThrownBy(() -> CacheControl.noStore().cachePublic());
}
@Test
void staleIfError() {
CacheControl cc = CacheControl.maxAge(1, TimeUnit.HOURS).staleIfError(2, TimeUnit.HOURS);