1
2
3
4
5
6
7
8
9
10
11
12
13
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
33
34
35
36 public class HtmlUnitCookieStoreTest {
37
38 private CookieManager mgr_;
39 private HtmlUnitCookieStore store_;
40
41
42
43
44 @Before
45 public void setUp() {
46 mgr_ = new CookieManager();
47 store_ = new HtmlUnitCookieStore(mgr_);
48 }
49
50
51
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
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
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
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 }