|
|
|
@ -34,6 +34,10 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder |
|
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
|
|
|
|
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; |
|
|
|
|
|
|
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
|
import java.util.Locale; |
|
|
|
|
import java.util.TimeZone; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Examples of expectations on response header values. |
|
|
|
|
* |
|
|
|
@ -50,6 +54,10 @@ public class HeaderAssertionTests { |
|
|
|
|
|
|
|
|
|
private final long currentTime = System.currentTimeMillis(); |
|
|
|
|
|
|
|
|
|
private String currentDate; |
|
|
|
|
|
|
|
|
|
private SimpleDateFormat dateFormat; |
|
|
|
|
|
|
|
|
|
private MockMvc mockMvc; |
|
|
|
|
|
|
|
|
|
private PersonController personController; |
|
|
|
@ -57,6 +65,9 @@ public class HeaderAssertionTests { |
|
|
|
|
|
|
|
|
|
@Before |
|
|
|
|
public void setup() { |
|
|
|
|
this.dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US); |
|
|
|
|
this.dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); |
|
|
|
|
this.currentDate = dateFormat.format(currentTime); |
|
|
|
|
this.personController = new PersonController(); |
|
|
|
|
this.personController.setStubTimestamp(currentTime); |
|
|
|
|
this.mockMvc = standaloneSetup(this.personController).build(); |
|
|
|
@ -65,19 +76,19 @@ public class HeaderAssertionTests { |
|
|
|
|
@Test |
|
|
|
|
public void stringWithCorrectResponseHeaderValue() throws Exception { |
|
|
|
|
this.mockMvc.perform(get("/persons/1").header(IF_MODIFIED_SINCE, currentTime - (1000 * 60)))//
|
|
|
|
|
.andExpect(header().string(LAST_MODIFIED, String.valueOf(currentTime))); |
|
|
|
|
.andExpect(header().string(LAST_MODIFIED, currentDate)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void stringWithMatcherAndCorrectResponseHeaderValue() throws Exception { |
|
|
|
|
this.mockMvc.perform(get("/persons/1").header(IF_MODIFIED_SINCE, currentTime - (1000 * 60)))//
|
|
|
|
|
.andExpect(header().string(LAST_MODIFIED, equalTo(String.valueOf(currentTime)))); |
|
|
|
|
.andExpect(header().string(LAST_MODIFIED, equalTo(currentDate))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void longValueWithCorrectResponseHeaderValue() throws Exception { |
|
|
|
|
this.mockMvc.perform(get("/persons/1").header(IF_MODIFIED_SINCE, currentTime - (1000 * 60)))//
|
|
|
|
|
.andExpect(header().longValue(LAST_MODIFIED, currentTime)); |
|
|
|
|
.andExpect(header().string(LAST_MODIFIED, currentDate)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ -129,21 +140,21 @@ public class HeaderAssertionTests { |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void stringWithIncorrectResponseHeaderValue() throws Exception { |
|
|
|
|
long unexpected = currentTime + 1; |
|
|
|
|
assertIncorrectResponseHeaderValue(header().string(LAST_MODIFIED, String.valueOf(unexpected)), unexpected); |
|
|
|
|
long unexpected = currentTime + 1000; |
|
|
|
|
assertIncorrectResponseHeaderValue(header().string(LAST_MODIFIED, dateFormat.format(unexpected)), unexpected); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void stringWithMatcherAndIncorrectResponseHeaderValue() throws Exception { |
|
|
|
|
long unexpected = currentTime + 1; |
|
|
|
|
assertIncorrectResponseHeaderValue(header().string(LAST_MODIFIED, equalTo(String.valueOf(unexpected))), |
|
|
|
|
long unexpected = currentTime + 1000; |
|
|
|
|
assertIncorrectResponseHeaderValue(header().string(LAST_MODIFIED, equalTo(dateFormat.format(unexpected))), |
|
|
|
|
unexpected); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void longValueWithIncorrectResponseHeaderValue() throws Exception { |
|
|
|
|
long unexpected = currentTime + 1; |
|
|
|
|
assertIncorrectResponseHeaderValue(header().longValue(LAST_MODIFIED, unexpected), unexpected); |
|
|
|
|
long unexpected = currentTime + 1000; |
|
|
|
|
assertIncorrectResponseHeaderValue(header().string(LAST_MODIFIED, dateFormat.format(unexpected)), unexpected); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void assertIncorrectResponseHeaderValue(ResultMatcher resultMatcher, long unexpected) throws Exception { |
|
|
|
@ -162,8 +173,8 @@ public class HeaderAssertionTests { |
|
|
|
|
// We don't use assertEquals() since we cannot control the formatting
|
|
|
|
|
// produced by JUnit or Hamcrest.
|
|
|
|
|
assertMessageContains(e, "Response header " + LAST_MODIFIED); |
|
|
|
|
assertMessageContains(e, String.valueOf(unexpected)); |
|
|
|
|
assertMessageContains(e, String.valueOf(currentTime)); |
|
|
|
|
assertMessageContains(e, dateFormat.format(unexpected)); |
|
|
|
|
assertMessageContains(e, currentDate); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|