diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HeaderAssertionTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HeaderAssertionTests.java index a23dc676b887..7a3ae1a5b3c8 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HeaderAssertionTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HeaderAssertionTests.java @@ -24,6 +24,7 @@ import org.junit.Before; import org.junit.Test; +import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.test.web.Person; @@ -56,20 +57,19 @@ public class HeaderAssertionTests { private String minuteAgo; - private String secondLater; - private MockMvc mockMvc; private final long currentTime = System.currentTimeMillis(); + private SimpleDateFormat dateFormat; + @Before public void setup() { - SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); - dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); + this.dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); + this.dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); this.now = dateFormat.format(new Date(this.currentTime)); this.minuteAgo = dateFormat.format(new Date(this.currentTime - (1000 * 60))); - this.secondLater = dateFormat.format(new Date(this.currentTime + 1000)); PersonController controller = new PersonController(); controller.setStubTimestamp(this.currentTime); @@ -164,29 +164,24 @@ public void doesNotExistFail() throws Exception { this.mockMvc.perform(get("/persons/1")).andExpect(header().doesNotExist(LAST_MODIFIED)); } - @Test - public void stringWithIncorrectResponseHeaderValue() throws Exception { - assertIncorrectResponseHeader(header().string(LAST_MODIFIED, secondLater), secondLater); - } - - @Test - public void stringWithMatcherAndIncorrectResponseHeaderValue() throws Exception { - assertIncorrectResponseHeader(header().string(LAST_MODIFIED, equalTo(secondLater)), secondLater); - } - - @Test - public void dateValueWithIncorrectResponseHeaderValue() throws Exception { - long unexpected = this.currentTime + 1000; - assertIncorrectResponseHeader(header().dateValue(LAST_MODIFIED, unexpected), secondLater); - } - @Test(expected = AssertionError.class) public void longValueWithIncorrectResponseHeaderValue() throws Exception { this.mockMvc.perform(get("/persons/1")).andExpect(header().longValue("X-Rate-Limiting", 1)); } + @Test + public void stringWithMatcherAndIncorrectResponseHeaderValue() throws Exception { + long secondLater = this.currentTime + 1000; + String expected = this.dateFormat.format(new Date(secondLater)); + assertIncorrectResponseHeader(header().string(LAST_MODIFIED, expected), expected); + assertIncorrectResponseHeader(header().string(LAST_MODIFIED, equalTo(expected)), expected); + // Comparison by date uses HttpHeaders to format the date in the error message. + HttpHeaders headers = new HttpHeaders(); + headers.setDate("expected", secondLater); + assertIncorrectResponseHeader(header().dateValue(LAST_MODIFIED, secondLater), headers.getFirst("expected")); + } - private void assertIncorrectResponseHeader(ResultMatcher matcher, String unexpected) throws Exception { + private void assertIncorrectResponseHeader(ResultMatcher matcher, String expected) throws Exception { try { this.mockMvc.perform(get("/persons/1") .header(IF_MODIFIED_SINCE, minuteAgo)) @@ -201,15 +196,14 @@ private void assertIncorrectResponseHeader(ResultMatcher matcher, String unexpec // SPR-10659: ensure header name is in the message // Unfortunately, we can't control formatting from JUnit or Hamcrest. assertMessageContains(err, "Response header '" + LAST_MODIFIED + "'"); - assertMessageContains(err, unexpected); - assertMessageContains(err, now); + assertMessageContains(err, expected); + assertMessageContains(err, this.now); } } private void assertMessageContains(AssertionError error, String expected) { - String message = error.getMessage(); - assertTrue("Failure message should contain [" + expected + "], actual is [" + message + "]", - message.contains(expected)); + assertTrue("Failure message should contain [" + expected + "], actual is [" + error.getMessage() + "]", + error.getMessage().contains(expected)); } @@ -226,15 +220,11 @@ public void setStubTimestamp(long timestamp) { public ResponseEntity showEntity(@PathVariable long id, WebRequest request) { return ResponseEntity .ok() - .lastModified(calculateLastModified(id)) + .lastModified(this.timestamp) .header("X-Rate-Limiting", "42") .header("Vary", "foo", "bar") .body(new Person("Jason")); } - - private long calculateLastModified(long id) { - return this.timestamp; - } } }