-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathCookieManager2Test.java
More file actions
167 lines (147 loc) · 6.36 KB
/
CookieManager2Test.java
File metadata and controls
167 lines (147 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright (c) 2002-2026 Gargoyle Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.htmlunit;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.htmlunit.html.HtmlPage;
import org.htmlunit.http.Cookie;
import org.htmlunit.junit.annotation.Alerts;
import org.junit.jupiter.api.Test;
/**
* Unit tests for {@link CookieManager}.
*
* @author Daniel Gredler
* @author Ahmed Ashour
* @author Marc Guillemot
* @author Ronald Brill
*/
public class CookieManager2Test extends SimpleWebTestCase {
/**
* @throws Exception if the test fails
*/
@Test
public void resettingCookie() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><head>\n"
+ "<script>\n"
+ " function createCookie(name, value, days, path) {\n"
+ " if (days) {\n"
+ " var date = new Date();\n"
+ " date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));\n"
+ " var expires = '; expires=' + date.toGMTString();\n"
+ " }\n"
+ " else\n"
+ " var expires = '';\n"
+ " document.cookie = name + '=' + value + expires + '; path=' + path;\n"
+ " }\n"
+ "\n"
+ " function readCookie(name) {\n"
+ " var nameEQ = name + '=';\n"
+ " var ca = document.cookie.split(';');\n"
+ " for(var i = 0; i < ca.length; i++) {\n"
+ " var c = ca[i];\n"
+ " while (c.charAt(0) == ' ')\n"
+ " c = c.substring(1, c.length);\n"
+ " if (c.indexOf(nameEQ) == 0)\n"
+ " return c.substring(nameEQ.length, c.length);\n"
+ " }\n"
+ " return null;\n"
+ " }\n"
+ "</script></head><body>\n"
+ " <input id='button1' type='button' "
+ "onclick=\"createCookie('button','button1',1,'/'); alert('cookie:' + readCookie('button'));\" "
+ "value='Button 1'>\n"
+ " <input id='button2' type='button' "
+ "onclick=\"createCookie('button','button2',1,'/'); alert('cookie:' + readCookie('button'));\" "
+ "value='Button 2'>\n"
+ "</form></body></html>";
final String[] expectedAlerts = {"cookie:button1", "cookie:button2"};
final List<String> collectedAlerts = new ArrayList<>();
final HtmlPage page = loadPage(html, collectedAlerts);
page.getHtmlElementById("button1").click();
page.getHtmlElementById("button2").click();
assertEquals(expectedAlerts, collectedAlerts);
}
/**
* @throws Exception if the test fails
*/
@Test
@Alerts("my_key=§")
public void cookie_nullValue() throws Exception {
final WebClient webClient = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setResponse(URL_FIRST, CookieManagerTest.HTML_ALERT_COOKIE);
webClient.setWebConnection(webConnection);
final CookieManager mgr = webClient.getCookieManager();
mgr.addCookie(new Cookie(URL_FIRST.getHost(), "my_key", null, "/", null, false));
final List<String> collectedAlerts = new ArrayList<>();
webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final HtmlPage page = webClient.getPage(URL_FIRST);
assertEquals(getExpectedAlerts()[0], page.getTitleText());
}
/**
* @throws Exception if the test fails
*/
@Test
@Alerts("my_name=my_data§")
public void cookie_maxAgeMinusOne() throws Exception {
final WebClient webClient = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setResponse(URL_FIRST, CookieManagerTest.HTML_ALERT_COOKIE);
webClient.setWebConnection(webConnection);
final CookieManager mgr = webClient.getCookieManager();
mgr.addCookie(new Cookie(URL_FIRST.getHost(), "my_name", "my_data", "/", -1, false));
final HtmlPage page = webClient.getPage(URL_FIRST);
assertEquals(getExpectedAlerts()[0], page.getTitleText());
}
/**
* Regression test for bug 3053526: HtmlUnit was throwing an Exception when asking for cookies
* of "about:blank".
* @throws Exception if the test fails
*/
@Test
public void cookiesForAboutBlank() throws Exception {
final WebClient webClient = getWebClient();
final HtmlPage htmlPage = webClient.getPage("about:blank");
final Set<Cookie> cookies = webClient.getCookies(htmlPage.getUrl());
assertTrue(cookies.toString(), cookies.isEmpty());
}
/**
* This was causing a ConcurrentModificationException.
* In "real life" the problem was arising due to changes to the cookies made from
* the JS processing thread.
* @throws Exception if the test fails
*/
@Test
public void getCookiesShouldReturnACopyOfCurentState() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><body>\n"
+ "<button id='it' onclick=\"document.cookie = 'foo=bla'\">click me</button>\n"
+ "<script>\n"
+ "document.cookie = 'cookie1=value1';\n"
+ "</script></body></html>";
final WebClient webClient = getWebClient();
final HtmlPage page = loadPage(html);
final Set<Cookie> initialCookies = webClient.getCookieManager().getCookies();
assertEquals(1, initialCookies.size());
final Iterator<Cookie> iterator = initialCookies.iterator();
page.getHtmlElementById("it").click();
iterator.next(); // ConcurrentModificationException was here
assertEquals(1, initialCookies.size());
assertEquals(2, webClient.getCookieManager().getCookies().size());
}
}