Skip to content

Commit 3f81460

Browse files
committed
Added extra endpoint for default caching
1 parent c9394b2 commit 3f81460

3 files changed

Lines changed: 37 additions & 11 deletions

File tree

‎spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/ResourceEndpoint.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@
1717
@Controller
1818
public class ResourceEndpoint {
1919

20+
@RequestMapping(value = "/default/users/{name}", method = RequestMethod.GET)
21+
public ResponseEntity<UserDto> getUserWithDefaultCaching(@PathVariable(value = "name") String name) {
22+
return ResponseEntity.ok(new UserDto(name));
23+
}
24+
2025
@RequestMapping(value = "/users/{name}", method = RequestMethod.GET)
2126
public ResponseEntity<UserDto> getUser(@PathVariable(value = "name") String name) {
2227
return ResponseEntity.ok()
2328
.cacheControl(CacheControl.maxAge(60, TimeUnit.SECONDS))
2429
.body(new UserDto(name));
2530
}
2631

27-
2832
@RequestMapping(value = "/timestamp", method = RequestMethod.GET)
2933
public ResponseEntity<TimestampDto> getServerTimestamp() {
3034
return ResponseEntity.ok()

‎spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/config/SpringSecurityConfig.java‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,5 @@
1313
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
1414

1515
@Override
16-
protected void configure(HttpSecurity http) throws Exception {
17-
http.headers()
18-
.defaultsDisabled()
19-
.cacheControl();
20-
}
16+
protected void configure(HttpSecurity http) throws Exception {}
2117
}

‎spring-security-cache-control/src/test/java/com/baeldung/cachecontrol/ResourceEndpointLiveTest.java‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,43 @@
33
import com.jayway.restassured.http.ContentType;
44
import org.junit.Test;
55
import org.junit.runner.RunWith;
6+
import org.springframework.boot.context.embedded.LocalServerPort;
67
import org.springframework.boot.test.context.SpringBootTest;
78
import org.springframework.test.context.junit4.SpringRunner;
89

910
import static com.jayway.restassured.RestAssured.given;
1011

1112
@RunWith(SpringRunner.class)
12-
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, classes = AppRunner.class)
13+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = AppRunner.class)
1314
public class ResourceEndpointLiveTest {
14-
private static final String URL_PREFIX = "http://localhost:8080";
15+
16+
@LocalServerPort
17+
private int serverPort;
18+
19+
@Test
20+
public void whenGetRequestForUser_shouldRespondWithDefaultCacheHeaders() {
21+
given()
22+
.when()
23+
.get(getBaseUrl() + "/default/users/Michael")
24+
.then()
25+
.headers("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate")
26+
.header("Pragma", "no-cache");
27+
}
28+
29+
@Test
30+
public void whenGetRequestForUser_shouldRespondMaxAgeCacheControl() {
31+
given()
32+
.when()
33+
.get(getBaseUrl() + "/users/Michael")
34+
.then()
35+
.header("Cache-Control", "max-age=60");
36+
}
1537

1638
@Test
1739
public void givenServiceEndpoint_whenGetRequestForUser_shouldResponseWithCacheControlMaxAge() {
1840
given()
1941
.when()
20-
.get(URL_PREFIX + "/users/Michael")
42+
.get(getBaseUrl() + "/users/Michael")
2143
.then()
2244
.contentType(ContentType.JSON).and().statusCode(200).and()
2345
.header("Cache-Control", "max-age=60");
@@ -27,7 +49,7 @@ public void givenServiceEndpoint_whenGetRequestForUser_shouldResponseWithCacheCo
2749
public void givenServiceEndpoint_whenGetRequestForNotCacheableContent_shouldResponseWithCacheControlNoCache() {
2850
given()
2951
.when()
30-
.get(URL_PREFIX + "/timestamp")
52+
.get(getBaseUrl() + "/timestamp")
3153
.then()
3254
.contentType(ContentType.JSON).and().statusCode(200).and()
3355
.header("Cache-Control", "no-store");
@@ -37,10 +59,14 @@ public void givenServiceEndpoint_whenGetRequestForNotCacheableContent_shouldResp
3759
public void givenServiceEndpoint_whenGetRequestForPrivateUser_shouldResponseWithSecurityDefaultCacheControl() {
3860
given()
3961
.when()
40-
.get(URL_PREFIX + "/private/users/Michael")
62+
.get(getBaseUrl() + "/private/users/Michael")
4163
.then()
4264
.contentType(ContentType.JSON).and().statusCode(200).and()
4365
.header("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate");
4466
}
4567

68+
private String getBaseUrl() {
69+
return "http://localhost:" + serverPort;
70+
}
71+
4672
}

0 commit comments

Comments
 (0)