Skip to content

Commit a75dd2d

Browse files
committed
Consistent getDateHeader checks in spring-webmvc unit tests
Issue: SPR-16160
1 parent e5c1dee commit a75dd2d

File tree

3 files changed

+24
-44
lines changed

3 files changed

+24
-44
lines changed

spring-webmvc/src/test/java/org/springframework/web/servlet/DispatcherServletTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public void localeRequest() throws Exception {
178178
MockHttpServletResponse response = new MockHttpServletResponse();
179179
simpleDispatcherServlet.service(request, response);
180180
assertTrue("Not forwarded", response.getForwardedUrl() == null);
181-
assertEquals("Wed, 1 Apr 2015 00:00:00 GMT", response.getHeader("Last-Modified"));
181+
assertEquals("Wed, 01 Apr 2015 00:00:00 GMT", response.getHeader("Last-Modified"));
182182
}
183183

184184
@Test
@@ -208,7 +208,7 @@ public void anotherLocaleRequest() throws Exception {
208208
assertTrue(request.getAttribute("test3") != null);
209209
assertTrue(request.getAttribute("test3x") != null);
210210
assertTrue(request.getAttribute("test3y") != null);
211-
assertEquals("Wed, 1 Apr 2015 00:00:01 GMT", response.getHeader("Last-Modified"));
211+
assertEquals("Wed, 01 Apr 2015 00:00:01 GMT", response.getHeader("Last-Modified"));
212212
}
213213

214214
@Test

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessorMockTests.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.web.servlet.mvc.method.annotation;
1818

19+
import java.io.IOException;
1920
import java.lang.reflect.Method;
2021
import java.net.URI;
2122
import java.nio.charset.StandardCharsets;
@@ -56,8 +57,7 @@
5657
import static java.time.format.DateTimeFormatter.*;
5758
import static org.junit.Assert.*;
5859
import static org.mockito.BDDMockito.*;
59-
import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM;
60-
import static org.springframework.http.MediaType.TEXT_PLAIN;
60+
import static org.springframework.http.MediaType.*;
6161
import static org.springframework.web.servlet.HandlerMapping.*;
6262

6363
/**
@@ -618,13 +618,13 @@ private void initStringMessageConversion(MediaType accepted) {
618618
given(stringHttpMessageConverter.canWrite(String.class, accepted)).willReturn(true);
619619
}
620620

621-
private void assertResponseBody(String body) throws Exception {
621+
private void assertResponseBody(String body) throws IOException {
622622
ArgumentCaptor<HttpOutputMessage> outputMessage = ArgumentCaptor.forClass(HttpOutputMessage.class);
623623
verify(stringHttpMessageConverter).write(eq(body), eq(TEXT_PLAIN), outputMessage.capture());
624624
}
625625

626-
private void assertConditionalResponse(HttpStatus status, String body, String etag,
627-
long lastModified) throws Exception {
626+
private void assertConditionalResponse(HttpStatus status, String body, String etag, long lastModified)
627+
throws IOException {
628628

629629
assertEquals(status.value(), servletResponse.getStatus());
630630
assertTrue(mavContainer.isRequestHandled());
@@ -640,8 +640,7 @@ private void assertConditionalResponse(HttpStatus status, String body, String et
640640
}
641641
if (lastModified != -1) {
642642
assertEquals(1, servletResponse.getHeaderValues(HttpHeaders.LAST_MODIFIED).size());
643-
assertEquals(RFC_1123_DATE_TIME.format(ofEpochMilli(lastModified).atZone(GMT)),
644-
servletResponse.getHeader(HttpHeaders.LAST_MODIFIED));
643+
assertEquals(lastModified / 1000, servletResponse.getDateHeader(HttpHeaders.LAST_MODIFIED) / 1000);
645644
}
646645
}
647646

spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
package org.springframework.web.servlet.resource;
1818

1919
import java.io.IOException;
20-
import java.time.Instant;
21-
import java.time.ZoneId;
22-
import java.time.ZonedDateTime;
2320
import java.util.ArrayList;
2421
import java.util.Arrays;
2522
import java.util.Collections;
@@ -45,15 +42,10 @@
4542
import org.springframework.web.accept.ContentNegotiationManagerFactoryBean;
4643
import 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

Comments
 (0)