Leverage ZonedDateTime in HttpHeaders
This commit introduces 2 new public methods in HttpHeaders in order to leverage Java 8 ZonedDateTime in addition to the existing long (with GMT time zone implied) variants: - ZonedDateTime getFirstZonedDateTime(String headerName) - void setZonedDateTime(String headerName, ZonedDateTime date) This commit also leverages Java 8 thread-safe DateTimeFormatter for HttpHeader implementation instead of SimpleDateFormat. As a consequence of the usage of DateTimeFormatter.RFC_1123_DATE_TIME, HTTP date header serialization could change slightly for single digit days from for example "Thu, 01 Jan 1970 00:00:00 GMT" to "Thu, 1 Jan 1970 00:00:00 GMT". Issue: SPR-15661
This commit is contained in:
@@ -21,6 +21,8 @@ import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
@@ -34,6 +36,7 @@ import java.util.TimeZone;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Test;
|
||||
|
||||
import static java.time.format.DateTimeFormatter.*;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
@@ -466,4 +469,40 @@ public class HttpHeadersTests {
|
||||
assertEquals("Expected one (first) locale", Locale.GERMAN, headers.getContentLanguage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void firstDate() {
|
||||
headers.setDate(HttpHeaders.DATE, 1229595600000L);
|
||||
assertThat(headers.getFirstDate(HttpHeaders.DATE), is(1229595600000L));
|
||||
|
||||
headers.clear();
|
||||
|
||||
headers.add(HttpHeaders.DATE, "Thu, 18 Dec 2008 10:20:00 GMT");
|
||||
headers.add(HttpHeaders.DATE, "Sat, 18 Dec 2010 10:20:00 GMT");
|
||||
assertThat(headers.getFirstDate(HttpHeaders.DATE), is(1229595600000L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void firstZonedDateTime() {
|
||||
ZonedDateTime date = ZonedDateTime.of(2017, 6, 22, 22, 22, 0, 0, ZoneId.of("GMT"));
|
||||
headers.setZonedDateTime(HttpHeaders.DATE, date);
|
||||
assertThat(headers.getFirst(HttpHeaders.DATE), is("Thu, 22 Jun 2017 22:22:00 GMT"));
|
||||
assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date));
|
||||
|
||||
headers.clear();
|
||||
ZonedDateTime otherDate = ZonedDateTime.of(2010, 12, 18, 10, 20, 0, 0, ZoneId.of("GMT"));
|
||||
headers.add(HttpHeaders.DATE, RFC_1123_DATE_TIME.format(date));
|
||||
headers.add(HttpHeaders.DATE, RFC_1123_DATE_TIME.format(otherDate));
|
||||
assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date));
|
||||
|
||||
// obsolete RFC 850 format
|
||||
headers.clear();
|
||||
headers.set(HttpHeaders.DATE, "Thursday, 22-Jun-17 22:22:00 GMT");
|
||||
assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date));
|
||||
|
||||
// ANSI C's asctime() format
|
||||
headers.clear();
|
||||
headers.set(HttpHeaders.DATE, "Thu Jun 22 22:22:00 2017");
|
||||
assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ public class RequestEntityTests {
|
||||
|
||||
assertEquals("text/plain", responseHeaders.getFirst("Accept"));
|
||||
assertEquals("utf-8", responseHeaders.getFirst("Accept-Charset"));
|
||||
assertEquals("Thu, 01 Jan 1970 00:00:12 GMT", responseHeaders.getFirst("If-Modified-Since"));
|
||||
assertEquals("Thu, 1 Jan 1970 00:00:12 GMT", responseHeaders.getFirst("If-Modified-Since"));
|
||||
assertEquals(ifNoneMatch, responseHeaders.getFirst("If-None-Match"));
|
||||
assertEquals(String.valueOf(contentLength), responseHeaders.getFirst("Content-Length"));
|
||||
assertEquals(contentType.toString(), responseHeaders.getFirst("Content-Type"));
|
||||
|
||||
@@ -160,7 +160,7 @@ public class ResponseEntityTests {
|
||||
HttpHeaders responseHeaders = responseEntity.getHeaders();
|
||||
|
||||
assertEquals("GET", responseHeaders.getFirst("Allow"));
|
||||
assertEquals("Thu, 01 Jan 1970 00:00:12 GMT",
|
||||
assertEquals("Thu, 1 Jan 1970 00:00:12 GMT",
|
||||
responseHeaders.getFirst("Last-Modified"));
|
||||
assertEquals(location.toASCIIString(),
|
||||
responseHeaders.getFirst("Location"));
|
||||
|
||||
Reference in New Issue
Block a user