View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.client5.http.utils;
29  
30  import java.time.Instant;
31  import java.time.LocalDate;
32  import java.time.Month;
33  import java.time.ZoneId;
34  import java.time.format.DateTimeFormatter;
35  import java.time.temporal.ChronoUnit;
36  
37  import org.apache.hc.core5.http.HttpHeaders;
38  import org.apache.hc.core5.http.message.BasicHeader;
39  import org.apache.hc.core5.http.message.HeaderGroup;
40  import org.junit.jupiter.api.Assertions;
41  import org.junit.jupiter.api.Test;
42  
43  /**
44   * Unit tests for {@link DateUtils}.
45   */
46  class TestDateUtils {
47  
48      private static Instant createInstant(final int year, final Month month, final int day) {
49          return LocalDate.of(year, month, day).atStartOfDay(ZoneId.of("GMT")).toInstant();
50      }
51  
52      @Test
53      void testBasicDateParse() {
54          final Instant instant = createInstant(2005, Month.OCTOBER, 14);
55          Assertions.assertEquals(instant, DateUtils.parseDate("Fri, 14 Oct 2005 00:00:00 GMT", DateUtils.FORMATTER_RFC1123));
56          Assertions.assertEquals(instant, DateUtils.parseDate("Friday, 14 Oct 2005 00:00:00 GMT", DateUtils.FORMATTER_RFC1123));
57          Assertions.assertEquals(instant, DateUtils.parseDate("Fri, 14-Oct-2005 00:00:00 GMT", DateUtils.FORMATTER_RFC1036));
58          Assertions.assertEquals(instant, DateUtils.parseDate("Friday, 14-Oct-2005 00:00:00 GMT", DateUtils.FORMATTER_RFC1036));
59          Assertions.assertEquals(instant.minus(2, ChronoUnit.HOURS),
60                  DateUtils.parseDate("Fri, 14 Oct 2005 00:00:00 CET", DateUtils.FORMATTER_RFC1123));
61          Assertions.assertEquals(instant.minus(2, ChronoUnit.HOURS),
62                  DateUtils.parseDate("Fri, 14-Oct-05 00:00:00 CET", DateUtils.FORMATTER_RFC1036));
63          Assertions.assertEquals(instant, DateUtils.parseStandardDate("Fri, 14 Oct 2005 00:00:00 GMT"));
64      }
65  
66      @Test
67      void testDateParseMessage() {
68          final HeaderGroup message1 = new HeaderGroup();
69          message1.setHeader(new BasicHeader(HttpHeaders.DATE, "Fri, 14 Oct 2005 00:00:00 GMT"));
70          Assertions.assertEquals(createInstant(2005, Month.OCTOBER, 14), DateUtils.parseStandardDate(message1, HttpHeaders.DATE));
71  
72          final HeaderGroup message2 = new HeaderGroup();
73          message2.addHeader(new BasicHeader(HttpHeaders.DATE, "Fri, 14 Oct 2005 00:00:00 GMT"));
74          message2.addHeader(new BasicHeader(HttpHeaders.DATE, "Fri, 21 Oct 2005 00:00:00 GMT"));
75          Assertions.assertEquals(createInstant(2005, Month.OCTOBER, 14), DateUtils.parseStandardDate(message2, HttpHeaders.DATE));
76      }
77  
78      @Test
79      void testMalformedDate() {
80          Assertions.assertNull(DateUtils.parseDate("Fri, 14 Oct 2005 00:00:00 GMT", new DateTimeFormatter[] {}));
81          Assertions.assertNull(DateUtils.parseDate("Thu Feb 22 17:20:18 2024", new DateTimeFormatter[] {}));
82      }
83  
84      @Test
85      void testInvalidInput() {
86          Assertions.assertThrows(NullPointerException.class, () -> DateUtils.parseStandardDate(null));
87          Assertions.assertThrows(NullPointerException.class, () -> DateUtils.formatStandardDate(null));
88      }
89  
90      @Test
91      void testTwoDigitYearDateParse() {
92          Assertions.assertEquals(createInstant(2005, Month.OCTOBER, 14),
93                  DateUtils.parseDate("Friday, 14-Oct-05 00:00:00 GMT", DateUtils.FORMATTER_RFC1036));
94      }
95  
96      @Test
97      void testParseQuotedDate() {
98          Assertions.assertEquals(createInstant(2005, Month.OCTOBER, 14),
99                  DateUtils.parseDate("'Fri, 14 Oct 2005 00:00:00 GMT'", DateUtils.FORMATTER_RFC1123));
100     }
101 
102     @Test
103     void testBasicDateFormat() {
104         final Instant instant = createInstant(2005, Month.OCTOBER, 14);
105         Assertions.assertEquals("Fri, 14 Oct 2005 00:00:00 GMT", DateUtils.formatStandardDate(instant));
106         Assertions.assertEquals("Fri, 14 Oct 2005 00:00:00 GMT", DateUtils.formatDate(instant, DateUtils.FORMATTER_RFC1123));
107         Assertions.assertEquals("Fri, 14-Oct-05 00:00:00 GMT", DateUtils.formatDate(instant, DateUtils.FORMATTER_RFC1036));
108         Assertions.assertEquals("Fri Oct 14 00:00:00 2005", DateUtils.formatDate(instant, DateUtils.FORMATTER_ASCTIME));
109     }
110 
111 }