Skip to content

Commit c2b55e6

Browse files
committed
Fix failing test
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.
1 parent 48654c6 commit c2b55e6

File tree

1 file changed

+22
-32
lines changed

1 file changed

+22
-32
lines changed

spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HeaderAssertionTests.java

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.junit.Before;
2525
import org.junit.Test;
2626

27+
import org.springframework.http.HttpHeaders;
2728
import org.springframework.http.ResponseEntity;
2829
import org.springframework.stereotype.Controller;
2930
import org.springframework.test.web.Person;
@@ -56,20 +57,19 @@ public class HeaderAssertionTests {
5657

5758
private String minuteAgo;
5859

59-
private String secondLater;
60-
6160
private MockMvc mockMvc;
6261

6362
private final long currentTime = System.currentTimeMillis();
6463

64+
private SimpleDateFormat dateFormat;
65+
6566

6667
@Before
6768
public void setup() {
68-
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
69-
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
69+
this.dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
70+
this.dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
7071
this.now = dateFormat.format(new Date(this.currentTime));
7172
this.minuteAgo = dateFormat.format(new Date(this.currentTime - (1000 * 60)));
72-
this.secondLater = dateFormat.format(new Date(this.currentTime + 1000));
7373

7474
PersonController controller = new PersonController();
7575
controller.setStubTimestamp(this.currentTime);
@@ -164,29 +164,24 @@ public void doesNotExistFail() throws Exception {
164164
this.mockMvc.perform(get("/persons/1")).andExpect(header().doesNotExist(LAST_MODIFIED));
165165
}
166166

167-
@Test
168-
public void stringWithIncorrectResponseHeaderValue() throws Exception {
169-
assertIncorrectResponseHeader(header().string(LAST_MODIFIED, secondLater), secondLater);
170-
}
171-
172-
@Test
173-
public void stringWithMatcherAndIncorrectResponseHeaderValue() throws Exception {
174-
assertIncorrectResponseHeader(header().string(LAST_MODIFIED, equalTo(secondLater)), secondLater);
175-
}
176-
177-
@Test
178-
public void dateValueWithIncorrectResponseHeaderValue() throws Exception {
179-
long unexpected = this.currentTime + 1000;
180-
assertIncorrectResponseHeader(header().dateValue(LAST_MODIFIED, unexpected), secondLater);
181-
}
182-
183167
@Test(expected = AssertionError.class)
184168
public void longValueWithIncorrectResponseHeaderValue() throws Exception {
185169
this.mockMvc.perform(get("/persons/1")).andExpect(header().longValue("X-Rate-Limiting", 1));
186170
}
187171

172+
@Test
173+
public void stringWithMatcherAndIncorrectResponseHeaderValue() throws Exception {
174+
long secondLater = this.currentTime + 1000;
175+
String expected = this.dateFormat.format(new Date(secondLater));
176+
assertIncorrectResponseHeader(header().string(LAST_MODIFIED, expected), expected);
177+
assertIncorrectResponseHeader(header().string(LAST_MODIFIED, equalTo(expected)), expected);
178+
// Comparison by date uses HttpHeaders to format the date in the error message.
179+
HttpHeaders headers = new HttpHeaders();
180+
headers.setDate("expected", secondLater);
181+
assertIncorrectResponseHeader(header().dateValue(LAST_MODIFIED, secondLater), headers.getFirst("expected"));
182+
}
188183

189-
private void assertIncorrectResponseHeader(ResultMatcher matcher, String unexpected) throws Exception {
184+
private void assertIncorrectResponseHeader(ResultMatcher matcher, String expected) throws Exception {
190185
try {
191186
this.mockMvc.perform(get("/persons/1")
192187
.header(IF_MODIFIED_SINCE, minuteAgo))
@@ -201,15 +196,14 @@ private void assertIncorrectResponseHeader(ResultMatcher matcher, String unexpec
201196
// SPR-10659: ensure header name is in the message
202197
// Unfortunately, we can't control formatting from JUnit or Hamcrest.
203198
assertMessageContains(err, "Response header '" + LAST_MODIFIED + "'");
204-
assertMessageContains(err, unexpected);
205-
assertMessageContains(err, now);
199+
assertMessageContains(err, expected);
200+
assertMessageContains(err, this.now);
206201
}
207202
}
208203

209204
private void assertMessageContains(AssertionError error, String expected) {
210-
String message = error.getMessage();
211-
assertTrue("Failure message should contain [" + expected + "], actual is [" + message + "]",
212-
message.contains(expected));
205+
assertTrue("Failure message should contain [" + expected + "], actual is [" + error.getMessage() + "]",
206+
error.getMessage().contains(expected));
213207
}
214208

215209

@@ -226,15 +220,11 @@ public void setStubTimestamp(long timestamp) {
226220
public ResponseEntity<Person> showEntity(@PathVariable long id, WebRequest request) {
227221
return ResponseEntity
228222
.ok()
229-
.lastModified(calculateLastModified(id))
223+
.lastModified(this.timestamp)
230224
.header("X-Rate-Limiting", "42")
231225
.header("Vary", "foo", "bar")
232226
.body(new Person("Jason"));
233227
}
234-
235-
private long calculateLastModified(long id) {
236-
return this.timestamp;
237-
}
238228
}
239229

240230
}

0 commit comments

Comments
 (0)