33import com .jayway .restassured .http .ContentType ;
44import org .junit .Test ;
55import org .junit .runner .RunWith ;
6+ import org .springframework .boot .context .embedded .LocalServerPort ;
67import org .springframework .boot .test .context .SpringBootTest ;
78import org .springframework .test .context .junit4 .SpringRunner ;
89
910import 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 )
1314public 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