Skip to content

Commit

Permalink
Fix failing test
Browse files Browse the repository at this point in the history
After the fix #658c7f for lenient parsing of dates, the error message
raised uses an HttpHeaders-formatted date. As a result the test
verifying the error message fails in the beginning of the month between
1-9 because it's formatted slightly differently.
  • Loading branch information
rstoyanchev committed Nov 1, 2018
1 parent 48654c6 commit c2b55e6
Showing 1 changed file with 22 additions and 32 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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))
Expand All @@ -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));
}


Expand All @@ -226,15 +220,11 @@ public void setStubTimestamp(long timestamp) {
public ResponseEntity<Person> 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;
}
}

}

0 comments on commit c2b55e6

Please sign in to comment.