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;
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  
23  import org.htmlunit.util.Cookie;
24  import org.junit.Test;
25  
26  /**
27   * Unit tests for {@link CookieManager}.
28   *
29   * @author Daniel Gredler
30   * @author Ahmed Ashour
31   * @author Marc Guillemot
32   * @author Frank Danek
33   * @author Ronald Brill
34   */
35  public class CookieManager3Test {
36  
37      /**
38       * Verifies the basic cookie manager behavior.
39       */
40      @Test
41      public void basicBehavior() {
42          // Create a new cookie manager.
43          final CookieManager mgr = new CookieManager();
44          assertTrue(mgr.isCookiesEnabled());
45          assertTrue(mgr.getCookies().isEmpty());
46  
47          // Add a cookie to the manager.
48          final Cookie cookie = new Cookie("localhost", "a", "b");
49          mgr.addCookie(cookie);
50          assertFalse(mgr.getCookies().isEmpty());
51  
52          // Remove the cookie from the manager.
53          mgr.removeCookie(cookie);
54          assertTrue(mgr.getCookies().isEmpty());
55  
56          // Add the cookie back to the manager.
57          mgr.addCookie(cookie);
58          assertFalse(mgr.getCookies().isEmpty());
59  
60          // Clear all cookies from the manager.
61          mgr.clearCookies();
62          assertTrue(mgr.getCookies().isEmpty());
63  
64          // Add a cookie before disabling cookies.
65          mgr.addCookie(cookie);
66          assertEquals(1, mgr.getCookies().size());
67  
68          // Disable cookies.
69          mgr.setCookiesEnabled(false);
70          assertFalse(mgr.isCookiesEnabled());
71          assertTrue(mgr.getCookies().isEmpty());
72  
73          // Add a cookie after disabling cookies.
74          final Cookie cookie2 = new Cookie("a", "b", "c", "d", new Date(System.currentTimeMillis() + 5000), false);
75          mgr.addCookie(cookie2);
76          assertTrue(mgr.getCookies().isEmpty());
77          assertFalse(mgr.clearExpired(new Date(System.currentTimeMillis() + 10_000)));
78  
79          // Enable cookies again.
80          mgr.setCookiesEnabled(true);
81          assertTrue(mgr.isCookiesEnabled());
82          assertEquals(1, mgr.getCookies().size());
83  
84          // Clear expired cookies
85          assertFalse(mgr.clearExpired(new Date(System.currentTimeMillis() + 10_000)));
86          assertEquals(1, mgr.getCookies().size());
87  
88          mgr.addCookie(cookie2);
89          assertEquals(2, mgr.getCookies().size());
90          assertTrue(mgr.clearExpired(new Date(System.currentTimeMillis() + 10_000)));
91          assertEquals(1, mgr.getCookies().size());
92      }
93  }