3434import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .*;
3535import static org .springframework .test .web .servlet .setup .MockMvcBuilders .*;
3636
37+ import java .text .SimpleDateFormat ;
38+ import java .util .Locale ;
39+ import java .util .TimeZone ;
40+
3741/**
3842 * Examples of expectations on response header values.
3943 *
@@ -50,13 +54,20 @@ public class HeaderAssertionTests {
5054
5155 private final long currentTime = System .currentTimeMillis ();
5256
57+ private String currentDate ;
58+
59+ private SimpleDateFormat dateFormat ;
60+
5361 private MockMvc mockMvc ;
5462
5563 private PersonController personController ;
5664
5765
5866 @ Before
5967 public void setup () {
68+ this .dateFormat = new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss z" , Locale .US );
69+ this .dateFormat .setTimeZone (TimeZone .getTimeZone ("GMT" ));
70+ this .currentDate = dateFormat .format (currentTime );
6071 this .personController = new PersonController ();
6172 this .personController .setStubTimestamp (currentTime );
6273 this .mockMvc = standaloneSetup (this .personController ).build ();
@@ -65,19 +76,19 @@ public void setup() {
6576 @ Test
6677 public void stringWithCorrectResponseHeaderValue () throws Exception {
6778 this .mockMvc .perform (get ("/persons/1" ).header (IF_MODIFIED_SINCE , currentTime - (1000 * 60 )))//
68- .andExpect (header ().string (LAST_MODIFIED , String . valueOf ( currentTime ) ));
79+ .andExpect (header ().string (LAST_MODIFIED , currentDate ));
6980 }
7081
7182 @ Test
7283 public void stringWithMatcherAndCorrectResponseHeaderValue () throws Exception {
7384 this .mockMvc .perform (get ("/persons/1" ).header (IF_MODIFIED_SINCE , currentTime - (1000 * 60 )))//
74- .andExpect (header ().string (LAST_MODIFIED , equalTo (String . valueOf ( currentTime ) )));
85+ .andExpect (header ().string (LAST_MODIFIED , equalTo (currentDate )));
7586 }
7687
7788 @ Test
7889 public void longValueWithCorrectResponseHeaderValue () throws Exception {
7990 this .mockMvc .perform (get ("/persons/1" ).header (IF_MODIFIED_SINCE , currentTime - (1000 * 60 )))//
80- .andExpect (header ().longValue (LAST_MODIFIED , currentTime ));
91+ .andExpect (header ().string (LAST_MODIFIED , currentDate ));
8192 }
8293
8394 @ Test
@@ -129,21 +140,21 @@ public void doesNotExistFail() throws Exception {
129140
130141 @ Test
131142 public void stringWithIncorrectResponseHeaderValue () throws Exception {
132- long unexpected = currentTime + 1 ;
133- assertIncorrectResponseHeaderValue (header ().string (LAST_MODIFIED , String . valueOf (unexpected )), unexpected );
143+ long unexpected = currentTime + 1000 ;
144+ assertIncorrectResponseHeaderValue (header ().string (LAST_MODIFIED , dateFormat . format (unexpected )), unexpected );
134145 }
135146
136147 @ Test
137148 public void stringWithMatcherAndIncorrectResponseHeaderValue () throws Exception {
138- long unexpected = currentTime + 1 ;
139- assertIncorrectResponseHeaderValue (header ().string (LAST_MODIFIED , equalTo (String . valueOf (unexpected ))),
149+ long unexpected = currentTime + 1000 ;
150+ assertIncorrectResponseHeaderValue (header ().string (LAST_MODIFIED , equalTo (dateFormat . format (unexpected ))),
140151 unexpected );
141152 }
142153
143154 @ Test
144155 public void longValueWithIncorrectResponseHeaderValue () throws Exception {
145- long unexpected = currentTime + 1 ;
146- assertIncorrectResponseHeaderValue (header ().longValue (LAST_MODIFIED , unexpected ), unexpected );
156+ long unexpected = currentTime + 1000 ;
157+ assertIncorrectResponseHeaderValue (header ().string (LAST_MODIFIED , dateFormat . format ( unexpected ) ), unexpected );
147158 }
148159
149160 private void assertIncorrectResponseHeaderValue (ResultMatcher resultMatcher , long unexpected ) throws Exception {
@@ -162,8 +173,8 @@ private void assertIncorrectResponseHeaderValue(ResultMatcher resultMatcher, lon
162173 // We don't use assertEquals() since we cannot control the formatting
163174 // produced by JUnit or Hamcrest.
164175 assertMessageContains (e , "Response header " + LAST_MODIFIED );
165- assertMessageContains (e , String . valueOf (unexpected ));
166- assertMessageContains (e , String . valueOf ( currentTime ) );
176+ assertMessageContains (e , dateFormat . format (unexpected ));
177+ assertMessageContains (e , currentDate );
167178 }
168179 }
169180
0 commit comments