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.hc.client5.http.impl.cookie;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.ObjectInputStream;
33  import java.io.ObjectOutputStream;
34  import java.util.Calendar;
35  import java.util.List;
36  
37  import org.apache.hc.client5.http.cookie.BasicCookieStore;
38  import org.apache.hc.client5.http.cookie.Cookie;
39  import org.junit.Assert;
40  import org.junit.Test;
41  
42  /**
43   * Unit tests for {@link BasicCookieStore}.
44   */
45  public class TestBasicCookieStore {
46  
47      @Test
48      public void testBasics() throws Exception {
49          final BasicCookieStore store = new BasicCookieStore();
50          store.addCookie(new BasicClientCookie("name1", "value1"));
51          store.addCookies(new BasicClientCookie[] {new BasicClientCookie("name2", "value2")});
52          List<Cookie> list = store.getCookies();
53          Assert.assertNotNull(list);
54          Assert.assertEquals(2, list.size());
55          Assert.assertEquals("name1", list.get(0).getName());
56          Assert.assertEquals("name2", list.get(1).getName());
57          store.clear();
58          list = store.getCookies();
59          Assert.assertNotNull(list);
60          Assert.assertEquals(0, list.size());
61      }
62  
63      @Test
64      public void testExpiredCookie() throws Exception {
65          final BasicCookieStore store = new BasicCookieStore();
66          final BasicClientCookie cookie = new BasicClientCookie("name1", "value1");
67  
68          final Calendar c = Calendar.getInstance();
69          c.add(Calendar.DAY_OF_YEAR, -10);
70          cookie.setExpiryDate(c.getTime());
71          store.addCookie(cookie);
72          final List<Cookie> list = store.getCookies();
73          Assert.assertNotNull(list);
74          Assert.assertEquals(0, list.size());
75      }
76  
77      @Test
78      public void testSerialization() throws Exception {
79          final BasicCookieStore orig = new BasicCookieStore();
80          orig.addCookie(new BasicClientCookie("name1", "value1"));
81          orig.addCookie(new BasicClientCookie("name2", "value2"));
82          final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
83          final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer);
84          outStream.writeObject(orig);
85          outStream.close();
86          final byte[] raw = outbuffer.toByteArray();
87          final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
88          final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
89          final BasicCookieStore clone = (BasicCookieStore) inStream.readObject();
90          final List<Cookie> expected = orig.getCookies();
91          final List<Cookie> clones = clone.getCookies();
92          Assert.assertNotNull(expected);
93          Assert.assertNotNull(clones);
94          Assert.assertEquals(expected.size(), clones.size());
95          for (int i = 0; i < expected.size(); i++) {
96              Assert.assertEquals(expected.get(i).getName(), clones.get(i).getName());
97              Assert.assertEquals(expected.get(i).getValue(), clones.get(i).getValue());
98          }
99      }
100 
101 }