17
17
package org .springframework .web .servlet .resource ;
18
18
19
19
import java .io .IOException ;
20
- import java .time .Instant ;
21
- import java .time .ZoneId ;
22
- import java .time .ZonedDateTime ;
23
20
import java .util .ArrayList ;
24
21
import java .util .Arrays ;
25
22
import java .util .Collections ;
45
42
import org .springframework .web .accept .ContentNegotiationManagerFactoryBean ;
46
43
import org .springframework .web .servlet .HandlerMapping ;
47
44
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 .*;
54
46
55
47
/**
56
- * Unit tests for ResourceHttpRequestHandler.
48
+ * Unit tests for {@link ResourceHttpRequestHandler} .
57
49
*
58
50
* @author Keith Donald
59
51
* @author Jeremy Grelle
@@ -96,7 +88,7 @@ public void getResource() throws Exception {
96
88
assertEquals (17 , this .response .getContentLength ());
97
89
assertEquals ("max-age=3600" , this .response .getHeader ("Cache-Control" ));
98
90
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 );
100
92
assertEquals ("bytes" , this .response .getHeader ("Accept-Ranges" ));
101
93
assertEquals (1 , this .response .getHeaders ("Accept-Ranges" ).size ());
102
94
assertEquals ("h1 { color:red; }" , this .response .getContentAsString ());
@@ -113,7 +105,7 @@ public void getResourceHttpHeader() throws Exception {
113
105
assertEquals (17 , this .response .getContentLength ());
114
106
assertEquals ("max-age=3600" , this .response .getHeader ("Cache-Control" ));
115
107
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 );
117
109
assertEquals ("bytes" , this .response .getHeader ("Accept-Ranges" ));
118
110
assertEquals (1 , this .response .getHeaders ("Accept-Ranges" ).size ());
119
111
assertEquals (0 , this .response .getContentAsByteArray ().length );
@@ -137,7 +129,7 @@ public void getResourceNoCache() throws Exception {
137
129
138
130
assertEquals ("no-store" , this .response .getHeader ("Cache-Control" ));
139
131
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 );
141
133
assertEquals ("bytes" , this .response .getHeader ("Accept-Ranges" ));
142
134
assertEquals (1 , this .response .getHeaders ("Accept-Ranges" ).size ());
143
135
}
@@ -168,9 +160,9 @@ public void getResourceHttp10BehaviorCache() throws Exception {
168
160
this .handler .handleRequest (this .request , this .response );
169
161
170
162
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 ));
172
164
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 );
174
166
assertEquals ("bytes" , this .response .getHeader ("Accept-Ranges" ));
175
167
assertEquals (1 , this .response .getHeaders ("Accept-Ranges" ).size ());
176
168
}
@@ -188,9 +180,9 @@ public void getResourceHttp10BehaviorNoCache() throws Exception {
188
180
assertEquals ("no-cache" , this .response .getHeader ("Pragma" ));
189
181
assertThat (this .response .getHeaderValues ("Cache-Control" ), Matchers .iterableWithSize (1 ));
190
182
assertEquals ("no-cache" , this .response .getHeader ("Cache-Control" ));
191
- assertTrue (dateHeaderAsLong ("Expires" ) <= System .currentTimeMillis ());
183
+ assertTrue (this . response . getDateHeader ("Expires" ) <= System .currentTimeMillis ());
192
184
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 );
194
186
assertEquals ("bytes" , this .response .getHeader ("Accept-Ranges" ));
195
187
assertEquals (1 , this .response .getHeaders ("Accept-Ranges" ).size ());
196
188
}
@@ -203,7 +195,7 @@ public void getResourceWithHtmlMediaType() throws Exception {
203
195
assertEquals ("text/html" , this .response .getContentType ());
204
196
assertEquals ("max-age=3600" , this .response .getHeader ("Cache-Control" ));
205
197
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 );
207
199
assertEquals ("bytes" , this .response .getHeader ("Accept-Ranges" ));
208
200
assertEquals (1 , this .response .getHeaders ("Accept-Ranges" ).size ());
209
201
}
@@ -217,7 +209,8 @@ public void getResourceFromAlternatePath() throws Exception {
217
209
assertEquals (17 , this .response .getContentLength ());
218
210
assertEquals ("max-age=3600" , this .response .getHeader ("Cache-Control" ));
219
211
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 );
221
214
assertEquals ("bytes" , this .response .getHeader ("Accept-Ranges" ));
222
215
assertEquals (1 , this .response .getHeaders ("Accept-Ranges" ).size ());
223
216
assertEquals ("h1 { color:red; }" , this .response .getContentAsString ());
@@ -241,7 +234,7 @@ public void getResourceFromSubDirectoryOfAlternatePath() throws Exception {
241
234
assertEquals ("function foo() { console.log(\" hello world\" ); }" , this .response .getContentAsString ());
242
235
}
243
236
244
- @ Test // SPR-13658
237
+ @ Test // SPR-13658
245
238
public void getResourceWithRegisteredMediaType () throws Exception {
246
239
ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean ();
247
240
factory .addMediaType ("bar" , new MediaType ("foo" , "bar" ));
@@ -262,7 +255,7 @@ public void getResourceWithRegisteredMediaType() throws Exception {
262
255
assertEquals ("h1 { color:red; }" , this .response .getContentAsString ());
263
256
}
264
257
265
- @ Test // SPR-14577
258
+ @ Test // SPR-14577
266
259
public void getMediaTypeWithFavorPathExtensionOff () throws Exception {
267
260
ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean ();
268
261
factory .setFavorPathExtension (false );
@@ -283,18 +276,16 @@ public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
283
276
assertEquals ("text/html" , this .response .getContentType ());
284
277
}
285
278
286
- @ Test // SPR-14368
279
+ @ Test // SPR-14368
287
280
public void getResourceWithMediaTypeResolvedThroughServletContext () throws Exception {
288
281
MockServletContext servletContext = new MockServletContext () {
289
-
290
282
@ Override
291
283
public String getMimeType (String filePath ) {
292
284
return "foo/bar" ;
293
285
}
294
-
295
286
@ Override
296
287
public String getVirtualServerName () {
297
- return null ;
288
+ return "" ;
298
289
}
299
290
};
300
291
@@ -619,20 +610,10 @@ public void doOverwriteExistingCacheControlHeaders() throws Exception {
619
610
}
620
611
621
612
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
-
627
613
private long resourceLastModified (String resourceName ) throws IOException {
628
614
return new ClassPathResource (resourceName , getClass ()).getFile ().lastModified ();
629
615
}
630
616
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
-
636
617
637
618
private static class TestServletContext extends MockServletContext {
638
619
0 commit comments