diff --git a/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/ResourceEndpoint.java b/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/ResourceEndpoint.java index f1c9786c68..9f756b5ab4 100644 --- a/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/ResourceEndpoint.java +++ b/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/ResourceEndpoint.java @@ -17,6 +17,11 @@ import java.util.concurrent.TimeUnit; @Controller public class ResourceEndpoint { + @RequestMapping(value = "/default/users/{name}", method = RequestMethod.GET) + public ResponseEntity getUserWithDefaultCaching(@PathVariable(value = "name") String name) { + return ResponseEntity.ok(new UserDto(name)); + } + @RequestMapping(value = "/users/{name}", method = RequestMethod.GET) public ResponseEntity 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 getServerTimestamp() { return ResponseEntity.ok() diff --git a/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/config/SpringSecurityConfig.java b/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/config/SpringSecurityConfig.java index fbb6399c22..b4127e9b71 100644 --- a/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/config/SpringSecurityConfig.java +++ b/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/config/SpringSecurityConfig.java @@ -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 {} } diff --git a/spring-security-cache-control/src/test/java/com/baeldung/cachecontrol/ResourceEndpointLiveTest.java b/spring-security-cache-control/src/test/java/com/baeldung/cachecontrol/ResourceEndpointLiveTest.java index 0c23b1969d..94b6052ba4 100644 --- a/spring-security-cache-control/src/test/java/com/baeldung/cachecontrol/ResourceEndpointLiveTest.java +++ b/spring-security-cache-control/src/test/java/com/baeldung/cachecontrol/ResourceEndpointLiveTest.java @@ -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; + } + } \ No newline at end of file