49 lines
2.0 KiB
Java
Raw Normal View History

2017-01-26 20:34:40 +01:00
package com.baeldung.cachecontrol;
import static io.restassured.RestAssured.given;
2017-03-11 23:31:41 +02:00
2017-01-26 20:34:40 +01:00
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;
2017-01-26 20:34:40 +01:00
import io.restassured.http.ContentType;
2017-01-26 20:34:40 +01:00
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = AppRunner.class)
2017-03-11 23:31:41 +02:00
public class ResourceEndpointIntegrationTest {
@LocalServerPort
private int serverPort;
@Test
public void whenGetRequestForUser_shouldRespondWithDefaultCacheHeaders() {
2017-03-11 23:31:41 +02:00
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() {
2017-03-11 23:31:41 +02:00
given().when().get(getBaseUrl() + "/users/Michael").then().header("Cache-Control", "max-age=60");
}
2017-01-26 20:34:40 +01:00
2017-01-26 21:47:52 +01:00
@Test
public void givenServiceEndpoint_whenGetRequestForUser_shouldResponseWithCacheControlMaxAge() {
2017-03-11 23:31:41 +02:00
given().when().get(getBaseUrl() + "/users/Michael").then().contentType(ContentType.JSON).and().statusCode(200).and().header("Cache-Control", "max-age=60");
2017-01-26 21:47:52 +01:00
}
2017-01-26 20:34:40 +01:00
@Test
public void givenServiceEndpoint_whenGetRequestForNotCacheableContent_shouldResponseWithCacheControlNoCache() {
2017-03-11 23:31:41 +02:00
given().when().get(getBaseUrl() + "/timestamp").then().contentType(ContentType.JSON).and().statusCode(200).and().header("Cache-Control", "no-store");
2017-01-26 20:34:40 +01:00
}
@Test
public void givenServiceEndpoint_whenGetRequestForPrivateUser_shouldResponseWithSecurityDefaultCacheControl() {
2017-03-11 23:31:41 +02:00
given().when().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");
2017-01-26 20:34:40 +01:00
}
private String getBaseUrl() {
2017-02-05 10:20:12 +01:00
return String.format("http://localhost:%d", serverPort);
}
2017-01-26 20:34:40 +01:00
}