1717package org .springframework .web .servlet .resource ;
1818
1919import java .io .IOException ;
20- import java .time .Instant ;
21- import java .time .ZoneId ;
22- import java .time .ZonedDateTime ;
2320import java .util .ArrayList ;
2421import java .util .Arrays ;
2522import java .util .Collections ;
4542import org .springframework .web .accept .ContentNegotiationManagerFactoryBean ;
4643import org .springframework .web .servlet .HandlerMapping ;
4744
48- import static java .time .format .DateTimeFormatter .*;
49- import static org .junit .Assert .assertEquals ;
50- import static org .junit .Assert .assertSame ;
51- import static org .junit .Assert .assertThat ;
52- import static org .junit .Assert .assertTrue ;
53- import static org .junit .Assert .fail ;
45+ import static org .junit .Assert .*;
5446
5547/**
56- * Unit tests for ResourceHttpRequestHandler.
48+ * Unit tests for {@link ResourceHttpRequestHandler} .
5749 *
5850 * @author Keith Donald
5951 * @author Jeremy Grelle
@@ -96,7 +88,7 @@ public void getResource() throws Exception {
9688 assertEquals (17 , this .response .getContentLength ());
9789 assertEquals ("max-age=3600" , this .response .getHeader ("Cache-Control" ));
9890 assertTrue (this .response .containsHeader ("Last-Modified" ));
99- assertEquals (this .response .getHeader ("Last-Modified" ), resourceLastModifiedDate ( "test/foo.css" ) );
91+ assertEquals (resourceLastModified ( "test/foo.css" ) / 1000 , this .response .getDateHeader ("Last-Modified" ) / 1000 );
10092 assertEquals ("bytes" , this .response .getHeader ("Accept-Ranges" ));
10193 assertEquals (1 , this .response .getHeaders ("Accept-Ranges" ).size ());
10294 assertEquals ("h1 { color:red; }" , this .response .getContentAsString ());
@@ -113,7 +105,7 @@ public void getResourceHttpHeader() throws Exception {
113105 assertEquals (17 , this .response .getContentLength ());
114106 assertEquals ("max-age=3600" , this .response .getHeader ("Cache-Control" ));
115107 assertTrue (this .response .containsHeader ("Last-Modified" ));
116- assertEquals (this .response .getHeader ("Last-Modified" ), resourceLastModifiedDate ( "test/foo.css" ) );
108+ assertEquals (resourceLastModified ( "test/foo.css" ) / 1000 , this .response .getDateHeader ("Last-Modified" ) / 1000 );
117109 assertEquals ("bytes" , this .response .getHeader ("Accept-Ranges" ));
118110 assertEquals (1 , this .response .getHeaders ("Accept-Ranges" ).size ());
119111 assertEquals (0 , this .response .getContentAsByteArray ().length );
@@ -137,7 +129,7 @@ public void getResourceNoCache() throws Exception {
137129
138130 assertEquals ("no-store" , this .response .getHeader ("Cache-Control" ));
139131 assertTrue (this .response .containsHeader ("Last-Modified" ));
140- assertEquals (this .response .getHeader ("Last-Modified" ), resourceLastModifiedDate ( "test/foo.css" ) );
132+ assertEquals (resourceLastModified ( "test/foo.css" ) / 1000 , this .response .getDateHeader ("Last-Modified" ) / 1000 );
141133 assertEquals ("bytes" , this .response .getHeader ("Accept-Ranges" ));
142134 assertEquals (1 , this .response .getHeaders ("Accept-Ranges" ).size ());
143135 }
@@ -168,9 +160,9 @@ public void getResourceHttp10BehaviorCache() throws Exception {
168160 this .handler .handleRequest (this .request , this .response );
169161
170162 assertEquals ("max-age=3600, must-revalidate" , this .response .getHeader ("Cache-Control" ));
171- assertTrue (dateHeaderAsLong ("Expires" ) >= System .currentTimeMillis () - 1000 + (3600 * 1000 ));
163+ assertTrue (this . response . getDateHeader ("Expires" ) >= System .currentTimeMillis () - 1000 + (3600 * 1000 ));
172164 assertTrue (this .response .containsHeader ("Last-Modified" ));
173- assertEquals (this .response .getHeader ("Last-Modified" ), resourceLastModifiedDate ( "test/foo.css" ) );
165+ assertEquals (resourceLastModified ( "test/foo.css" ) / 1000 , this .response .getDateHeader ("Last-Modified" ) / 1000 );
174166 assertEquals ("bytes" , this .response .getHeader ("Accept-Ranges" ));
175167 assertEquals (1 , this .response .getHeaders ("Accept-Ranges" ).size ());
176168 }
@@ -188,9 +180,9 @@ public void getResourceHttp10BehaviorNoCache() throws Exception {
188180 assertEquals ("no-cache" , this .response .getHeader ("Pragma" ));
189181 assertThat (this .response .getHeaderValues ("Cache-Control" ), Matchers .iterableWithSize (1 ));
190182 assertEquals ("no-cache" , this .response .getHeader ("Cache-Control" ));
191- assertTrue (dateHeaderAsLong ("Expires" ) <= System .currentTimeMillis ());
183+ assertTrue (this . response . getDateHeader ("Expires" ) <= System .currentTimeMillis ());
192184 assertTrue (this .response .containsHeader ("Last-Modified" ));
193- assertEquals (dateHeaderAsLong ( "Last-Modified " ) / 1000 , resourceLastModified ( "test/foo.css " ) / 1000 );
185+ assertEquals (resourceLastModified ( "test/foo.css " ) / 1000 , this . response . getDateHeader ( "Last-Modified " ) / 1000 );
194186 assertEquals ("bytes" , this .response .getHeader ("Accept-Ranges" ));
195187 assertEquals (1 , this .response .getHeaders ("Accept-Ranges" ).size ());
196188 }
@@ -203,7 +195,7 @@ public void getResourceWithHtmlMediaType() throws Exception {
203195 assertEquals ("text/html" , this .response .getContentType ());
204196 assertEquals ("max-age=3600" , this .response .getHeader ("Cache-Control" ));
205197 assertTrue (this .response .containsHeader ("Last-Modified" ));
206- assertEquals (this .response .getHeader ("Last-Modified" ), resourceLastModifiedDate ( "test/foo.html" ) );
198+ assertEquals (resourceLastModified ( "test/foo.html" ) / 1000 , this .response .getDateHeader ("Last-Modified" ) / 1000 );
207199 assertEquals ("bytes" , this .response .getHeader ("Accept-Ranges" ));
208200 assertEquals (1 , this .response .getHeaders ("Accept-Ranges" ).size ());
209201 }
@@ -217,7 +209,8 @@ public void getResourceFromAlternatePath() throws Exception {
217209 assertEquals (17 , this .response .getContentLength ());
218210 assertEquals ("max-age=3600" , this .response .getHeader ("Cache-Control" ));
219211 assertTrue (this .response .containsHeader ("Last-Modified" ));
220- assertEquals (this .response .getHeader ("Last-Modified" ), resourceLastModifiedDate ("testalternatepath/baz.css" ));
212+ assertEquals (resourceLastModified ("testalternatepath/baz.css" ) / 1000 ,
213+ this .response .getDateHeader ("Last-Modified" ) / 1000 );
221214 assertEquals ("bytes" , this .response .getHeader ("Accept-Ranges" ));
222215 assertEquals (1 , this .response .getHeaders ("Accept-Ranges" ).size ());
223216 assertEquals ("h1 { color:red; }" , this .response .getContentAsString ());
@@ -241,7 +234,7 @@ public void getResourceFromSubDirectoryOfAlternatePath() throws Exception {
241234 assertEquals ("function foo() { console.log(\" hello world\" ); }" , this .response .getContentAsString ());
242235 }
243236
244- @ Test // SPR-13658
237+ @ Test // SPR-13658
245238 public void getResourceWithRegisteredMediaType () throws Exception {
246239 ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean ();
247240 factory .addMediaType ("bar" , new MediaType ("foo" , "bar" ));
@@ -262,7 +255,7 @@ public void getResourceWithRegisteredMediaType() throws Exception {
262255 assertEquals ("h1 { color:red; }" , this .response .getContentAsString ());
263256 }
264257
265- @ Test // SPR-14577
258+ @ Test // SPR-14577
266259 public void getMediaTypeWithFavorPathExtensionOff () throws Exception {
267260 ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean ();
268261 factory .setFavorPathExtension (false );
@@ -283,18 +276,16 @@ public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
283276 assertEquals ("text/html" , this .response .getContentType ());
284277 }
285278
286- @ Test // SPR-14368
279+ @ Test // SPR-14368
287280 public void getResourceWithMediaTypeResolvedThroughServletContext () throws Exception {
288281 MockServletContext servletContext = new MockServletContext () {
289-
290282 @ Override
291283 public String getMimeType (String filePath ) {
292284 return "foo/bar" ;
293285 }
294-
295286 @ Override
296287 public String getVirtualServerName () {
297- return null ;
288+ return "" ;
298289 }
299290 };
300291
@@ -619,20 +610,10 @@ public void doOverwriteExistingCacheControlHeaders() throws Exception {
619610 }
620611
621612
622- private long dateHeaderAsLong (String responseHeaderName ) throws Exception {
623- String header = this .response .getHeader (responseHeaderName );
624- return ZonedDateTime .parse (header , RFC_1123_DATE_TIME ).toInstant ().toEpochMilli ();
625- }
626-
627613 private long resourceLastModified (String resourceName ) throws IOException {
628614 return new ClassPathResource (resourceName , getClass ()).getFile ().lastModified ();
629615 }
630616
631- private String resourceLastModifiedDate (String resourceName ) throws IOException {
632- long lastModified = new ClassPathResource (resourceName , getClass ()).getFile ().lastModified ();
633- return RFC_1123_DATE_TIME .format (Instant .ofEpochMilli (lastModified ).atZone (ZoneId .of ("GMT" )));
634- }
635-
636617
637618 private static class TestServletContext extends MockServletContext {
638619
0 commit comments