1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.hc.client5.http.impl.cache;
28
29 import java.time.Instant;
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.jupiter.api.Assertions;
35 import org.junit.jupiter.api.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 Assertions.assertEquals(1, result.length);
44 final WarningValue wv = result[0];
45 Assertions.assertEquals(110, wv.getWarnCode());
46 Assertions.assertEquals("fred", wv.getWarnAgent());
47 Assertions.assertEquals("\"stale\"", wv.getWarnText());
48 Assertions.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 Assertions.assertEquals(2, result.length);
56 WarningValue wv = result[0];
57 Assertions.assertEquals(110, wv.getWarnCode());
58 Assertions.assertEquals("fred", wv.getWarnAgent());
59 Assertions.assertEquals("\"stale\"", wv.getWarnText());
60 Assertions.assertNull(wv.getWarnDate());
61 wv = result[1];
62 Assertions.assertEquals(111, wv.getWarnCode());
63 Assertions.assertEquals("wilma", wv.getWarnAgent());
64 Assertions.assertEquals("\"other\"", wv.getWarnText());
65 Assertions.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 Assertions.assertEquals(2, result.length);
73 WarningValue wv = result[0];
74 Assertions.assertEquals(110, wv.getWarnCode());
75 Assertions.assertEquals("fred", wv.getWarnAgent());
76 Assertions.assertEquals("\"stale\"", wv.getWarnText());
77 Assertions.assertNull(wv.getWarnDate());
78 wv = result[1];
79 Assertions.assertEquals(111, wv.getWarnCode());
80 Assertions.assertEquals("wilma", wv.getWarnAgent());
81 Assertions.assertEquals("\"other\"", wv.getWarnText());
82 Assertions.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 Assertions.assertEquals(2, result.length);
90 WarningValue wv = result[0];
91 Assertions.assertEquals(110, wv.getWarnCode());
92 Assertions.assertEquals("fred", wv.getWarnAgent());
93 Assertions.assertEquals("\"stale\"", wv.getWarnText());
94 Assertions.assertNull(wv.getWarnDate());
95 wv = result[1];
96 Assertions.assertEquals(111, wv.getWarnCode());
97 Assertions.assertEquals("wilma", wv.getWarnAgent());
98 Assertions.assertEquals("\"other\"", wv.getWarnText());
99 Assertions.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 Assertions.assertEquals(2, result.length);
107 WarningValue wv = result[0];
108 Assertions.assertEquals(110, wv.getWarnCode());
109 Assertions.assertEquals("fred", wv.getWarnAgent());
110 Assertions.assertEquals("\"stale\"", wv.getWarnText());
111 Assertions.assertNull(wv.getWarnDate());
112 wv = result[1];
113 Assertions.assertEquals(111, wv.getWarnCode());
114 Assertions.assertEquals("wilma", wv.getWarnAgent());
115 Assertions.assertEquals("\"other\"", wv.getWarnText());
116 Assertions.assertNull(wv.getWarnDate());
117 }
118
119 @Test
120 public void testConstructSingleWarnValue() {
121 final WarningValue impl = new WarningValue("110 fred \"stale\"");
122 Assertions.assertEquals(110, impl.getWarnCode());
123 Assertions.assertEquals("fred", impl.getWarnAgent());
124 Assertions.assertEquals("\"stale\"", impl.getWarnText());
125 Assertions.assertNull(impl.getWarnDate());
126 }
127
128 @Test
129 public void testConstructWarnValueWithIPv4Address() {
130 final WarningValue impl = new WarningValue("110 192.168.1.1 \"stale\"");
131 Assertions.assertEquals(110, impl.getWarnCode());
132 Assertions.assertEquals("192.168.1.1", impl.getWarnAgent());
133 Assertions.assertEquals("\"stale\"", impl.getWarnText());
134 Assertions.assertNull(impl.getWarnDate());
135 }
136
137 @Test
138 public void testConstructWarnValueWithHostname() {
139 final WarningValue impl = new WarningValue("110 foo.example.com \"stale\"");
140 Assertions.assertEquals(110, impl.getWarnCode());
141 Assertions.assertEquals("foo.example.com", impl.getWarnAgent());
142 Assertions.assertEquals("\"stale\"", impl.getWarnText());
143 Assertions.assertNull(impl.getWarnDate());
144 }
145
146 @Test
147 public void testConstructWarnValueWithHostnameAndPort() {
148 final WarningValue impl = new WarningValue("110 foo.example.com:8080 \"stale\"");
149 Assertions.assertEquals(110, impl.getWarnCode());
150 Assertions.assertEquals("foo.example.com:8080", impl.getWarnAgent());
151 Assertions.assertEquals("\"stale\"", impl.getWarnText());
152 Assertions.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 Assertions.assertEquals(110, impl.getWarnCode());
159 Assertions.assertEquals("192.168.1.1:8080", impl.getWarnAgent());
160 Assertions.assertEquals("\"stale\"", impl.getWarnText());
161 Assertions.assertNull(impl.getWarnDate());
162 }
163
164 @Test
165 public void testConstructWarnValueWithPseudonym() {
166 final WarningValue impl = new WarningValue("110 ca$hm0ney \"stale\"");
167 Assertions.assertEquals(110, impl.getWarnCode());
168 Assertions.assertEquals("ca$hm0ney", impl.getWarnAgent());
169 Assertions.assertEquals("\"stale\"", impl.getWarnText());
170 Assertions.assertNull(impl.getWarnDate());
171 }
172
173 @Test
174 public void testConstructWarnValueWithTextWithSpaces() {
175 final WarningValue impl = new WarningValue("110 fred \"stale stuff\"");
176 Assertions.assertEquals(110, impl.getWarnCode());
177 Assertions.assertEquals("fred", impl.getWarnAgent());
178 Assertions.assertEquals("\"stale stuff\"", impl.getWarnText());
179 Assertions.assertNull(impl.getWarnDate());
180 }
181
182 @Test
183 public void testConstructWarnValueWithTextWithCommas() {
184 final WarningValue impl = new WarningValue("110 fred \"stale, stuff\"");
185 Assertions.assertEquals(110, impl.getWarnCode());
186 Assertions.assertEquals("fred", impl.getWarnAgent());
187 Assertions.assertEquals("\"stale, stuff\"", impl.getWarnText());
188 Assertions.assertNull(impl.getWarnDate());
189 }
190
191 @Test
192 public void testConstructWarnValueWithTextWithEscapedQuotes() {
193 final WarningValue impl = new WarningValue("110 fred \"stale\\\" stuff\"");
194 Assertions.assertEquals(110, impl.getWarnCode());
195 Assertions.assertEquals("fred", impl.getWarnAgent());
196 Assertions.assertEquals("\"stale\\\" stuff\"", impl.getWarnText());
197 Assertions.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 Assertions.assertEquals(110, impl.getWarnCode());
204 Assertions.assertEquals("fred", impl.getWarnAgent());
205 Assertions.assertEquals("\"stale\"", impl.getWarnText());
206 final Instant target = DateUtils.parseStandardDate("Sun Nov 6 08:49:37 1994");
207 Assertions.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 Assertions.assertEquals(110, impl.getWarnCode());
214 Assertions.assertEquals("fred", impl.getWarnAgent());
215 Assertions.assertEquals("\"stale\"", impl.getWarnText());
216 final Instant target = DateUtils.parseStandardDate("Sunday, 06-Nov-94 08:49:37 GMT");
217 Assertions.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 Assertions.assertEquals(110, impl.getWarnCode());
224 Assertions.assertEquals("fred", impl.getWarnAgent());
225 Assertions.assertEquals("\"stale\"", impl.getWarnText());
226 final Instant target = DateUtils.parseStandardDate("Sun, 06 Nov 1994 08:49:37 GMT");
227 Assertions.assertEquals(target, impl.getWarnDate());
228 }
229
230 }