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.http.impl.cookie;
29  
30  import java.util.ArrayList;
31  import java.util.Date;
32  import java.util.List;
33  
34  import org.apache.http.Header;
35  import org.apache.http.client.utils.DateUtils;
36  import org.apache.http.cookie.ClientCookie;
37  import org.apache.http.cookie.Cookie;
38  import org.apache.http.cookie.CookieOrigin;
39  import org.apache.http.cookie.CookieSpec;
40  import org.apache.http.cookie.MalformedCookieException;
41  import org.apache.http.message.BasicHeader;
42  import org.junit.Assert;
43  import org.junit.Test;
44  
45  /**
46   * Test cases for BrowserCompatSpec
47   *
48   */
49  public class TestBrowserCompatSpec {
50  
51      @SuppressWarnings("unused")
52      @Test
53      public void testConstructor() throws Exception {
54          new BrowserCompatSpec();
55          new BrowserCompatSpec(null);
56          new BrowserCompatSpec(new String[] { DateUtils.PATTERN_RFC1036 });
57      }
58  
59      /**
60       * Tests whether domain attribute check is case-insensitive.
61       */
62      @Test
63      public void testDomainCaseInsensitivity() throws Exception {
64          final Header header = new BasicHeader("Set-Cookie",
65              "name=value; path=/; domain=.whatever.com");
66  
67          final CookieSpec cookiespec = new BrowserCompatSpec();
68          final CookieOrigin origin = new CookieOrigin("www.WhatEver.com", 80, "/", false);
69          final List<Cookie> cookies = cookiespec.parse(header, origin);
70          for (int i = 0; i < cookies.size(); i++) {
71              cookiespec.validate(cookies.get(i), origin);
72          }
73          Assert.assertNotNull(cookies);
74          Assert.assertEquals(1, cookies.size());
75          Assert.assertEquals(".whatever.com", cookies.get(0).getDomain());
76      }
77  
78      /**
79       * Test basic parse (with various spacings
80       */
81      @Test
82      public void testParse1() throws Exception {
83          final String headerValue = "custno = 12345; comment=test; version=1," +
84              " name=John; version=1; max-age=600; secure; domain=.apache.org";
85  
86          final Header header = new BasicHeader("set-cookie", headerValue);
87  
88          final CookieSpec cookiespec = new BrowserCompatSpec();
89          final CookieOrigin origin = new CookieOrigin("www.apache.org", 80, "/", false);
90          final List<Cookie> cookies = cookiespec.parse(header, origin);
91          for (int i = 0; i < cookies.size(); i++) {
92              cookiespec.validate(cookies.get(i), origin);
93          }
94          Assert.assertEquals(2, cookies.size());
95  
96          Assert.assertEquals("custno", cookies.get(0).getName());
97          Assert.assertEquals("12345", cookies.get(0).getValue());
98          Assert.assertEquals("test", cookies.get(0).getComment());
99          Assert.assertEquals(1, cookies.get(0).getVersion());
100         Assert.assertEquals("www.apache.org", cookies.get(0).getDomain());
101         Assert.assertEquals("/", cookies.get(0).getPath());
102         Assert.assertFalse(cookies.get(0).isSecure());
103 
104         Assert.assertEquals("name", cookies.get(1).getName());
105         Assert.assertEquals("John", cookies.get(1).getValue());
106         Assert.assertEquals(null, cookies.get(1).getComment());
107         Assert.assertEquals(1, cookies.get(1).getVersion());
108         Assert.assertEquals(".apache.org", cookies.get(1).getDomain());
109         Assert.assertEquals("/", cookies.get(1).getPath());
110         Assert.assertTrue(cookies.get(1).isSecure());
111     }
112 
113     /**
114      * Test no spaces
115      */
116     @Test
117     public void testParse2() throws Exception {
118         final String headerValue = "custno=12345;comment=test; version=1," +
119             "name=John;version=1;max-age=600;secure;domain=.apache.org";
120 
121         final Header header = new BasicHeader("set-cookie", headerValue);
122 
123         final CookieSpec cookiespec = new BrowserCompatSpec();
124         final CookieOrigin origin = new CookieOrigin("www.apache.org", 80, "/", false);
125         final List<Cookie> cookies = cookiespec.parse(header, origin);
126         for (int i = 0; i < cookies.size(); i++) {
127             cookiespec.validate(cookies.get(i), origin);
128         }
129 
130         Assert.assertEquals(2, cookies.size());
131 
132         Assert.assertEquals("custno", cookies.get(0).getName());
133         Assert.assertEquals("12345", cookies.get(0).getValue());
134         Assert.assertEquals("test", cookies.get(0).getComment());
135         Assert.assertEquals(1, cookies.get(0).getVersion());
136         Assert.assertEquals("www.apache.org", cookies.get(0).getDomain());
137         Assert.assertEquals("/", cookies.get(0).getPath());
138         Assert.assertFalse(cookies.get(0).isSecure());
139 
140         Assert.assertEquals("name", cookies.get(1).getName());
141         Assert.assertEquals("John", cookies.get(1).getValue());
142         Assert.assertEquals(null, cookies.get(1).getComment());
143         Assert.assertEquals(1, cookies.get(1).getVersion());
144         Assert.assertEquals(".apache.org", cookies.get(1).getDomain());
145         Assert.assertEquals("/", cookies.get(1).getPath());
146         Assert.assertTrue(cookies.get(1).isSecure());
147     }
148 
149 
150     /**
151      * Test parse with quoted text
152      */
153     @Test
154     public void testParse3() throws Exception {
155         final String headerValue =
156             "name=\"Doe, John\";version=1;max-age=600;secure;domain=.apache.org";
157         final Header header = new BasicHeader("set-cookie", headerValue);
158 
159         final CookieSpec cookiespec = new BrowserCompatSpec();
160         final CookieOrigin origin = new CookieOrigin("www.apache.org", 80, "/", false);
161         final List<Cookie> cookies = cookiespec.parse(header, origin);
162         for (int i = 0; i < cookies.size(); i++) {
163             cookiespec.validate(cookies.get(i), origin);
164         }
165 
166         Assert.assertEquals(1, cookies.size());
167 
168         Assert.assertEquals("name", cookies.get(0).getName());
169         Assert.assertEquals("Doe, John", cookies.get(0).getValue());
170         Assert.assertEquals(null, cookies.get(0).getComment());
171         Assert.assertEquals(1, cookies.get(0).getVersion());
172         Assert.assertEquals(".apache.org", cookies.get(0).getDomain());
173         Assert.assertEquals("/", cookies.get(0).getPath());
174         Assert.assertTrue(cookies.get(0).isSecure());
175     }
176 
177 
178     // see issue #5279
179     @Test
180     public void testQuotedExpiresAttribute() throws Exception {
181         final String headerValue = "custno=12345;Expires='Thu, 01-Jan-2070 00:00:10 GMT'";
182 
183         final Header header = new BasicHeader("set-cookie", headerValue);
184 
185         final CookieSpec cookiespec = new BrowserCompatSpec();
186         final CookieOrigin origin = new CookieOrigin("www.apache.org", 80, "/", true);
187         final List<Cookie> cookies = cookiespec.parse(header, origin);
188         for (int i = 0; i < cookies.size(); i++) {
189             cookiespec.validate(cookies.get(i), origin);
190         }
191         Assert.assertNotNull("Expected some cookies",cookies);
192         Assert.assertEquals("Expected 1 cookie",1,cookies.size());
193         Assert.assertNotNull("Expected cookie to have getExpiryDate",cookies.get(0).getExpiryDate());
194     }
195 
196     @Test
197     public void testSecurityError() throws Exception {
198         final String headerValue = "custno=12345;comment=test; version=1," +
199             "name=John;version=1;max-age=600;secure;domain=jakarta.apache.org";
200         final Header header = new BasicHeader("set-cookie", headerValue);
201 
202         final CookieSpec cookiespec = new BrowserCompatSpec();
203         final CookieOrigin origin = new CookieOrigin("www.apache.org", 80, "/", true);
204         try {
205             final List<Cookie> cookies = cookiespec.parse(header, origin);
206             for (int i = 0; i < cookies.size(); i++) {
207                 cookiespec.validate(cookies.get(i), origin);
208             }
209             Assert.fail("MalformedCookieException exception should have been thrown");
210         } catch (final MalformedCookieException ex) {
211             // expected
212         }
213     }
214 
215     @Test
216     public void testParseSimple() throws Exception {
217         final Header header = new BasicHeader("Set-Cookie","cookie-name=cookie-value");
218 
219         final CookieSpec cookiespec = new BrowserCompatSpec();
220         final CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/path/path", false);
221         final List<Cookie> cookies = cookiespec.parse(header, origin);
222         for (int i = 0; i < cookies.size(); i++) {
223             cookiespec.validate(cookies.get(i), origin);
224         }
225         Assert.assertEquals("Found 1 cookie.",1,cookies.size());
226         Assert.assertEquals("Name","cookie-name",cookies.get(0).getName());
227         Assert.assertEquals("Value","cookie-value",cookies.get(0).getValue());
228         Assert.assertTrue("Comment",null == cookies.get(0).getComment());
229         Assert.assertTrue("ExpiryDate",null == cookies.get(0).getExpiryDate());
230         //Assert.assertTrue("isToBeDiscarded",cookies.get(0).isToBeDiscarded());
231         Assert.assertTrue("isPersistent",!cookies.get(0).isPersistent());
232         Assert.assertEquals("Domain","127.0.0.1",cookies.get(0).getDomain());
233         Assert.assertEquals("Path","/path",cookies.get(0).getPath());
234         Assert.assertTrue("Secure",!cookies.get(0).isSecure());
235         Assert.assertEquals("Version",0,cookies.get(0).getVersion());
236     }
237 
238     @Test
239     public void testParseSimple2() throws Exception {
240         final Header header = new BasicHeader("Set-Cookie", "cookie-name=cookie-value");
241 
242         final CookieSpec cookiespec = new BrowserCompatSpec();
243         final CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/path", false);
244         final List<Cookie> cookies = cookiespec.parse(header, origin);
245         for (int i = 0; i < cookies.size(); i++) {
246             cookiespec.validate(cookies.get(i), origin);
247         }
248         Assert.assertEquals("Found 1 cookie.", 1, cookies.size());
249         Assert.assertEquals("Name", "cookie-name", cookies.get(0).getName());
250         Assert.assertEquals("Value", "cookie-value", cookies.get(0).getValue());
251         Assert.assertTrue("Comment", null == cookies.get(0).getComment());
252         Assert.assertTrue("ExpiryDate", null == cookies.get(0).getExpiryDate());
253         //Assert.assertTrue("isToBeDiscarded",cookies.get(0).isToBeDiscarded());
254         Assert.assertTrue("isPersistent", !cookies.get(0).isPersistent());
255         Assert.assertEquals("Domain", "127.0.0.1", cookies.get(0).getDomain());
256         Assert.assertEquals("Path", "/", cookies.get(0).getPath());
257         Assert.assertTrue("Secure", !cookies.get(0).isSecure());
258         Assert.assertEquals("Version", 0, cookies.get(0).getVersion());
259     }
260 
261     @Test
262     public void testParseNoName() throws Exception {
263         final Header header = new BasicHeader("Set-Cookie","=stuff; path=/");
264 
265         final CookieSpec cookiespec = new BrowserCompatSpec();
266         final CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/", false);
267         try {
268             final List<Cookie> cookies = cookiespec.parse(header, origin);
269             for (int i = 0; i < cookies.size(); i++) {
270                 cookiespec.validate(cookies.get(i), origin);
271             }
272             Assert.fail("MalformedCookieException should have been thrown");
273         } catch (final MalformedCookieException ex) {
274             // expected
275         }
276     }
277 
278     @Test
279     public void testParseNoValue() throws Exception {
280         final Header header = new BasicHeader("Set-Cookie","cookie-name=");
281 
282         final CookieSpec cookiespec = new BrowserCompatSpec();
283         final CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/", false);
284         final List<Cookie> cookies = cookiespec.parse(header, origin);
285         for (int i = 0; i < cookies.size(); i++) {
286             cookiespec.validate(cookies.get(i), origin);
287         }
288         Assert.assertEquals("Found 1 cookie.",1,cookies.size());
289         Assert.assertEquals("Name","cookie-name",cookies.get(0).getName());
290         Assert.assertEquals("Value", "", cookies.get(0).getValue());
291         Assert.assertTrue("Comment",null == cookies.get(0).getComment());
292         Assert.assertTrue("ExpiryDate",null == cookies.get(0).getExpiryDate());
293         //Assert.assertTrue("isToBeDiscarded",cookies.get(0).isToBeDiscarded());
294         Assert.assertTrue("isPersistent",!cookies.get(0).isPersistent());
295         Assert.assertEquals("Domain","127.0.0.1",cookies.get(0).getDomain());
296         Assert.assertEquals("Path","/",cookies.get(0).getPath());
297         Assert.assertTrue("Secure",!cookies.get(0).isSecure());
298         Assert.assertEquals("Version",0,cookies.get(0).getVersion());
299     }
300 
301     @Test
302     public void testParseWithWhiteSpace() throws Exception {
303         final Header header = new BasicHeader("Set-Cookie"," cookie-name  =    cookie-value  ");
304 
305         final CookieSpec cookiespec = new BrowserCompatSpec();
306         final CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/", false);
307         final List<Cookie> cookies = cookiespec.parse(header, origin);
308         for (int i = 0; i < cookies.size(); i++) {
309             cookiespec.validate(cookies.get(i), origin);
310         }
311         Assert.assertEquals("Found 1 cookie.",1,cookies.size());
312         Assert.assertEquals("Name","cookie-name",cookies.get(0).getName());
313         Assert.assertEquals("Value","cookie-value",cookies.get(0).getValue());
314         Assert.assertEquals("Domain","127.0.0.1",cookies.get(0).getDomain());
315         Assert.assertEquals("Path","/",cookies.get(0).getPath());
316         Assert.assertTrue("Secure",!cookies.get(0).isSecure());
317         Assert.assertTrue("ExpiryDate",null == cookies.get(0).getExpiryDate());
318         Assert.assertTrue("Comment",null == cookies.get(0).getComment());
319     }
320 
321     @Test
322     public void testParseWithQuotes() throws Exception {
323         final Header header = new BasicHeader("Set-Cookie"," cookie-name  =  \" cookie-value \" ;path=/");
324 
325         final CookieSpec cookiespec = new BrowserCompatSpec();
326         final CookieOrigin origin = new CookieOrigin("127.0.0.1",80, "/", false);
327         final List<Cookie> cookies = cookiespec.parse(header, origin);
328         for (int i = 0; i < cookies.size(); i++) {
329             cookiespec.validate(cookies.get(i), origin);
330         }
331         Assert.assertEquals("Found 1 cookie.",1,cookies.size());
332         Assert.assertEquals("Name","cookie-name",cookies.get(0).getName());
333         Assert.assertEquals("Value","\" cookie-value \"",cookies.get(0).getValue());
334         Assert.assertEquals("Domain","127.0.0.1",cookies.get(0).getDomain());
335         Assert.assertEquals("Path","/",cookies.get(0).getPath());
336         Assert.assertTrue("Secure",!cookies.get(0).isSecure());
337         Assert.assertTrue("ExpiryDate",null == cookies.get(0).getExpiryDate());
338         Assert.assertTrue("Comment",null == cookies.get(0).getComment());
339     }
340 
341     @Test
342     public void testParseWithPath() throws Exception {
343         final Header header = new BasicHeader("Set-Cookie","cookie-name=cookie-value; Path=/path/");
344 
345         final CookieSpec cookiespec = new BrowserCompatSpec();
346         final CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/path/path", false);
347         final List<Cookie> cookies = cookiespec.parse(header, origin);
348         for (int i = 0; i < cookies.size(); i++) {
349             cookiespec.validate(cookies.get(i), origin);
350         }
351         Assert.assertEquals("Found 1 cookie.",1,cookies.size());
352         Assert.assertEquals("Name","cookie-name",cookies.get(0).getName());
353         Assert.assertEquals("Value","cookie-value",cookies.get(0).getValue());
354         Assert.assertEquals("Domain","127.0.0.1",cookies.get(0).getDomain());
355         Assert.assertEquals("Path","/path/",cookies.get(0).getPath());
356         Assert.assertTrue("Secure",!cookies.get(0).isSecure());
357         Assert.assertTrue("ExpiryDate",null == cookies.get(0).getExpiryDate());
358         Assert.assertTrue("Comment",null == cookies.get(0).getComment());
359     }
360 
361     @Test
362     public void testParseWithDomain() throws Exception {
363         final Header header = new BasicHeader("Set-Cookie","cookie-name=cookie-value; Domain=127.0.0.1");
364 
365         final CookieSpec cookiespec = new BrowserCompatSpec();
366         final CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/", false);
367         final List<Cookie> cookies = cookiespec.parse(header, origin);
368         for (int i = 0; i < cookies.size(); i++) {
369             cookiespec.validate(cookies.get(i), origin);
370         }
371         Assert.assertEquals("Found 1 cookie.",1,cookies.size());
372         Assert.assertEquals("Name","cookie-name",cookies.get(0).getName());
373         Assert.assertEquals("Value","cookie-value",cookies.get(0).getValue());
374         Assert.assertEquals("Domain","127.0.0.1",cookies.get(0).getDomain());
375         Assert.assertEquals("Path","/",cookies.get(0).getPath());
376         Assert.assertTrue("Secure",!cookies.get(0).isSecure());
377         Assert.assertTrue("ExpiryDate",null == cookies.get(0).getExpiryDate());
378         Assert.assertTrue("Comment",null == cookies.get(0).getComment());
379     }
380 
381     @Test
382     public void testParseWithSecure() throws Exception {
383         final Header header = new BasicHeader("Set-Cookie","cookie-name=cookie-value; secure");
384 
385         final CookieSpec cookiespec = new BrowserCompatSpec();
386         final CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/", true);
387         final List<Cookie> cookies = cookiespec.parse(header, origin);
388         for (int i = 0; i < cookies.size(); i++) {
389             cookiespec.validate(cookies.get(i), origin);
390         }
391         Assert.assertEquals("Found 1 cookie.",1,cookies.size());
392         Assert.assertEquals("Name","cookie-name",cookies.get(0).getName());
393         Assert.assertEquals("Value","cookie-value",cookies.get(0).getValue());
394         Assert.assertEquals("Domain","127.0.0.1",cookies.get(0).getDomain());
395         Assert.assertEquals("Path","/",cookies.get(0).getPath());
396         Assert.assertTrue("Secure",cookies.get(0).isSecure());
397         Assert.assertTrue("ExpiryDate",null == cookies.get(0).getExpiryDate());
398         Assert.assertTrue("Comment",null == cookies.get(0).getComment());
399     }
400 
401     @Test
402     public void testParseWithComment() throws Exception {
403         final Header header = new BasicHeader("Set-Cookie",
404             "cookie-name=cookie-value; comment=\"This is a comment.\"");
405 
406         final CookieSpec cookiespec = new BrowserCompatSpec();
407         final CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/", true);
408         final List<Cookie> cookies = cookiespec.parse(header, origin);
409         for (int i = 0; i < cookies.size(); i++) {
410             cookiespec.validate(cookies.get(i), origin);
411         }
412         Assert.assertEquals("Found 1 cookie.",1,cookies.size());
413         Assert.assertEquals("Name","cookie-name",cookies.get(0).getName());
414         Assert.assertEquals("Value","cookie-value",cookies.get(0).getValue());
415         Assert.assertEquals("Domain","127.0.0.1",cookies.get(0).getDomain());
416         Assert.assertEquals("Path","/",cookies.get(0).getPath());
417         Assert.assertTrue("Secure",!cookies.get(0).isSecure());
418         Assert.assertTrue("ExpiryDate",null == cookies.get(0).getExpiryDate());
419         Assert.assertEquals("Comment","\"This is a comment.\"",cookies.get(0).getComment());
420     }
421 
422     @Test
423     public void testParseWithExpires() throws Exception {
424         final Header header = new BasicHeader("Set-Cookie",
425             "cookie-name=cookie-value;Expires=Thu, 01-Jan-1970 00:00:10 GMT");
426 
427         final CookieSpec cookiespec = new BrowserCompatSpec();
428         final CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/", true);
429         final List<Cookie> cookies = cookiespec.parse(header, origin);
430         for (int i = 0; i < cookies.size(); i++) {
431             cookiespec.validate(cookies.get(i), origin);
432         }
433         Assert.assertEquals("Found 1 cookie.",1,cookies.size());
434         Assert.assertEquals("Name","cookie-name",cookies.get(0).getName());
435         Assert.assertEquals("Value","cookie-value",cookies.get(0).getValue());
436         Assert.assertEquals("Domain","127.0.0.1",cookies.get(0).getDomain());
437         Assert.assertEquals("Path","/",cookies.get(0).getPath());
438         Assert.assertTrue("Secure",!cookies.get(0).isSecure());
439         Assert.assertEquals(new Date(10000L),cookies.get(0).getExpiryDate());
440         Assert.assertTrue("Comment",null == cookies.get(0).getComment());
441     }
442 
443     @Test
444     public void testParseWithAll() throws Exception {
445         final Header header = new BasicHeader("Set-Cookie",
446             "cookie-name=cookie-value;Version=1;Path=/commons;Domain=.apache.org;" +
447             "Comment=This is a comment.;secure;Expires=Thu, 01-Jan-1970 00:00:10 GMT");
448 
449         final CookieSpec cookiespec = new BrowserCompatSpec();
450         final CookieOrigin origin = new CookieOrigin("www.apache.org", 80, "/commons/httpclient", true);
451         final List<Cookie> cookies = cookiespec.parse(header, origin);
452         for (int i = 0; i < cookies.size(); i++) {
453             cookiespec.validate(cookies.get(i), origin);
454         }
455         Assert.assertEquals("Found 1 cookie.",1,cookies.size());
456         Assert.assertEquals("Name","cookie-name",cookies.get(0).getName());
457         Assert.assertEquals("Value","cookie-value",cookies.get(0).getValue());
458         Assert.assertEquals("Domain",".apache.org",cookies.get(0).getDomain());
459         Assert.assertEquals("Path","/commons",cookies.get(0).getPath());
460         Assert.assertTrue("Secure",cookies.get(0).isSecure());
461         Assert.assertEquals(new Date(10000L),cookies.get(0).getExpiryDate());
462         Assert.assertEquals("Comment","This is a comment.",cookies.get(0).getComment());
463         Assert.assertEquals("Version",1,cookies.get(0).getVersion());
464     }
465 
466     @Test
467     public void testParseMultipleDifferentPaths() throws Exception {
468         final Header header = new BasicHeader("Set-Cookie",
469             "name1=value1;Version=1;Path=/commons,name1=value2;Version=1;" +
470             "Path=/commons/httpclient;Version=1");
471 
472         final CookieSpec cookiespec = new BrowserCompatSpec();
473         final CookieOrigin origin = new CookieOrigin("www.apache.org", 80, "/commons/httpclient", true);
474         final List<Cookie> cookies = cookiespec.parse(header, origin);
475         for (int i = 0; i < cookies.size(); i++) {
476             cookiespec.validate(cookies.get(i), origin);
477         }
478         Assert.assertEquals("Wrong number of cookies.",2,cookies.size());
479         Assert.assertEquals("Name","name1",cookies.get(0).getName());
480         Assert.assertEquals("Value","value1",cookies.get(0).getValue());
481         Assert.assertEquals("Name","name1",cookies.get(1).getName());
482         Assert.assertEquals("Value","value2",cookies.get(1).getValue());
483     }
484 
485     @Test
486     public void testParseRelativePath() throws Exception {
487         final Header header = new BasicHeader("Set-Cookie", "name1=value1;Path=whatever");
488 
489         final CookieSpec cookiespec = new BrowserCompatSpec();
490         final CookieOrigin origin = new CookieOrigin("www.apache.org", 80, "whatever", true);
491         final List<Cookie> cookies = cookiespec.parse(header, origin);
492         for (int i = 0; i < cookies.size(); i++) {
493             cookiespec.validate(cookies.get(i), origin);
494         }
495         Assert.assertEquals("Found 1 cookies.",1,cookies.size());
496         Assert.assertEquals("Name","name1",cookies.get(0).getName());
497         Assert.assertEquals("Value","value1",cookies.get(0).getValue());
498         Assert.assertEquals("Path","whatever",cookies.get(0).getPath());
499     }
500 
501     @Test
502     public void testParseWithWrongDomain() throws Exception {
503         final Header header = new BasicHeader("Set-Cookie",
504             "cookie-name=cookie-value; domain=127.0.0.1; version=1");
505 
506         final CookieSpec cookiespec = new BrowserCompatSpec();
507         final CookieOrigin origin = new CookieOrigin("127.0.0.2", 80, "/", false);
508         try {
509             final List<Cookie> cookies = cookiespec.parse(header, origin);
510             for (int i = 0; i < cookies.size(); i++) {
511                 cookiespec.validate(cookies.get(i), origin);
512             }
513             Assert.fail("MalformedCookieException exception should have been thrown");
514         } catch (final MalformedCookieException ex) {
515             // expected
516         }
517     }
518 
519     @Test
520     public void testParseWithPathMismatch() throws Exception {
521         final Header header = new BasicHeader("Set-Cookie",
522             "cookie-name=cookie-value; path=/path/path/path");
523 
524         final CookieSpec cookiespec = new BrowserCompatSpec();
525         final CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/path", false);
526         try {
527             final List<Cookie> cookies = cookiespec.parse(header, origin);
528             for (int i = 0; i < cookies.size(); i++) {
529                 cookiespec.validate(cookies.get(i), origin);
530             }
531             Assert.fail("MalformedCookieException should have been thrown.");
532         } catch (final MalformedCookieException e) {
533             // expected
534         }
535     }
536 
537     @Test
538     public void testParseWithPathMismatch2() throws Exception {
539         final Header header = new BasicHeader("Set-Cookie",
540             "cookie-name=cookie-value; path=/foobar");
541 
542         final CookieSpec cookiespec = new BrowserCompatSpec();
543         final CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/foo", false);
544         try {
545             final List<Cookie> cookies = cookiespec.parse(header, origin);
546             for (int i = 0; i < cookies.size(); i++) {
547                 cookiespec.validate(cookies.get(i), origin);
548             }
549             Assert.fail("MalformedCookieException should have been thrown.");
550         } catch (final MalformedCookieException e) {
551             // expected
552         }
553     }
554 
555     /**
556      * Tests if cookie constructor rejects cookie name containing blanks.
557      */
558     @Test
559     public void testCookieNameWithBlanks() throws Exception {
560         final Header header = new BasicHeader("Set-Cookie", "invalid name=");
561         final CookieSpec cookiespec = new BrowserCompatSpec();
562         final CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/", false);
563         final List<Cookie> cookies = cookiespec.parse(header, origin);
564         for (int i = 0; i < cookies.size(); i++) {
565             cookiespec.validate(cookies.get(i), origin);
566         }
567         Assert.assertNotNull(cookies);
568         Assert.assertEquals(1, cookies.size());
569     }
570 
571     /**
572      * Tests if cookie constructor rejects cookie name containing blanks.
573      */
574     @Test
575     public void testCookieNameBlank() throws Exception {
576         final Header header = new BasicHeader("Set-Cookie", "=stuff");
577         final CookieSpec cookiespec = new BrowserCompatSpec();
578         final CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/", false);
579         try {
580             final List<Cookie> cookies = cookiespec.parse(header, origin);
581             for (int i = 0; i < cookies.size(); i++) {
582                 cookiespec.validate(cookies.get(i), origin);
583             }
584             Assert.fail("MalformedCookieException should have been thrown");
585         } catch (final MalformedCookieException expected) {
586         }
587     }
588 
589     /**
590      * Tests if cookie constructor rejects cookie name starting with $.
591      */
592     @Test
593     public void testCookieNameStartingWithDollarSign() throws Exception {
594         final Header header = new BasicHeader("Set-Cookie", "$invalid_name=");
595         final CookieSpec cookiespec = new BrowserCompatSpec();
596         final CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/", false);
597         final List<Cookie> cookies = cookiespec.parse(header, origin);
598         for (int i = 0; i < cookies.size(); i++) {
599             cookiespec.validate(cookies.get(i), origin);
600         }
601         Assert.assertNotNull(cookies);
602         Assert.assertEquals(1, cookies.size());
603     }
604 
605 
606     /**
607      * Tests if malformatted expires attribute is cookies correctly.
608      */
609     @Test
610     public void testCookieWithComma() throws Exception {
611         final Header header = new BasicHeader("Set-Cookie", "name=value; expires=\"Thu, 01-Jan-1970 00:00:00 GMT");
612 
613         final CookieSpec cookiespec = new BrowserCompatSpec();
614         final CookieOrigin origin = new CookieOrigin("localhost", 80, "/", false);
615         try {
616             final List<Cookie> cookies = cookiespec.parse(header, origin);
617             for (int i = 0; i < cookies.size(); i++) {
618                 cookiespec.validate(cookies.get(i), origin);
619             }
620             Assert.fail("MalformedCookieException should have been thrown");
621         } catch (final MalformedCookieException expected) {
622         }
623     }
624 
625 
626     /**
627      * Tests several date formats.
628      */
629     @Test
630     public void testDateFormats() throws Exception {
631         //comma, dashes
632         checkDate("Thu, 01-Jan-70 00:00:10 GMT");
633         checkDate("Thu, 01-Jan-2070 00:00:10 GMT");
634         //no comma, dashes
635         checkDate("Thu 01-Jan-70 00:00:10 GMT");
636         checkDate("Thu 01-Jan-2070 00:00:10 GMT");
637         //comma, spaces
638         checkDate("Thu, 01 Jan 70 00:00:10 GMT");
639         checkDate("Thu, 01 Jan 2070 00:00:10 GMT");
640         //no comma, spaces
641         checkDate("Thu 01 Jan 70 00:00:10 GMT");
642         checkDate("Thu 01 Jan 2070 00:00:10 GMT");
643         //weird stuff
644         checkDate("Wed, 20-Nov-2002 09-38-33 GMT");
645 
646 
647         try {
648             checkDate("this aint a date");
649             Assert.fail("Date check is bogus");
650         } catch(final Exception e) {
651         }
652     }
653 
654     private void checkDate(final String date) throws Exception {
655         final Header header = new BasicHeader("Set-Cookie", "custno=12345;Expires='"+date+"';");
656         final CookieSpec cookiespec = new BrowserCompatSpec();
657         final CookieOrigin origin = new CookieOrigin("localhost", 80, "/", false);
658         final List<Cookie> cookies = cookiespec.parse(header, origin);
659         for (int i = 0; i < cookies.size(); i++) {
660             cookiespec.validate(cookies.get(i), origin);
661         }
662     }
663 
664     /**
665      * Tests if invalid second domain level cookie gets accepted in the
666      * browser compatibility mode.
667      */
668     @Test
669     public void testSecondDomainLevelCookie() throws Exception {
670         final BasicClientCookie cookie = new BasicClientCookie("name", null);
671         cookie.setDomain(".sourceforge.net");
672         cookie.setAttribute(ClientCookie.DOMAIN_ATTR, cookie.getDomain());
673         cookie.setPath("/");
674         cookie.setAttribute(ClientCookie.PATH_ATTR, cookie.getPath());
675 
676         final CookieSpec cookiespec = new BrowserCompatSpec();
677         final CookieOrigin origin = new CookieOrigin("sourceforge.net", 80, "/", false);
678         cookiespec.validate(cookie, origin);
679     }
680 
681     @Test
682     public void testSecondDomainLevelCookieMatch1() throws Exception {
683         final BasicClientCookie cookie = new BasicClientCookie("name", null);
684         cookie.setDomain(".sourceforge.net");
685         cookie.setAttribute(ClientCookie.DOMAIN_ATTR, cookie.getDomain());
686         cookie.setPath("/");
687         cookie.setAttribute(ClientCookie.PATH_ATTR, cookie.getPath());
688 
689         final CookieSpec cookiespec = new BrowserCompatSpec();
690         final CookieOrigin origin = new CookieOrigin("sourceforge.net", 80, "/", false);
691         Assert.assertTrue(cookiespec.match(cookie, origin));
692     }
693 
694     @Test
695     public void testSecondDomainLevelCookieMatch2() throws Exception {
696         final BasicClientCookie cookie = new BasicClientCookie("name", null);
697         cookie.setDomain("sourceforge.net");
698         cookie.setAttribute(ClientCookie.DOMAIN_ATTR, cookie.getDomain());
699         cookie.setPath("/");
700         cookie.setAttribute(ClientCookie.PATH_ATTR, cookie.getPath());
701 
702         final CookieSpec cookiespec = new BrowserCompatSpec();
703         final CookieOrigin origin = new CookieOrigin("www.sourceforge.net", 80, "/", false);
704         Assert.assertTrue(cookiespec.match(cookie, origin));
705     }
706 
707     @Test
708     public void testSecondDomainLevelCookieMatch3() throws Exception {
709         final BasicClientCookie cookie = new BasicClientCookie("name", null);
710         cookie.setDomain(".sourceforge.net");
711         cookie.setAttribute(ClientCookie.DOMAIN_ATTR, cookie.getDomain());
712         cookie.setPath("/");
713         cookie.setAttribute(ClientCookie.PATH_ATTR, cookie.getPath());
714 
715          final CookieSpec cookiespec = new BrowserCompatSpec();
716          final CookieOrigin origin = new CookieOrigin("www.sourceforge.net", 80, "/", false);
717          Assert.assertTrue(cookiespec.match(cookie, origin));
718     }
719 
720     @Test
721     public void testInvalidSecondDomainLevelCookieMatch1() throws Exception {
722         final BasicClientCookie cookie = new BasicClientCookie("name", null);
723         cookie.setDomain(".sourceforge.net");
724         cookie.setAttribute(ClientCookie.DOMAIN_ATTR, cookie.getDomain());
725         cookie.setPath("/");
726         cookie.setAttribute(ClientCookie.PATH_ATTR, cookie.getPath());
727 
728         final CookieSpec cookiespec = new BrowserCompatSpec();
729         final CookieOrigin origin = new CookieOrigin("antisourceforge.net", 80, "/", false);
730         Assert.assertFalse(cookiespec.match(cookie, origin));
731     }
732 
733     @Test
734     public void testInvalidSecondDomainLevelCookieMatch2() throws Exception {
735         final BasicClientCookie cookie = new BasicClientCookie("name", null);
736         cookie.setDomain("sourceforge.net");
737         cookie.setAttribute(ClientCookie.DOMAIN_ATTR, cookie.getDomain());
738         cookie.setPath("/");
739         cookie.setAttribute(ClientCookie.PATH_ATTR, cookie.getPath());
740 
741         final CookieSpec cookiespec = new BrowserCompatSpec();
742         final CookieOrigin origin = new CookieOrigin("antisourceforge.net", 80, "/", false);
743         Assert.assertFalse(cookiespec.match(cookie, origin));
744     }
745 
746     @Test
747     public void testMatchBlankPath() throws Exception {
748         final CookieSpec cookiespec = new BrowserCompatSpec();
749         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
750         cookie.setDomain("host");
751         cookie.setPath("/");
752         final CookieOrigin origin = new CookieOrigin("host", 80, "  ", false);
753         Assert.assertTrue(cookiespec.match(cookie, origin));
754     }
755 
756     @Test
757     public void testMatchNullCookieDomain() throws Exception {
758         final CookieSpec cookiespec = new BrowserCompatSpec();
759         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
760         cookie.setPath("/");
761         final CookieOrigin origin = new CookieOrigin("host", 80, "/", false);
762         Assert.assertFalse(cookiespec.match(cookie, origin));
763     }
764 
765     @Test
766     public void testMatchNullCookiePath() throws Exception {
767         final CookieSpec cookiespec = new BrowserCompatSpec();
768         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
769         cookie.setDomain("host");
770         final CookieOrigin origin = new CookieOrigin("host", 80, "/", false);
771         Assert.assertTrue(cookiespec.match(cookie, origin));
772     }
773 
774     @Test
775     public void testCookieMatch1() throws Exception {
776         final CookieSpec cookiespec = new BrowserCompatSpec();
777         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
778         cookie.setDomain("host");
779         cookie.setPath("/");
780         final CookieOrigin origin = new CookieOrigin("host", 80, "/", false);
781         Assert.assertTrue(cookiespec.match(cookie, origin));
782     }
783 
784     @Test
785     public void testCookieMatch2() throws Exception {
786         final CookieSpec cookiespec = new BrowserCompatSpec();
787         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
788         cookie.setDomain(".whatever.com");
789         cookie.setPath("/");
790         final CookieOrigin origin = new CookieOrigin(".whatever.com", 80, "/", false);
791         Assert.assertTrue(cookiespec.match(cookie, origin));
792     }
793 
794     @Test
795     public void testCookieMatch3() throws Exception {
796         final CookieSpec cookiespec = new BrowserCompatSpec();
797         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
798         cookie.setDomain(".whatever.com");
799         cookie.setPath("/");
800         final CookieOrigin origin = new CookieOrigin(".really.whatever.com", 80, "/", false);
801         Assert.assertTrue(cookiespec.match(cookie, origin));
802     }
803 
804     @Test
805     public void testCookieMatch4() throws Exception {
806         final CookieSpec cookiespec = new BrowserCompatSpec();
807         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
808         cookie.setDomain("host");
809         cookie.setPath("/");
810         final CookieOrigin origin = new CookieOrigin("host", 80, "/foobar", false);
811         Assert.assertTrue(cookiespec.match(cookie, origin));
812     }
813 
814     @Test
815     public void testCookieMismatch1() throws Exception {
816         final CookieSpec cookiespec = new BrowserCompatSpec();
817         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
818         cookie.setDomain("host1");
819         cookie.setPath("/");
820         final CookieOrigin origin = new CookieOrigin("host2", 80, "/", false);
821         Assert.assertFalse(cookiespec.match(cookie, origin));
822     }
823 
824     @Test
825     public void testCookieMismatch2() throws Exception {
826         final CookieSpec cookiespec = new BrowserCompatSpec();
827         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
828         cookie.setDomain(".aaaaaaaaa.com");
829         cookie.setPath("/");
830         final CookieOrigin origin = new CookieOrigin(".bbbbbbbb.com", 80, "/", false);
831         Assert.assertFalse(cookiespec.match(cookie, origin));
832     }
833 
834     @Test
835     public void testCookieMismatch3() throws Exception {
836         final CookieSpec cookiespec = new BrowserCompatSpec();
837         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
838         cookie.setDomain("host");
839         cookie.setPath("/foobar");
840         final CookieOrigin origin = new CookieOrigin("host", 80, "/foo", false);
841         Assert.assertFalse(cookiespec.match(cookie, origin));
842     }
843 
844     @Test
845     public void testCookieMismatch4() throws Exception {
846         final CookieSpec cookiespec = new BrowserCompatSpec();
847         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
848         cookie.setDomain("host");
849         cookie.setPath("/foobar");
850         final CookieOrigin origin = new CookieOrigin("host", 80, "/foobar/", false);
851         Assert.assertTrue(cookiespec.match(cookie, origin));
852     }
853 
854     @Test
855     public void testCookieMatch5() throws Exception {
856         final CookieSpec cookiespec = new BrowserCompatSpec();
857         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
858         cookie.setDomain("host");
859         cookie.setPath("/foobar/r");
860         final CookieOrigin origin = new CookieOrigin("host", 80, "/foobar/", false);
861         Assert.assertFalse(cookiespec.match(cookie, origin));
862     }
863 
864     @Test
865     public void testCookieMismatch6() throws Exception {
866         final CookieSpec cookiespec = new BrowserCompatSpec();
867         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
868         cookie.setDomain("host");
869         cookie.setPath("/foobar");
870         cookie.setSecure(true);
871         final CookieOrigin origin = new CookieOrigin("host", 80, "/foobar", false);
872         Assert.assertFalse(cookiespec.match(cookie, origin));
873     }
874 
875     @Test
876     public void testInvalidMatchDomain() throws Exception {
877         final BasicClientCookie cookie = new BasicClientCookie("name", null);
878         cookie.setDomain("beta.gamma.com");
879         cookie.setAttribute(ClientCookie.DOMAIN_ATTR, cookie.getDomain());
880         cookie.setPath("/");
881         cookie.setAttribute(ClientCookie.PATH_ATTR, cookie.getPath());
882 
883         final CookieSpec cookiespec = new BrowserCompatSpec();
884         final CookieOrigin origin = new CookieOrigin("alpha.beta.gamma.com", 80, "/", false);
885         cookiespec.validate(cookie, origin);
886         Assert.assertTrue(cookiespec.match(cookie, origin));
887     }
888 
889     /**
890      * Tests generic cookie formatting.
891      */
892     @Test
893     public void testGenericCookieFormatting() throws Exception {
894         final Header header = new BasicHeader("Set-Cookie",
895             "name=value; path=/; domain=.mydomain.com");
896         final CookieSpec cookiespec = new BrowserCompatSpec();
897         final CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
898         final List<Cookie> cookies = cookiespec.parse(header, origin);
899         cookiespec.validate(cookies.get(0), origin);
900         final List<Header> headers = cookiespec.formatCookies(cookies);
901         Assert.assertNotNull(headers);
902         Assert.assertEquals(1, headers.size());
903         Assert.assertEquals("name=value", headers.get(0).getValue());
904     }
905 
906     /**
907      * Tests if null cookie values are handled correctly.
908      */
909     @Test
910     public void testNullCookieValueFormatting() {
911         final BasicClientCookie cookie = new BasicClientCookie("name", null);
912         cookie.setDomain(".whatever.com");
913         cookie.setAttribute(ClientCookie.DOMAIN_ATTR, cookie.getDomain());
914         cookie.setPath("/");
915         cookie.setAttribute(ClientCookie.PATH_ATTR, cookie.getPath());
916 
917         final CookieSpec cookiespec = new BrowserCompatSpec();
918         final List<Cookie> cookies = new ArrayList<Cookie>(1);
919         cookies.add(cookie);
920         final List<Header> headers = cookiespec.formatCookies(cookies);
921         Assert.assertNotNull(headers);
922         Assert.assertEquals(1, headers.size());
923         Assert.assertEquals("name=", headers.get(0).getValue());
924     }
925 
926     @Test
927     public void testNullCookieValueFormattingCookieVersion1() {
928         final BasicClientCookie cookie = new BasicClientCookie("name", null);
929         cookie.setVersion(1);
930         cookie.setDomain(".whatever.com");
931         cookie.setAttribute(ClientCookie.DOMAIN_ATTR, cookie.getDomain());
932         cookie.setPath("/");
933         cookie.setAttribute(ClientCookie.PATH_ATTR, cookie.getPath());
934 
935         final CookieSpec cookiespec = new BrowserCompatSpec();
936         final List<Cookie> cookies = new ArrayList<Cookie>(1);
937         cookies.add(cookie);
938         final List<Header> headers = cookiespec.formatCookies(cookies);
939         Assert.assertNotNull(headers);
940         Assert.assertEquals(1, headers.size());
941         Assert.assertEquals("name", headers.get(0).getValue());
942     }
943 
944     /**
945      * Tests generic cookie formatting.
946      */
947     @Test
948     public void testFormatSeveralCookies() throws Exception {
949         final Header header = new BasicHeader("Set-Cookie",
950             "name1=value1; path=/; domain=.mydomain.com, name2 = value2 ; path=/; domain=.mydomain.com; version=0");
951         final CookieSpec cookiespec = new BrowserCompatSpec();
952         final CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
953         final List<Cookie> cookies = cookiespec.parse(header, origin);
954         final List<Header> headers = cookiespec.formatCookies(cookies);
955         Assert.assertNotNull(headers);
956         Assert.assertEquals(1, headers.size());
957         Assert.assertEquals("name1=value1; name2=value2", headers.get(0).getValue());
958     }
959 
960     @SuppressWarnings("unused")
961     @Test
962     public void testKeepCloverHappy() throws Exception {
963         new MalformedCookieException();
964         new MalformedCookieException("whatever");
965         new MalformedCookieException("whatever", null);
966     }
967 
968     @Test
969     public void testInvalidInput() throws Exception {
970         final CookieSpec cookiespec = new BrowserCompatSpec();
971         try {
972             cookiespec.parse(null, null);
973             Assert.fail("IllegalArgumentException must have been thrown");
974         } catch (final IllegalArgumentException ex) {
975             // expected
976         }
977         try {
978             cookiespec.parse(new BasicHeader("Set-Cookie", "name=value"), null);
979             Assert.fail("IllegalArgumentException must have been thrown");
980         } catch (final IllegalArgumentException ex) {
981             // expected
982         }
983         try {
984             cookiespec.validate(null, null);
985             Assert.fail("IllegalArgumentException must have been thrown");
986         } catch (final IllegalArgumentException ex) {
987             // expected
988         }
989         try {
990             cookiespec.validate(new BasicClientCookie("name", null), null);
991             Assert.fail("IllegalArgumentException must have been thrown");
992         } catch (final IllegalArgumentException ex) {
993             // expected
994         }
995         try {
996             cookiespec.match(null, null);
997             Assert.fail("IllegalArgumentException must have been thrown");
998         } catch (final IllegalArgumentException ex) {
999             // expected
1000         }
1001         try {
1002             cookiespec.match(new BasicClientCookie("name", null), null);
1003             Assert.fail("IllegalArgumentException must have been thrown");
1004         } catch (final IllegalArgumentException ex) {
1005             // expected
1006         }
1007         try {
1008             cookiespec.formatCookies(null);
1009             Assert.fail("IllegalArgumentException must have been thrown");
1010         } catch (final IllegalArgumentException ex) {
1011             // expected
1012         }
1013         try {
1014             final List<Cookie> cookies = new ArrayList<Cookie>();
1015             cookiespec.formatCookies(cookies);
1016             Assert.fail("IllegalArgumentException must have been thrown");
1017         } catch (final IllegalArgumentException ex) {
1018             // expected
1019         }
1020     }
1021 
1022     /**
1023      * Tests cookie version 1 with space in cookie value.
1024      */
1025     @Test
1026     public void testFormatCookieWithSpaceInValue() throws Exception {
1027         final CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
1028         final CookieSpec cookieSpec = new BrowserCompatSpec();
1029         final Header setCookieHeader = new BasicHeader("Set-Cookie", "test=\"value 1\"; Version=1");
1030         final Cookie cookie = cookieSpec.parse(setCookieHeader, origin).get(0);
1031         final List<Cookie> cookies = new ArrayList<Cookie>();
1032         cookies.add(cookie);
1033         final List<Header> headers = cookieSpec.formatCookies(cookies);
1034         Assert.assertNotNull(headers);
1035         Assert.assertEquals(1, headers.size());
1036         Assert.assertEquals("test=\"value 1\"", headers.get(0).getValue());
1037     }
1038 
1039     /**
1040      * Tests Netscape cookie with space in cookie value.
1041      */
1042     @Test
1043     public void testFormatCookieVersion0WithSpaceInValue() throws Exception {
1044         final CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
1045         final CookieSpec cookieSpec = new BrowserCompatSpec();
1046         final Header setCookieHeader = new BasicHeader("Set-Cookie", "test=value 1");
1047         final Cookie cookie = cookieSpec.parse(setCookieHeader, origin).get(0);
1048         final List<Cookie> cookies = new ArrayList<Cookie>();
1049         cookies.add(cookie);
1050         final List<Header> headers = cookieSpec.formatCookies(cookies);
1051         Assert.assertNotNull(headers);
1052         Assert.assertEquals(1, headers.size());
1053         Assert.assertEquals("test=value 1", headers.get(0).getValue());
1054     }
1055 
1056     @Test
1057     public void testVersion1CookieWithInvalidExpires() throws Exception {
1058         final CookieSpec cookiespec = new BrowserCompatSpec();
1059         final CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
1060 
1061         final Header origHeader = new BasicHeader("Set-Cookie",
1062             "test=\"test\"; Version=1; Expires=Mon, 11-Feb-2013 10:39:19 GMT; Path=/");
1063         final List<Cookie> cookies = cookiespec.parse(origHeader, origin);
1064         Assert.assertNotNull(cookies);
1065         Assert.assertEquals(1, cookies.size());
1066 
1067         final List<Header> headers = cookiespec.formatCookies(cookies);
1068         Assert.assertNotNull(headers);
1069         Assert.assertEquals(1, headers.size());
1070         final Header header1 = headers.get(0);
1071         Assert.assertEquals("test=\"test\"", header1.getValue());
1072     }
1073 
1074 }