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.util.Calendar;
31 import java.util.Date;
32
33 import org.apache.hc.client5.http.cookie.CookieAttributeHandler;
34 import org.apache.hc.client5.http.cookie.MalformedCookieException;
35 import org.junit.Assert;
36 import org.junit.Test;
37
38 public class TestLaxCookieAttribHandlers {
39
40 @Test
41 public void testParseMaxAge() throws Exception {
42 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
43 final CookieAttributeHandler h = new LaxMaxAgeHandler();
44 h.parse(cookie, "2000");
45 Assert.assertNotNull(cookie.getExpiryDate());
46 }
47
48 @Test
49 public void testParseMaxNegative() throws Exception {
50 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
51 final CookieAttributeHandler h = new LaxMaxAgeHandler();
52 h.parse(cookie, "-2000");
53 Assert.assertNotNull(cookie.getExpiryDate());
54 }
55
56 @Test
57 public void testParseMaxZero() throws Exception {
58 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
59 final CookieAttributeHandler h = new LaxMaxAgeHandler();
60 h.parse(cookie, "0000");
61 Assert.assertNotNull(cookie.getExpiryDate());
62 }
63
64 @Test
65 public void testBasicMaxAgeParseEmpty() throws Exception {
66 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
67 final CookieAttributeHandler h = new LaxMaxAgeHandler();
68 h.parse(cookie, " ");
69 Assert.assertNull(cookie.getExpiryDate());
70 }
71
72 @Test
73 public void testBasicMaxAgeParseInvalid() throws Exception {
74 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
75 final CookieAttributeHandler h = new LaxMaxAgeHandler();
76 h.parse(cookie, "garbage");
77 Assert.assertNull(cookie.getExpiryDate());
78 }
79
80 @Test
81 public void testBasicMaxAgeInvalidInput() throws Exception {
82 final CookieAttributeHandler h = new LaxMaxAgeHandler();
83 try {
84 h.parse(null, "stuff");
85 Assert.fail("NullPointerException must have been thrown");
86 } catch (final NullPointerException ex) {
87
88 }
89 }
90
91 @Test(expected = MalformedCookieException.class)
92 public void testExpiryGarbage() throws Exception {
93 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
94 final CookieAttributeHandler h = new LaxExpiresHandler();
95 h.parse(cookie, ";;blah,blah;yada ");
96 }
97
98 @Test
99 public void testParseExpiry() throws Exception {
100 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
101 final CookieAttributeHandler h = new LaxExpiresHandler();
102 h.parse(cookie, "1:0:12 8-jan-2012");
103
104 final Date expiryDate = cookie.getExpiryDate();
105 Assert.assertNotNull(expiryDate);
106 final Calendar c = Calendar.getInstance();
107 c.setTimeZone(LaxExpiresHandler.UTC);
108 c.setTime(expiryDate);
109 Assert.assertEquals(2012, c.get(Calendar.YEAR));
110 Assert.assertEquals(Calendar.JANUARY, c.get(Calendar.MONTH));
111 Assert.assertEquals(8, c.get(Calendar.DAY_OF_MONTH));
112 Assert.assertEquals(1, c.get(Calendar.HOUR_OF_DAY));
113 Assert.assertEquals(0, c.get(Calendar.MINUTE));
114 Assert.assertEquals(12, c.get(Calendar.SECOND));
115 Assert.assertEquals(0, c.get(Calendar.MILLISECOND));
116 }
117
118 @Test
119 public void testParseExpiryInvalidTime0() throws Exception {
120 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
121 final CookieAttributeHandler h = new LaxExpiresHandler();
122 h.parse(cookie, null);
123 Assert.assertNull(cookie.getExpiryDate());
124 }
125
126 @Test(expected = MalformedCookieException.class)
127 public void testParseExpiryInvalidTime1() throws Exception {
128 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
129 final CookieAttributeHandler h = new LaxExpiresHandler();
130 h.parse(cookie, "1:0:122 8 dec 1980");
131 }
132
133 @Test(expected = MalformedCookieException.class)
134 public void testParseExpiryInvalidTime2() throws Exception {
135 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
136 final CookieAttributeHandler h = new LaxExpiresHandler();
137 h.parse(cookie, "24:00:00 8 dec 1980");
138 }
139
140 @Test(expected = MalformedCookieException.class)
141 public void testParseExpiryInvalidTime3() throws Exception {
142 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
143 final CookieAttributeHandler h = new LaxExpiresHandler();
144 h.parse(cookie, "23:60:00 8 dec 1980");
145 }
146
147 @Test(expected = MalformedCookieException.class)
148 public void testParseExpiryInvalidTime4() throws Exception {
149 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
150 final CookieAttributeHandler h = new LaxExpiresHandler();
151 h.parse(cookie, "23:00:60 8 dec 1980");
152 }
153
154 @Test
155 public void testParseExpiryFunnyTime() throws Exception {
156 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
157 final CookieAttributeHandler h = new LaxExpiresHandler();
158 h.parse(cookie, "1:59:00blah; 8-feb-2000");
159
160 final Date expiryDate = cookie.getExpiryDate();
161 Assert.assertNotNull(expiryDate);
162 final Calendar c = Calendar.getInstance();
163 c.setTimeZone(LaxExpiresHandler.UTC);
164 c.setTime(expiryDate);
165 Assert.assertEquals(2000, c.get(Calendar.YEAR));
166 Assert.assertEquals(Calendar.FEBRUARY, c.get(Calendar.MONTH));
167 Assert.assertEquals(8, c.get(Calendar.DAY_OF_MONTH));
168 Assert.assertEquals(1, c.get(Calendar.HOUR_OF_DAY));
169 Assert.assertEquals(59, c.get(Calendar.MINUTE));
170 Assert.assertEquals(0, c.get(Calendar.SECOND));
171 Assert.assertEquals(0, c.get(Calendar.MILLISECOND));
172 }
173
174 @Test(expected = MalformedCookieException.class)
175 public void testParseExpiryInvalidDayOfMonth1() throws Exception {
176 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
177 final CookieAttributeHandler h = new LaxExpiresHandler();
178 h.parse(cookie, "12:00:00 888 mar 1880");
179 }
180
181 @Test(expected = MalformedCookieException.class)
182 public void testParseExpiryInvalidDayOfMonth2() throws Exception {
183 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
184 final CookieAttributeHandler h = new LaxExpiresHandler();
185 h.parse(cookie, "12:00:00 0 mar 1880");
186 }
187
188 @Test(expected = MalformedCookieException.class)
189 public void testParseExpiryInvalidDayOfMonth3() throws Exception {
190 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
191 final CookieAttributeHandler h = new LaxExpiresHandler();
192 h.parse(cookie, "12:00:00 32 mar 1880");
193 }
194
195 @Test
196 public void testParseExpiryFunnyDayOfMonth() throws Exception {
197 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
198 final CookieAttributeHandler h = new LaxExpiresHandler();
199 h.parse(cookie, "12:00:00 8blah;mar;1880");
200
201 final Date expiryDate = cookie.getExpiryDate();
202 Assert.assertNotNull(expiryDate);
203 final Calendar c = Calendar.getInstance();
204 c.setTimeZone(LaxExpiresHandler.UTC);
205 c.setTime(expiryDate);
206 Assert.assertEquals(1880, c.get(Calendar.YEAR));
207 Assert.assertEquals(Calendar.MARCH, c.get(Calendar.MONTH));
208 Assert.assertEquals(8, c.get(Calendar.DAY_OF_MONTH));
209 Assert.assertEquals(12, c.get(Calendar.HOUR_OF_DAY));
210 Assert.assertEquals(0, c.get(Calendar.MINUTE));
211 Assert.assertEquals(0, c.get(Calendar.SECOND));
212 Assert.assertEquals(0, c.get(Calendar.MILLISECOND));
213 }
214
215 @Test(expected = MalformedCookieException.class)
216 public void testParseExpiryInvalidMonth() throws Exception {
217 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
218 final CookieAttributeHandler h = new LaxExpiresHandler();
219 h.parse(cookie, "1:00:00 8 dek 80");
220 }
221
222 @Test
223 public void testParseExpiryFunnyMonth() throws Exception {
224 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
225 final CookieAttributeHandler h = new LaxExpiresHandler();
226 h.parse(cookie, "23:59:59; 1-ApriLLLLL-2008");
227
228 final Date expiryDate = cookie.getExpiryDate();
229 Assert.assertNotNull(expiryDate);
230 final Calendar c = Calendar.getInstance();
231 c.setTimeZone(LaxExpiresHandler.UTC);
232 c.setTime(expiryDate);
233 Assert.assertEquals(2008, c.get(Calendar.YEAR));
234 Assert.assertEquals(Calendar.APRIL, c.get(Calendar.MONTH));
235 Assert.assertEquals(1, c.get(Calendar.DAY_OF_MONTH));
236 Assert.assertEquals(23, c.get(Calendar.HOUR_OF_DAY));
237 Assert.assertEquals(59, c.get(Calendar.MINUTE));
238 Assert.assertEquals(59, c.get(Calendar.SECOND));
239 Assert.assertEquals(0, c.get(Calendar.MILLISECOND));
240 }
241
242 @Test(expected = MalformedCookieException.class)
243 public void testParseExpiryInvalidYearTooShort() throws Exception {
244 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
245 final CookieAttributeHandler h = new LaxExpiresHandler();
246 h.parse(cookie, "1:00:00 8 dec 8");
247 }
248
249 @Test(expected = MalformedCookieException.class)
250 public void testParseExpiryInvalidYearTooLong() throws Exception {
251 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
252 final CookieAttributeHandler h = new LaxExpiresHandler();
253 h.parse(cookie, "1:00:00 8 dec 88888");
254 }
255
256 @Test(expected = MalformedCookieException.class)
257 public void testParseExpiryInvalidYearTooLongAgo() throws Exception {
258 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
259 final CookieAttributeHandler h = new LaxExpiresHandler();
260 h.parse(cookie, "1:00:00 8 dec 1600");
261 }
262
263 @Test
264 public void testParseExpiryFunnyYear() throws Exception {
265 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
266 final CookieAttributeHandler h = new LaxExpiresHandler();
267 h.parse(cookie, "23:59:59; 1-Apr-2008blah");
268
269 final Date expiryDate = cookie.getExpiryDate();
270 Assert.assertNotNull(expiryDate);
271 final Calendar c = Calendar.getInstance();
272 c.setTimeZone(LaxExpiresHandler.UTC);
273 c.setTime(expiryDate);
274 Assert.assertEquals(2008, c.get(Calendar.YEAR));
275 Assert.assertEquals(Calendar.APRIL, c.get(Calendar.MONTH));
276 Assert.assertEquals(1, c.get(Calendar.DAY_OF_MONTH));
277 Assert.assertEquals(23, c.get(Calendar.HOUR_OF_DAY));
278 Assert.assertEquals(59, c.get(Calendar.MINUTE));
279 Assert.assertEquals(59, c.get(Calendar.SECOND));
280 Assert.assertEquals(0, c.get(Calendar.MILLISECOND));
281 }
282
283 @Test
284 public void testParseExpiryYearTwoDigit1() throws Exception {
285 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
286 final CookieAttributeHandler h = new LaxExpiresHandler();
287 h.parse(cookie, "23:59:59; 1-Apr-70");
288
289 final Date expiryDate = cookie.getExpiryDate();
290 Assert.assertNotNull(expiryDate);
291 final Calendar c = Calendar.getInstance();
292 c.setTimeZone(LaxExpiresHandler.UTC);
293 c.setTime(expiryDate);
294 Assert.assertEquals(1970, c.get(Calendar.YEAR));
295 }
296
297 @Test
298 public void testParseExpiryYearTwoDigit2() throws Exception {
299 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
300 final CookieAttributeHandler h = new LaxExpiresHandler();
301 h.parse(cookie, "23:59:59; 1-Apr-99");
302
303 final Date expiryDate = cookie.getExpiryDate();
304 Assert.assertNotNull(expiryDate);
305 final Calendar c = Calendar.getInstance();
306 c.setTimeZone(LaxExpiresHandler.UTC);
307 c.setTime(expiryDate);
308 Assert.assertEquals(1999, c.get(Calendar.YEAR));
309 }
310
311 @Test
312 public void testParseExpiryYearTwoDigit3() throws Exception {
313 final BasicClientCookie cookie = new BasicClientCookie("name", "value");
314 final CookieAttributeHandler h = new LaxExpiresHandler();
315 h.parse(cookie, "23:59:59; 1-Apr-00");
316
317 final Date expiryDate = cookie.getExpiryDate();
318 Assert.assertNotNull(expiryDate);
319 final Calendar c = Calendar.getInstance();
320 c.setTimeZone(LaxExpiresHandler.UTC);
321 c.setTime(expiryDate);
322 Assert.assertEquals(2000, c.get(Calendar.YEAR));
323 }
324
325 }