Leverage RFC_1123_DATE_TIME formatter in tests
Issue: SPR-15661
This commit is contained in:
@@ -178,7 +178,7 @@ public class DispatcherServletTests {
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
simpleDispatcherServlet.service(request, response);
|
||||
assertTrue("Not forwarded", response.getForwardedUrl() == null);
|
||||
assertEquals("Wed, 01 Apr 2015 00:00:00 GMT", response.getHeader("Last-Modified"));
|
||||
assertEquals("Wed, 1 Apr 2015 00:00:00 GMT", response.getHeader("Last-Modified"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -208,7 +208,7 @@ public class DispatcherServletTests {
|
||||
assertTrue(request.getAttribute("test3") != null);
|
||||
assertTrue(request.getAttribute("test3x") != null);
|
||||
assertTrue(request.getAttribute("test3y") != null);
|
||||
assertEquals("Wed, 01 Apr 2015 00:00:01 GMT", response.getHeader("Last-Modified"));
|
||||
assertEquals("Wed, 1 Apr 2015 00:00:01 GMT", response.getHeader("Last-Modified"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -19,13 +19,11 @@ package org.springframework.web.servlet.mvc.method.annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.ZoneId;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
@@ -54,6 +52,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
|
||||
import static java.time.Instant.*;
|
||||
import static java.time.format.DateTimeFormatter.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
import static org.springframework.web.servlet.HandlerMapping.*;
|
||||
@@ -70,11 +70,12 @@ import static org.springframework.web.servlet.HandlerMapping.*;
|
||||
*/
|
||||
public class HttpEntityMethodProcessorMockTests {
|
||||
|
||||
private static final ZoneId GMT = ZoneId.of("GMT");
|
||||
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private SimpleDateFormat dateFormat;
|
||||
|
||||
private HttpEntityMethodProcessor processor;
|
||||
|
||||
private HttpMessageConverter<String> stringHttpMessageConverter;
|
||||
@@ -113,8 +114,6 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
@Before
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setup() throws Exception {
|
||||
dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
|
||||
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
|
||||
stringHttpMessageConverter = mock(HttpMessageConverter.class);
|
||||
given(stringHttpMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
|
||||
@@ -350,7 +349,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
public void shouldHandleLastModifiedWithHttp304() throws Exception {
|
||||
long currentTime = new Date().getTime();
|
||||
long oneMinuteAgo = currentTime - (1000 * 60);
|
||||
servletRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, dateFormat.format(currentTime));
|
||||
servletRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, RFC_1123_DATE_TIME.format(ofEpochMilli(currentTime).atZone(GMT)));
|
||||
ResponseEntity<String> returnValue = ResponseEntity.ok().lastModified(oneMinuteAgo).body("body");
|
||||
|
||||
initStringMessageConversion(MediaType.TEXT_PLAIN);
|
||||
@@ -388,7 +387,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
long currentTime = new Date().getTime();
|
||||
long oneMinuteAgo = currentTime - (1000 * 60);
|
||||
String etagValue = "\"deadb33f8badf00d\"";
|
||||
servletRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, dateFormat.format(currentTime));
|
||||
servletRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, RFC_1123_DATE_TIME.format(ofEpochMilli(currentTime).atZone(GMT)));
|
||||
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue);
|
||||
ResponseEntity<String> returnValue = ResponseEntity.ok().eTag(etagValue).lastModified(oneMinuteAgo).body("body");
|
||||
|
||||
@@ -418,7 +417,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
long oneMinuteAgo = currentTime - (1000 * 60);
|
||||
String etagValue = "\"deadb33f8badf00d\"";
|
||||
String changedEtagValue = "\"changed-etag-value\"";
|
||||
servletRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, dateFormat.format(currentTime));
|
||||
servletRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, RFC_1123_DATE_TIME.format(ofEpochMilli(currentTime).atZone(GMT)));
|
||||
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue);
|
||||
ResponseEntity<String> returnValue = ResponseEntity.ok()
|
||||
.eTag(changedEtagValue).lastModified(oneMinuteAgo).body("body");
|
||||
@@ -473,7 +472,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
public void shouldHandleIfNoneMatchIfUnmodifiedSince() throws Exception {
|
||||
String etagValue = "\"some-etag\"";
|
||||
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue);
|
||||
servletRequest.addHeader(HttpHeaders.IF_UNMODIFIED_SINCE, dateFormat.format(new Date().getTime()));
|
||||
servletRequest.addHeader(HttpHeaders.IF_UNMODIFIED_SINCE, RFC_1123_DATE_TIME.format(ofEpochMilli(new Date().getTime()).atZone(GMT)));
|
||||
ResponseEntity<String> returnValue = ResponseEntity.ok().eTag(etagValue).body("body");
|
||||
|
||||
initStringMessageConversion(MediaType.TEXT_PLAIN);
|
||||
@@ -538,7 +537,7 @@ public class HttpEntityMethodProcessorMockTests {
|
||||
}
|
||||
if (lastModified != -1) {
|
||||
assertEquals(1, servletResponse.getHeaderValues(HttpHeaders.LAST_MODIFIED).size());
|
||||
assertEquals(dateFormat.format(lastModified), servletResponse.getHeader(HttpHeaders.LAST_MODIFIED));
|
||||
assertEquals(RFC_1123_DATE_TIME.format(ofEpochMilli(lastModified).atZone(GMT)), servletResponse.getHeader(HttpHeaders.LAST_MODIFIED));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,13 +17,13 @@
|
||||
package org.springframework.web.servlet.resource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
@@ -45,6 +45,7 @@ import org.springframework.web.accept.ContentNegotiationManager;
|
||||
import org.springframework.web.accept.ContentNegotiationManagerFactoryBean;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
|
||||
import static java.time.format.DateTimeFormatter.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertThat;
|
||||
@@ -61,8 +62,6 @@ import static org.junit.Assert.fail;
|
||||
*/
|
||||
public class ResourceHttpRequestHandlerTests {
|
||||
|
||||
private SimpleDateFormat dateFormat;
|
||||
|
||||
private ResourceHttpRequestHandler handler;
|
||||
|
||||
private MockHttpServletRequest request;
|
||||
@@ -72,8 +71,6 @@ public class ResourceHttpRequestHandlerTests {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
|
||||
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
|
||||
List<Resource> paths = new ArrayList<>(2);
|
||||
paths.add(new ClassPathResource("test/", getClass()));
|
||||
@@ -623,7 +620,7 @@ public class ResourceHttpRequestHandlerTests {
|
||||
|
||||
|
||||
private long dateHeaderAsLong(String responseHeaderName) throws Exception {
|
||||
return dateFormat.parse(this.response.getHeader(responseHeaderName)).getTime();
|
||||
return ZonedDateTime.parse(this.response.getHeader(responseHeaderName), RFC_1123_DATE_TIME).toInstant().toEpochMilli();
|
||||
}
|
||||
|
||||
private long resourceLastModified(String resourceName) throws IOException {
|
||||
@@ -632,7 +629,7 @@ public class ResourceHttpRequestHandlerTests {
|
||||
|
||||
private String resourceLastModifiedDate(String resourceName) throws IOException {
|
||||
long lastModified = new ClassPathResource(resourceName, getClass()).getFile().lastModified();
|
||||
return dateFormat.format(lastModified);
|
||||
return RFC_1123_DATE_TIME.format(Instant.ofEpochMilli(lastModified).atZone(ZoneId.of("GMT")));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user