View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.httpclient;
16  
17  import static org.junit.Assert.assertEquals;
18  import static org.junit.Assert.assertFalse;
19  import static org.junit.Assert.assertTrue;
20  
21  import java.util.Date;
22  import java.util.List;
23  
24  import org.apache.commons.lang3.builder.EqualsBuilder;
25  import org.apache.http.impl.cookie.BasicClientCookie;
26  import org.htmlunit.CookieManager;
27  import org.htmlunit.util.Cookie;
28  import org.junit.Before;
29  import org.junit.Test;
30  
31  /**
32   * Tests the HtmlUnitCookieStore, which is a wrapper around the CookieManager.
33   *
34   * @author James Phillpotts
35   */
36  public class HtmlUnitCookieStoreTest {
37  
38      private CookieManager mgr_;
39      private HtmlUnitCookieStore store_;
40  
41      /**
42       * Setup.
43       */
44      @Before
45      public void setUp() {
46          mgr_ = new CookieManager();
47          store_ = new HtmlUnitCookieStore(mgr_);
48      }
49  
50      /**
51       * Simple test for addCookie.
52       */
53      @Test
54      public void addCookie() {
55          final BasicClientCookie c = new BasicClientCookie("myname", "myvalue");
56          c.setDomain("localhost");
57          store_.addCookie(c);
58  
59          assertEquals(1, mgr_.getCookies().size());
60          assertEquals("myvalue", mgr_.getCookie("myname").getValue());
61      }
62  
63      /**
64       * Simple test for getCookies.
65       */
66      @Test
67      public void getCookies() {
68          mgr_.addCookie(new Cookie("localhost", "myname", "myvalue"));
69          mgr_.addCookie(new Cookie("localhost", "myname2", "myvalue2"));
70  
71          final List<org.apache.http.cookie.Cookie> cookies = store_.getCookies();
72          assertEquals(2, cookies.size());
73          assertTrue(cookies.contains(new MyCookie("myname", "myvalue")));
74          assertTrue(cookies.contains(new MyCookie("myname2", "myvalue2")));
75  
76          mgr_.setCookiesEnabled(false);
77          assertTrue(store_.getCookies().isEmpty());
78      }
79  
80      /**
81       * Simple test for clearExpired.
82       */
83      @Test
84      public void clearExpired() {
85          mgr_.addCookie(new Cookie("localhost", "myname", "myvalue"));
86          final Cookie cookie = new Cookie("localhost", "myname2", "myvalue2", null,
87                  new Date(System.currentTimeMillis() + 10_000), false);
88          mgr_.addCookie(cookie);
89  
90          assertTrue(store_.clearExpired(new Date(System.currentTimeMillis() + 20_000)));
91          assertFalse(store_.clearExpired(new Date(System.currentTimeMillis() + 20_000)));
92  
93          final List<org.apache.http.cookie.Cookie> cookies = store_.getCookies();
94          assertEquals(1, cookies.size());
95          assertTrue(cookies.contains(new MyCookie("myname", "myvalue")));
96      }
97  
98      /**
99       * Simple test for clear.
100      */
101     @Test
102     public void clear() {
103         mgr_.addCookie(new Cookie("localhost", "myname", "myvalue"));
104         mgr_.addCookie(new Cookie("localhost", "myname2", "myvalue2"));
105 
106         assertFalse(store_.getCookies().isEmpty());
107 
108         store_.clear();
109         assertTrue(store_.getCookies().isEmpty());
110     }
111 
112     private static final class MyCookie extends BasicClientCookie {
113         MyCookie(final String name, final String value) {
114             super(name, value);
115         }
116 
117         @Override
118         public boolean equals(final Object obj) {
119             return obj instanceof org.apache.http.cookie.Cookie
120                 && new EqualsBuilder()
121                     .append(getName(), ((org.apache.http.cookie.Cookie) obj).getName())
122                     .append(getValue(), ((org.apache.http.cookie.Cookie) obj).getValue())
123                     .isEquals();
124         }
125 
126         @Override
127         public int hashCode() {
128             return super.hashCode();
129         }
130     }
131 }