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
28 package org.apache.hc.client5.http.impl.cookie;
29
30 import java.time.LocalDateTime;
31 import java.time.temporal.ChronoField;
32
33 import org.apache.hc.client5.http.cookie.CookieAttributeHandler;
34 import org.apache.hc.client5.http.cookie.MalformedCookieException;
35 import org.apache.hc.client5.http.utils.DateUtils;
36 import org.junit.jupiter.api.Assertions;
37 import org.junit.jupiter.api.Test;
38
39 class TestLaxCookieAttribHandlers {
40
41 @Test
42 void testParseMaxAge() throws Exception {
43 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
44 final CookieAttributeHandler h = LaxMaxAgeHandler.INSTANCE;
45 h.parse(cookie, "2000");
46 Assertions.assertNotNull(cookie.getExpiryInstant());
47 }
48
49 @Test
50 void testParseMaxNegative() throws Exception {
51 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
52 final CookieAttributeHandler h = LaxMaxAgeHandler.INSTANCE;
53 h.parse(cookie, "-2000");
54 Assertions.assertNotNull(cookie.getExpiryInstant());
55 }
56
57 @Test
58 void testParseMaxZero() throws Exception {
59 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
60 final CookieAttributeHandler h = LaxMaxAgeHandler.INSTANCE;
61 h.parse(cookie, "0000");
62 Assertions.assertNotNull(cookie.getExpiryInstant());
63 }
64
65 @Test
66 void testBasicMaxAgeParseEmpty() throws Exception {
67 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
68 final CookieAttributeHandler h = LaxMaxAgeHandler.INSTANCE;
69 h.parse(cookie, " ");
70 Assertions.assertNull(cookie.getExpiryInstant());
71 }
72
73 @Test
74 void testBasicMaxAgeParseInvalid() throws Exception {
75 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
76 final CookieAttributeHandler h = LaxMaxAgeHandler.INSTANCE;
77 h.parse(cookie, "garbage");
78 Assertions.assertNull(cookie.getExpiryInstant());
79 }
80
81 @Test
82 void testBasicMaxAgeInvalidInput() {
83 final CookieAttributeHandler h = LaxMaxAgeHandler.INSTANCE;
84 Assertions.assertThrows(NullPointerException.class, () -> h.parse(null, "stuff"));
85 }
86
87 @Test
88 void testExpiryGarbage() {
89 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
90 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
91 Assertions.assertThrows(MalformedCookieException.class, () ->
92 h.parse(cookie, ";;blah,blah;yada "));
93 }
94
95 @Test
96 void testParseExpiry() throws Exception {
97 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
98 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
99 h.parse(cookie, "1:0:12 8-jan-2012");
100
101 final LocalDateTime expiryDate = DateUtils.toUTC(cookie.getExpiryInstant());
102 Assertions.assertNotNull(expiryDate);
103
104 Assertions.assertEquals(2012, expiryDate.get(ChronoField.YEAR));
105 Assertions.assertEquals(1, expiryDate.get(ChronoField.MONTH_OF_YEAR));
106 Assertions.assertEquals(8, expiryDate.get(ChronoField.DAY_OF_MONTH));
107 Assertions.assertEquals(1, expiryDate.get(ChronoField.HOUR_OF_DAY));
108 Assertions.assertEquals(0, expiryDate.get(ChronoField.MINUTE_OF_HOUR));
109 Assertions.assertEquals(12, expiryDate.get(ChronoField.SECOND_OF_MINUTE));
110 Assertions.assertEquals(0, expiryDate.get(ChronoField.MILLI_OF_SECOND));
111 }
112
113 @Test
114 void testParseExpiryInstant() throws Exception {
115 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
116 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
117 h.parse(cookie, "1:0:12 8-jan-2012");
118
119 final LocalDateTime expiryDate = DateUtils.toUTC(cookie.getExpiryInstant());
120 Assertions.assertNotNull(expiryDate);
121
122 Assertions.assertEquals(2012, expiryDate.get(ChronoField.YEAR));
123 Assertions.assertEquals(1, expiryDate.get(ChronoField.MONTH_OF_YEAR));
124 Assertions.assertEquals(8, expiryDate.get(ChronoField.DAY_OF_MONTH));
125 Assertions.assertEquals(1, expiryDate.get(ChronoField.HOUR_OF_DAY));
126 Assertions.assertEquals(0, expiryDate.get(ChronoField.MINUTE_OF_HOUR));
127 Assertions.assertEquals(12, expiryDate.get(ChronoField.SECOND_OF_MINUTE));
128 Assertions.assertEquals(0, expiryDate.get(ChronoField.MILLI_OF_SECOND));
129 }
130
131 @Test
132 void testParseExpiryInvalidTime0() throws Exception {
133 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
134 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
135 h.parse(cookie, null);
136 Assertions.assertNull(cookie.getExpiryInstant());
137 }
138
139 @Test
140 void testParseExpiryInvalidTime1() {
141 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
142 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
143 Assertions.assertThrows(MalformedCookieException.class, () ->
144 h.parse(cookie, "1:0:122 8 dec 1980"));
145 }
146
147 @Test
148 void testParseExpiryInvalidTime2() {
149 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
150 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
151 Assertions.assertThrows(MalformedCookieException.class, () ->
152 h.parse(cookie, "24:00:00 8 dec 1980"));
153 }
154
155 @Test
156 void testParseExpiryInvalidTime3() {
157 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
158 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
159 Assertions.assertThrows(MalformedCookieException.class, () ->
160 h.parse(cookie, "23:60:00 8 dec 1980"));
161 }
162
163 @Test
164 void testParseExpiryInvalidTime4() {
165 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
166 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
167 Assertions.assertThrows(MalformedCookieException.class, () ->
168 h.parse(cookie, "23:00:60 8 dec 1980"));
169 }
170
171 @Test
172 void testParseExpiryFunnyTime() throws Exception {
173 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
174 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
175 h.parse(cookie, "1:59:00blah; 8-feb-2000");
176
177 final LocalDateTime expiryDate = DateUtils.toUTC(cookie.getExpiryInstant());
178 Assertions.assertNotNull(expiryDate);
179
180 Assertions.assertEquals(2000, expiryDate.get(ChronoField.YEAR_OF_ERA));
181 Assertions.assertEquals(2, expiryDate.get(ChronoField.MONTH_OF_YEAR));
182 Assertions.assertEquals(8, expiryDate.get(ChronoField.DAY_OF_MONTH));
183 Assertions.assertEquals(1, expiryDate.get(ChronoField.HOUR_OF_DAY));
184 Assertions.assertEquals(59, expiryDate.get(ChronoField.MINUTE_OF_HOUR));
185 Assertions.assertEquals(0, expiryDate.get(ChronoField.SECOND_OF_MINUTE));
186 Assertions.assertEquals(0, expiryDate.get(ChronoField.MILLI_OF_SECOND));
187 }
188
189 @Test
190 void testParseExpiryFunnyTimeInstant() throws Exception {
191 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
192 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
193 h.parse(cookie, "1:59:00blah; 8-feb-2000");
194
195 final LocalDateTime expiryDate = DateUtils.toUTC(cookie.getExpiryInstant());
196 Assertions.assertNotNull(expiryDate);
197
198 Assertions.assertEquals(2000, expiryDate.get(ChronoField.YEAR_OF_ERA));
199 Assertions.assertEquals(2, expiryDate.get(ChronoField.MONTH_OF_YEAR));
200 Assertions.assertEquals(8, expiryDate.get(ChronoField.DAY_OF_MONTH));
201 Assertions.assertEquals(1, expiryDate.get(ChronoField.HOUR_OF_DAY));
202 Assertions.assertEquals(59, expiryDate.get(ChronoField.MINUTE_OF_HOUR));
203 Assertions.assertEquals(0, expiryDate.get(ChronoField.SECOND_OF_MINUTE));
204 Assertions.assertEquals(0, expiryDate.get(ChronoField.MILLI_OF_SECOND));
205 }
206
207 @Test
208 void testParseExpiryInvalidDayOfMonth1() {
209 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
210 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
211 Assertions.assertThrows(MalformedCookieException.class, () ->
212 h.parse(cookie, "12:00:00 888 mar 1880"));
213 }
214
215 @Test
216 void testParseExpiryInvalidDayOfMonth2() {
217 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
218 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
219 Assertions.assertThrows(MalformedCookieException.class, () ->
220 h.parse(cookie, "12:00:00 0 mar 1880"));
221 }
222
223 @Test
224 void testParseExpiryInvalidDayOfMonth3() {
225 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
226 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
227 Assertions.assertThrows(MalformedCookieException.class, () ->
228 h.parse(cookie, "12:00:00 32 mar 1880"));
229 }
230
231 @Test
232 void testParseExpiryFunnyDayOfMonth() throws Exception {
233 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
234 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
235 h.parse(cookie, "12:00:00 8blah;mar;1880");
236
237 final LocalDateTime expiryDate = DateUtils.toUTC(cookie.getExpiryInstant());
238 Assertions.assertNotNull(expiryDate);
239
240 Assertions.assertEquals(1880, expiryDate.get(ChronoField.YEAR));
241 Assertions.assertEquals(3, expiryDate.get(ChronoField.MONTH_OF_YEAR));
242 Assertions.assertEquals(8, expiryDate.get(ChronoField.DAY_OF_MONTH));
243 Assertions.assertEquals(12, expiryDate.get(ChronoField.HOUR_OF_DAY));
244 Assertions.assertEquals(0, expiryDate.get(ChronoField.MINUTE_OF_HOUR));
245 Assertions.assertEquals(0, expiryDate.get(ChronoField.SECOND_OF_MINUTE));
246 Assertions.assertEquals(0, expiryDate.get(ChronoField.MILLI_OF_SECOND));
247 }
248
249 @Test
250 void testParseExpiryFunnyDayOfMonthInstant() throws Exception {
251 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
252 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
253 h.parse(cookie, "12:00:00 8blah;mar;1880");
254
255 final LocalDateTime expiryDate = DateUtils.toUTC(cookie.getExpiryInstant());
256 Assertions.assertNotNull(expiryDate);
257
258 Assertions.assertEquals(1880, expiryDate.get(ChronoField.YEAR));
259 Assertions.assertEquals(3, expiryDate.get(ChronoField.MONTH_OF_YEAR));
260 Assertions.assertEquals(8, expiryDate.get(ChronoField.DAY_OF_MONTH));
261 Assertions.assertEquals(12, expiryDate.get(ChronoField.HOUR_OF_DAY));
262 Assertions.assertEquals(0, expiryDate.get(ChronoField.MINUTE_OF_HOUR));
263 Assertions.assertEquals(0, expiryDate.get(ChronoField.SECOND_OF_MINUTE));
264 Assertions.assertEquals(0, expiryDate.get(ChronoField.MILLI_OF_SECOND));
265 }
266
267 @Test
268 void testParseExpiryInvalidMonth() {
269 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
270 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
271 Assertions.assertThrows(MalformedCookieException.class, () ->
272 h.parse(cookie, "1:00:00 8 dek 80"));
273 }
274
275 @Test
276 void testParseExpiryFunnyMonth() throws Exception {
277 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
278 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
279 h.parse(cookie, "23:59:59; 1-ApriLLLLL-2008");
280
281 final LocalDateTime expiryDate = DateUtils.toUTC(cookie.getExpiryInstant());
282 Assertions.assertNotNull(expiryDate);
283
284 Assertions.assertEquals(2008, expiryDate.get(ChronoField.YEAR));
285 Assertions.assertEquals(4, expiryDate.get(ChronoField.MONTH_OF_YEAR));
286 Assertions.assertEquals(1, expiryDate.get(ChronoField.DAY_OF_MONTH));
287 Assertions.assertEquals(23, expiryDate.get(ChronoField.HOUR_OF_DAY));
288 Assertions.assertEquals(59, expiryDate.get(ChronoField.MINUTE_OF_HOUR));
289 Assertions.assertEquals(59, expiryDate.get(ChronoField.SECOND_OF_MINUTE));
290 Assertions.assertEquals(0, expiryDate.get(ChronoField.MILLI_OF_SECOND));
291 }
292
293 @Test
294 void testParseExpiryFunnyMonthInstant() throws Exception {
295 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
296 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
297 h.parse(cookie, "23:59:59; 1-ApriLLLLL-2008");
298
299 final LocalDateTime expiryDate = DateUtils.toUTC(cookie.getExpiryInstant());
300 Assertions.assertNotNull(expiryDate);
301
302 Assertions.assertEquals(2008, expiryDate.get(ChronoField.YEAR));
303 Assertions.assertEquals(4, expiryDate.get(ChronoField.MONTH_OF_YEAR));
304 Assertions.assertEquals(1, expiryDate.get(ChronoField.DAY_OF_MONTH));
305 Assertions.assertEquals(23, expiryDate.get(ChronoField.HOUR_OF_DAY));
306 Assertions.assertEquals(59, expiryDate.get(ChronoField.MINUTE_OF_HOUR));
307 Assertions.assertEquals(59, expiryDate.get(ChronoField.SECOND_OF_MINUTE));
308 Assertions.assertEquals(0, expiryDate.get(ChronoField.MILLI_OF_SECOND));
309 }
310
311 @Test
312 void testParseExpiryInvalidYearTooShort() {
313 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
314 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
315 Assertions.assertThrows(MalformedCookieException.class, () ->
316 h.parse(cookie, "1:00:00 8 dec 8"));
317 }
318
319 @Test
320 void testParseExpiryInvalidYearTooLong() {
321 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
322 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
323 Assertions.assertThrows(MalformedCookieException.class, () ->
324 h.parse(cookie, "1:00:00 8 dec 88888"));
325 }
326
327 @Test
328 void testParseExpiryInvalidYearTooLongAgo() {
329 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
330 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
331 Assertions.assertThrows(MalformedCookieException.class, () ->
332 h.parse(cookie, "1:00:00 8 dec 1600"));
333 }
334
335 @Test
336 void testParseExpiryFunnyYear() throws Exception {
337 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
338 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
339 h.parse(cookie, "23:59:59; 1-Apr-2008blah");
340
341 final LocalDateTime expiryDate = DateUtils.toUTC(cookie.getExpiryInstant());
342 Assertions.assertNotNull(expiryDate);
343
344 Assertions.assertEquals(2008, expiryDate.get(ChronoField.YEAR));
345 Assertions.assertEquals(4, expiryDate.get(ChronoField.MONTH_OF_YEAR));
346 Assertions.assertEquals(1, expiryDate.get(ChronoField.DAY_OF_MONTH));
347 Assertions.assertEquals(23, expiryDate.get(ChronoField.HOUR_OF_DAY));
348 Assertions.assertEquals(59, expiryDate.get(ChronoField.MINUTE_OF_HOUR));
349 Assertions.assertEquals(59, expiryDate.get(ChronoField.SECOND_OF_MINUTE));
350 Assertions.assertEquals(0, expiryDate.get(ChronoField.MILLI_OF_SECOND));
351 }
352
353 @Test
354 void testParseExpiryFunnyYearInstant() throws Exception {
355 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
356 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
357 h.parse(cookie, "23:59:59; 1-Apr-2008blah");
358
359 final LocalDateTime expiryDate = DateUtils.toUTC(cookie.getExpiryInstant());
360 Assertions.assertNotNull(expiryDate);
361
362 Assertions.assertEquals(2008, expiryDate.get(ChronoField.YEAR));
363 Assertions.assertEquals(4, expiryDate.get(ChronoField.MONTH_OF_YEAR));
364 Assertions.assertEquals(1, expiryDate.get(ChronoField.DAY_OF_MONTH));
365 Assertions.assertEquals(23, expiryDate.get(ChronoField.HOUR_OF_DAY));
366 Assertions.assertEquals(59, expiryDate.get(ChronoField.MINUTE_OF_HOUR));
367 Assertions.assertEquals(59, expiryDate.get(ChronoField.SECOND_OF_MINUTE));
368 Assertions.assertEquals(0, expiryDate.get(ChronoField.MILLI_OF_SECOND));
369 }
370
371 @Test
372 void testParseExpiryYearTwoDigit1() throws Exception {
373 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
374 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
375 h.parse(cookie, "23:59:59; 1-Apr-70");
376
377 final LocalDateTime expiryDate = DateUtils.toUTC(cookie.getExpiryInstant());
378 Assertions.assertNotNull(expiryDate);
379
380 Assertions.assertEquals(1970, expiryDate.get(ChronoField.YEAR));
381 }
382
383 @Test
384 void testParseExpiryYearTwoDigit2() throws Exception {
385 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
386 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
387 h.parse(cookie, "23:59:59; 1-Apr-99");
388
389 final LocalDateTime expiryDate = DateUtils.toUTC(cookie.getExpiryInstant());
390 Assertions.assertNotNull(expiryDate);
391
392 Assertions.assertEquals(1999, expiryDate.get(ChronoField.YEAR));
393 }
394
395 @Test
396 void testParseExpiryYearTwoDigit3() throws Exception {
397 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
398 final CookieAttributeHandler h = LaxExpiresHandler.INSTANCE;
399 h.parse(cookie, "23:59:59; 1-Apr-00");
400
401 final LocalDateTime expiryDate = DateUtils.toUTC(cookie.getExpiryInstant());
402 Assertions.assertNotNull(expiryDate);
403
404 Assertions.assertEquals(2000, expiryDate.get(ChronoField.YEAR));
405 }
406
407 }