SLING-7797 make sure IllegalArgumentException is thrown when trying to parse an invalid date value
diff --git a/src/main/java/org/apache/sling/servlethelpers/HeaderSupport.java b/src/main/java/org/apache/sling/servlethelpers/HeaderSupport.java
index d644a74..24f0b63 100644
--- a/src/main/java/org/apache/sling/servlethelpers/HeaderSupport.java
+++ b/src/main/java/org/apache/sling/servlethelpers/HeaderSupport.java
@@ -22,6 +22,7 @@
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
@@ -158,7 +159,12 @@
}
private static synchronized Calendar parseDate(String dateString) {
- return GregorianCalendar.from(ZonedDateTime.parse(dateString, RFC_1123_DATE_TIME));
+ try {
+ return GregorianCalendar.from(ZonedDateTime.parse(dateString, RFC_1123_DATE_TIME));
+ }
+ catch (DateTimeParseException ex) {
+ throw new IllegalArgumentException("Invalid date value: " + dateString, ex);
+ }
}
}
diff --git a/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequestTest.java b/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequestTest.java
index 36ba6a8..3f74230 100644
--- a/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequestTest.java
+++ b/src/test/java/org/apache/sling/servlethelpers/MockSlingHttpServletRequestTest.java
@@ -269,6 +269,13 @@
assertFalse(header2Values.hasMoreElements());
}
+ @Test(expected = IllegalArgumentException.class)
+ public void testInvalidDateHeader() {
+ request.addHeader("header1", "thisIsNotADate");
+ // make sure IllegalArgumentException is thrown as defined by the HttpServletRequest API
+ request.getDateHeader("header1");
+ }
+
@Test
public void testCookies() {
assertNull(request.getCookies());