See gh-29955
This commit is contained in:
Brian Clozel 2023-02-14 17:08:03 +01:00
parent 616e7779e0
commit 5335778d8d
2 changed files with 19 additions and 16 deletions

View File

@ -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.
* <p>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 <a href="https://tools.ietf.org/html/rfc8246">rfc8246</a>
*/

View File

@ -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");
}