Provide controller level Cache-Control support
Prior to this commit, Cache-Control HTTP headers could be set using
a WebContentInterceptor and configured cache mappings.
This commit adds support for cache-related HTTP headers at the controller
method level, by returning a ResponseEntity instance:
ResponseEntity.status(HttpStatus.OK)
.cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic())
.eTag("deadb33f8badf00d")
.body(entity);
Also, this change now automatically checks the "ETag" and
"Last-Modified" headers in ResponseEntity, in order to respond HTTP
"304 - Not Modified" if necessary.
Issue: SPR-8550
This commit is contained in:
@@ -19,11 +19,14 @@ package org.springframework.http;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
* @author Marcel Overdijk
|
||||
@@ -163,7 +166,7 @@ public class ResponseEntityTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headersCopy(){
|
||||
public void headersCopy() {
|
||||
HttpHeaders customHeaders = new HttpHeaders();
|
||||
customHeaders.set("X-CustomHeader", "vale");
|
||||
|
||||
@@ -178,7 +181,7 @@ public class ResponseEntityTests {
|
||||
}
|
||||
|
||||
@Test // SPR-12792
|
||||
public void headersCopyWithEmptyAndNull(){
|
||||
public void headersCopyWithEmptyAndNull() {
|
||||
ResponseEntity<Void> responseEntityWithEmptyHeaders =
|
||||
ResponseEntity.ok().headers(new HttpHeaders()).build();
|
||||
ResponseEntity<Void> responseEntityWithNullHeaders =
|
||||
@@ -189,4 +192,58 @@ public class ResponseEntityTests {
|
||||
assertEquals(responseEntityWithEmptyHeaders.toString(), responseEntityWithNullHeaders.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyCacheControl() {
|
||||
|
||||
Integer entity = new Integer(42);
|
||||
|
||||
ResponseEntity<Integer> responseEntity =
|
||||
ResponseEntity.status(HttpStatus.OK)
|
||||
.cacheControl(CacheControl.empty())
|
||||
.body(entity);
|
||||
|
||||
assertNotNull(responseEntity);
|
||||
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
|
||||
assertFalse(responseEntity.getHeaders().containsKey(HttpHeaders.CACHE_CONTROL));
|
||||
assertEquals(entity, responseEntity.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cacheControl() {
|
||||
|
||||
Integer entity = new Integer(42);
|
||||
|
||||
ResponseEntity<Integer> responseEntity =
|
||||
ResponseEntity.status(HttpStatus.OK)
|
||||
.cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePrivate().
|
||||
mustRevalidate().proxyRevalidate().sMaxAge(30, TimeUnit.MINUTES))
|
||||
.body(entity);
|
||||
|
||||
assertNotNull(responseEntity);
|
||||
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
|
||||
assertTrue(responseEntity.getHeaders().containsKey(HttpHeaders.CACHE_CONTROL));
|
||||
assertEquals(entity, responseEntity.getBody());
|
||||
String cacheControlHeader = responseEntity.getHeaders().getCacheControl();
|
||||
assertThat(cacheControlHeader, Matchers.equalTo("max-age=3600, must-revalidate, private, proxy-revalidate, s-maxage=1800"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cacheControlNoCache() {
|
||||
|
||||
Integer entity = new Integer(42);
|
||||
|
||||
ResponseEntity<Integer> responseEntity =
|
||||
ResponseEntity.status(HttpStatus.OK)
|
||||
.cacheControl(CacheControl.noStore())
|
||||
.body(entity);
|
||||
|
||||
assertNotNull(responseEntity);
|
||||
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
|
||||
assertTrue(responseEntity.getHeaders().containsKey(HttpHeaders.CACHE_CONTROL));
|
||||
assertEquals(entity, responseEntity.getBody());
|
||||
|
||||
String cacheControlHeader = responseEntity.getHeaders().getCacheControl();
|
||||
assertThat(cacheControlHeader, Matchers.equalTo("no-store"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user