Added extra endpoint for default caching

This commit is contained in:
Andrew Morgan 2017-02-04 23:46:53 +00:00
parent c9394b216a
commit 3f81460e90
3 changed files with 37 additions and 11 deletions

View File

@ -17,6 +17,11 @@ import java.util.concurrent.TimeUnit;
@Controller
public class ResourceEndpoint {
@RequestMapping(value = "/default/users/{name}", method = RequestMethod.GET)
public ResponseEntity<UserDto> getUserWithDefaultCaching(@PathVariable(value = "name") String name) {
return ResponseEntity.ok(new UserDto(name));
}
@RequestMapping(value = "/users/{name}", method = RequestMethod.GET)
public ResponseEntity<UserDto> getUser(@PathVariable(value = "name") String name) {
return ResponseEntity.ok()
@ -24,7 +29,6 @@ public class ResourceEndpoint {
.body(new UserDto(name));
}
@RequestMapping(value = "/timestamp", method = RequestMethod.GET)
public ResponseEntity<TimestampDto> getServerTimestamp() {
return ResponseEntity.ok()

View File

@ -13,9 +13,5 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.headers()
.defaultsDisabled()
.cacheControl();
}
protected void configure(HttpSecurity http) throws Exception {}
}

View File

@ -3,21 +3,43 @@ package com.baeldung.cachecontrol;
import com.jayway.restassured.http.ContentType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static com.jayway.restassured.RestAssured.given;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, classes = AppRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = AppRunner.class)
public class ResourceEndpointLiveTest {
private static final String URL_PREFIX = "http://localhost:8080";
@LocalServerPort
private int serverPort;
@Test
public void whenGetRequestForUser_shouldRespondWithDefaultCacheHeaders() {
given()
.when()
.get(getBaseUrl() + "/default/users/Michael")
.then()
.headers("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate")
.header("Pragma", "no-cache");
}
@Test
public void whenGetRequestForUser_shouldRespondMaxAgeCacheControl() {
given()
.when()
.get(getBaseUrl() + "/users/Michael")
.then()
.header("Cache-Control", "max-age=60");
}
@Test
public void givenServiceEndpoint_whenGetRequestForUser_shouldResponseWithCacheControlMaxAge() {
given()
.when()
.get(URL_PREFIX + "/users/Michael")
.get(getBaseUrl() + "/users/Michael")
.then()
.contentType(ContentType.JSON).and().statusCode(200).and()
.header("Cache-Control", "max-age=60");
@ -27,7 +49,7 @@ public class ResourceEndpointLiveTest {
public void givenServiceEndpoint_whenGetRequestForNotCacheableContent_shouldResponseWithCacheControlNoCache() {
given()
.when()
.get(URL_PREFIX + "/timestamp")
.get(getBaseUrl() + "/timestamp")
.then()
.contentType(ContentType.JSON).and().statusCode(200).and()
.header("Cache-Control", "no-store");
@ -37,10 +59,14 @@ public class ResourceEndpointLiveTest {
public void givenServiceEndpoint_whenGetRequestForPrivateUser_shouldResponseWithSecurityDefaultCacheControl() {
given()
.when()
.get(URL_PREFIX + "/private/users/Michael")
.get(getBaseUrl() + "/private/users/Michael")
.then()
.contentType(ContentType.JSON).and().statusCode(200).and()
.header("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate");
}
private String getBaseUrl() {
return "http://localhost:" + serverPort;
}
}