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  package org.apache.hc.client5.http.impl.cache;
28  
29  import java.util.Date;
30  
31  import org.apache.hc.client5.http.utils.DateUtils;
32  import org.apache.hc.core5.http.Header;
33  import org.apache.hc.core5.http.message.BasicHeader;
34  import org.junit.Assert;
35  import org.junit.Test;
36  
37  public class TestWarningValue {
38  
39      @Test
40      public void testParseSingleWarnValue() {
41          final Header h = new BasicHeader("Warning","110 fred \"stale\"");
42          final WarningValue[] result = WarningValue.getWarningValues(h);
43          Assert.assertEquals(1, result.length);
44          final WarningValue wv = result[0];
45          Assert.assertEquals(110, wv.getWarnCode());
46          Assert.assertEquals("fred", wv.getWarnAgent());
47          Assert.assertEquals("\"stale\"", wv.getWarnText());
48          Assert.assertNull(wv.getWarnDate());
49      }
50  
51      @Test
52      public void testParseMultipleWarnValues() {
53          final Header h = new BasicHeader("Warning","110 fred \"stale\", 111 wilma \"other\"");
54          final WarningValue[] result = WarningValue.getWarningValues(h);
55          Assert.assertEquals(2, result.length);
56          WarningValue wv = result[0];
57          Assert.assertEquals(110, wv.getWarnCode());
58          Assert.assertEquals("fred", wv.getWarnAgent());
59          Assert.assertEquals("\"stale\"", wv.getWarnText());
60          Assert.assertNull(wv.getWarnDate());
61          wv = result[1];
62          Assert.assertEquals(111, wv.getWarnCode());
63          Assert.assertEquals("wilma", wv.getWarnAgent());
64          Assert.assertEquals("\"other\"", wv.getWarnText());
65          Assert.assertNull(wv.getWarnDate());
66      }
67  
68      @Test
69      public void testMidHeaderParseErrorRecovery() {
70          final Header h = new BasicHeader("Warning","110 fred \"stale\", bogus, 111 wilma \"other\"");
71          final WarningValue[] result = WarningValue.getWarningValues(h);
72          Assert.assertEquals(2, result.length);
73          WarningValue wv = result[0];
74          Assert.assertEquals(110, wv.getWarnCode());
75          Assert.assertEquals("fred", wv.getWarnAgent());
76          Assert.assertEquals("\"stale\"", wv.getWarnText());
77          Assert.assertNull(wv.getWarnDate());
78          wv = result[1];
79          Assert.assertEquals(111, wv.getWarnCode());
80          Assert.assertEquals("wilma", wv.getWarnAgent());
81          Assert.assertEquals("\"other\"", wv.getWarnText());
82          Assert.assertNull(wv.getWarnDate());
83      }
84  
85      @Test
86      public void testTrickyCommaMidHeaderParseErrorRecovery() {
87          final Header h = new BasicHeader("Warning","110 fred \"stale\", \"bogus, dude\", 111 wilma \"other\"");
88          final WarningValue[] result = WarningValue.getWarningValues(h);
89          Assert.assertEquals(2, result.length);
90          WarningValue wv = result[0];
91          Assert.assertEquals(110, wv.getWarnCode());
92          Assert.assertEquals("fred", wv.getWarnAgent());
93          Assert.assertEquals("\"stale\"", wv.getWarnText());
94          Assert.assertNull(wv.getWarnDate());
95          wv = result[1];
96          Assert.assertEquals(111, wv.getWarnCode());
97          Assert.assertEquals("wilma", wv.getWarnAgent());
98          Assert.assertEquals("\"other\"", wv.getWarnText());
99          Assert.assertNull(wv.getWarnDate());
100     }
101 
102     @Test
103     public void testParseErrorRecoveryAtEndOfHeader() {
104         final Header h = new BasicHeader("Warning","110 fred \"stale\", 111 wilma \"other\", \"bogus, dude\"");
105         final WarningValue[] result = WarningValue.getWarningValues(h);
106         Assert.assertEquals(2, result.length);
107         WarningValue wv = result[0];
108         Assert.assertEquals(110, wv.getWarnCode());
109         Assert.assertEquals("fred", wv.getWarnAgent());
110         Assert.assertEquals("\"stale\"", wv.getWarnText());
111         Assert.assertNull(wv.getWarnDate());
112         wv = result[1];
113         Assert.assertEquals(111, wv.getWarnCode());
114         Assert.assertEquals("wilma", wv.getWarnAgent());
115         Assert.assertEquals("\"other\"", wv.getWarnText());
116         Assert.assertNull(wv.getWarnDate());
117     }
118 
119     @Test
120     public void testConstructSingleWarnValue() {
121         final WarningValue impl = new WarningValue("110 fred \"stale\"");
122         Assert.assertEquals(110, impl.getWarnCode());
123         Assert.assertEquals("fred", impl.getWarnAgent());
124         Assert.assertEquals("\"stale\"", impl.getWarnText());
125         Assert.assertNull(impl.getWarnDate());
126     }
127 
128     @Test
129     public void testConstructWarnValueWithIPv4Address() {
130         final WarningValue impl = new WarningValue("110 192.168.1.1 \"stale\"");
131         Assert.assertEquals(110, impl.getWarnCode());
132         Assert.assertEquals("192.168.1.1", impl.getWarnAgent());
133         Assert.assertEquals("\"stale\"", impl.getWarnText());
134         Assert.assertNull(impl.getWarnDate());
135     }
136 
137     @Test
138     public void testConstructWarnValueWithHostname() {
139         final WarningValue impl = new WarningValue("110 foo.example.com \"stale\"");
140         Assert.assertEquals(110, impl.getWarnCode());
141         Assert.assertEquals("foo.example.com", impl.getWarnAgent());
142         Assert.assertEquals("\"stale\"", impl.getWarnText());
143         Assert.assertNull(impl.getWarnDate());
144     }
145 
146     @Test
147     public void testConstructWarnValueWithHostnameAndPort() {
148         final WarningValue impl = new WarningValue("110 foo.example.com:8080 \"stale\"");
149         Assert.assertEquals(110, impl.getWarnCode());
150         Assert.assertEquals("foo.example.com:8080", impl.getWarnAgent());
151         Assert.assertEquals("\"stale\"", impl.getWarnText());
152         Assert.assertNull(impl.getWarnDate());
153     }
154 
155     @Test
156     public void testConstructWarnValueWithIPv4AddressAndPort() {
157         final WarningValue impl = new WarningValue("110 192.168.1.1:8080 \"stale\"");
158         Assert.assertEquals(110, impl.getWarnCode());
159         Assert.assertEquals("192.168.1.1:8080", impl.getWarnAgent());
160         Assert.assertEquals("\"stale\"", impl.getWarnText());
161         Assert.assertNull(impl.getWarnDate());
162     }
163 
164     @Test
165     public void testConstructWarnValueWithPseudonym() {
166         final WarningValue impl = new WarningValue("110 ca$hm0ney \"stale\"");
167         Assert.assertEquals(110, impl.getWarnCode());
168         Assert.assertEquals("ca$hm0ney", impl.getWarnAgent());
169         Assert.assertEquals("\"stale\"", impl.getWarnText());
170         Assert.assertNull(impl.getWarnDate());
171     }
172 
173     @Test
174     public void testConstructWarnValueWithTextWithSpaces() {
175         final WarningValue impl = new WarningValue("110 fred \"stale stuff\"");
176         Assert.assertEquals(110, impl.getWarnCode());
177         Assert.assertEquals("fred", impl.getWarnAgent());
178         Assert.assertEquals("\"stale stuff\"", impl.getWarnText());
179         Assert.assertNull(impl.getWarnDate());
180     }
181 
182     @Test
183     public void testConstructWarnValueWithTextWithCommas() {
184         final WarningValue impl = new WarningValue("110 fred \"stale, stuff\"");
185         Assert.assertEquals(110, impl.getWarnCode());
186         Assert.assertEquals("fred", impl.getWarnAgent());
187         Assert.assertEquals("\"stale, stuff\"", impl.getWarnText());
188         Assert.assertNull(impl.getWarnDate());
189     }
190 
191     @Test
192     public void testConstructWarnValueWithTextWithEscapedQuotes() {
193         final WarningValue impl = new WarningValue("110 fred \"stale\\\" stuff\"");
194         Assert.assertEquals(110, impl.getWarnCode());
195         Assert.assertEquals("fred", impl.getWarnAgent());
196         Assert.assertEquals("\"stale\\\" stuff\"", impl.getWarnText());
197         Assert.assertNull(impl.getWarnDate());
198     }
199 
200     @Test
201     public void testConstructWarnValueWithAscTimeWarnDate() throws Exception {
202         final WarningValue impl = new WarningValue("110 fred \"stale\" \"Sun Nov  6 08:49:37 1994\"");
203         Assert.assertEquals(110, impl.getWarnCode());
204         Assert.assertEquals("fred", impl.getWarnAgent());
205         Assert.assertEquals("\"stale\"", impl.getWarnText());
206         final Date target = DateUtils.parseDate("Sun Nov  6 08:49:37 1994");
207         Assert.assertEquals(target, impl.getWarnDate());
208     }
209 
210     @Test
211     public void testConstructWarnValueWithRFC850WarnDate() throws Exception {
212         final WarningValue impl = new WarningValue("110 fred \"stale\" \"Sunday, 06-Nov-94 08:49:37 GMT\"");
213         Assert.assertEquals(110, impl.getWarnCode());
214         Assert.assertEquals("fred", impl.getWarnAgent());
215         Assert.assertEquals("\"stale\"", impl.getWarnText());
216         final Date target = DateUtils.parseDate("Sunday, 06-Nov-94 08:49:37 GMT");
217         Assert.assertEquals(target, impl.getWarnDate());
218     }
219 
220     @Test
221     public void testConstructWarnValueWithRFC1123WarnDate() throws Exception {
222         final WarningValue impl = new WarningValue("110 fred \"stale\" \"Sun, 06 Nov 1994 08:49:37 GMT\"");
223         Assert.assertEquals(110, impl.getWarnCode());
224         Assert.assertEquals("fred", impl.getWarnAgent());
225         Assert.assertEquals("\"stale\"", impl.getWarnText());
226         final Date target = DateUtils.parseDate("Sun, 06 Nov 1994 08:49:37 GMT");
227         Assert.assertEquals(target, impl.getWarnDate());
228     }
229 
230 }