-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathCookieManager5Test.java
More file actions
250 lines (213 loc) · 9.88 KB
/
CookieManager5Test.java
File metadata and controls
250 lines (213 loc) · 9.88 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* 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.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.client.utils.DateUtils;
import org.htmlunit.http.Cookie;
import org.htmlunit.util.MimeType;
import org.htmlunit.util.NameValuePair;
import org.junit.jupiter.api.Test;
import jakarta.servlet.Servlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
/**
* Unit tests for {@link CookieManager}.
*
* @author Ronald Brill
*/
public class CookieManager5Test extends WebServerTestCase {
/**
* @throws Exception if an error occurs
*/
@Test
public void sameDomainWithClientCookie() throws Exception {
final List<NameValuePair> headers = new ArrayList<>();
getMockWebConnection().setDefaultResponse("something", 200, "Ok", MimeType.TEXT_HTML, headers);
startWebServer(getMockWebConnection());
try (WebClient webClient = getWebClient()) {
webClient.getCookieManager().clearCookies();
webClient.getPage(CookieManager4Test.URL_HOST3);
WebRequest lastRequest = getMockWebConnection().getLastWebRequest();
assertNull(lastRequest.getAdditionalHeaders().get(HttpHeader.COOKIE));
final Cookie cookie = new Cookie(CookieManager4Test.DOMAIN, "name", "value");
webClient.getCookieManager().addCookie(cookie);
webClient.getPage(CookieManager4Test.URL_HOST3);
lastRequest = getMockWebConnection().getLastWebRequest();
assertEquals("name=value", lastRequest.getAdditionalHeaders().get(HttpHeader.COOKIE));
}
}
/**
* @throws Exception if an error occurs
*/
@Test
public void unqualifiedHostWithClientCookie() throws Exception {
final List<NameValuePair> headers = new ArrayList<>();
getMockWebConnection().setDefaultResponse("something", 200, "Ok", MimeType.TEXT_HTML, headers);
startWebServer(getMockWebConnection());
try (WebClient webClient = getWebClient()) {
webClient.getCookieManager().clearCookies();
webClient.getPage(CookieManager4Test.URL_HOST4);
WebRequest lastRequest = getMockWebConnection().getLastWebRequest();
assertNull(lastRequest.getAdditionalHeaders().get(HttpHeader.COOKIE));
final Cookie cookie = new Cookie(CookieManager4Test.DOMAIN, "name", "value");
webClient.getCookieManager().addCookie(cookie);
webClient.getPage(CookieManager4Test.URL_HOST4);
lastRequest = getMockWebConnection().getLastWebRequest();
assertNull(lastRequest.getAdditionalHeaders().get(HttpHeader.COOKIE));
}
}
/**
* @throws Exception if an error occurs
*/
@Test
public void subdomainWithClientCookie() throws Exception {
final List<NameValuePair> headers = new ArrayList<>();
getMockWebConnection().setDefaultResponse("something", 200, "Ok", MimeType.TEXT_HTML, headers);
startWebServer(getMockWebConnection());
try (WebClient webClient = getWebClient()) {
webClient.getCookieManager().clearCookies();
webClient.getPage(CookieManager4Test.URL_HOST1);
WebRequest lastRequest = getMockWebConnection().getLastWebRequest();
assertNull(lastRequest.getAdditionalHeaders().get(HttpHeader.COOKIE));
final Cookie cookie = new Cookie(CookieManager4Test.DOMAIN, "name", "value");
webClient.getCookieManager().addCookie(cookie);
webClient.getPage(CookieManager4Test.URL_HOST1);
lastRequest = getMockWebConnection().getLastWebRequest();
assertEquals("name=value", lastRequest.getAdditionalHeaders().get(HttpHeader.COOKIE));
}
}
/**
* @throws Exception if an error occurs
*/
@Test
public void differentSubdomainWithClientCookie() throws Exception {
final List<NameValuePair> headers = new ArrayList<>();
getMockWebConnection().setDefaultResponse("something", 200, "Ok", MimeType.TEXT_HTML, headers);
startWebServer(getMockWebConnection());
try (WebClient webClient = getWebClient()) {
webClient.getCookieManager().clearCookies();
webClient.getPage(CookieManager4Test.URL_HOST1);
WebRequest lastRequest = getMockWebConnection().getLastWebRequest();
assertNull(lastRequest.getAdditionalHeaders().get(HttpHeader.COOKIE));
final Cookie cookie = new Cookie("host2." + CookieManager4Test.DOMAIN, "name", "value");
webClient.getCookieManager().addCookie(cookie);
webClient.getPage(CookieManager4Test.URL_HOST1);
lastRequest = getMockWebConnection().getLastWebRequest();
assertNull(lastRequest.getAdditionalHeaders().get(HttpHeader.COOKIE));
}
}
/**
* Check the cookie expires gets overwritten.
*
* @throws Exception if the test fails
*/
@Test
public void updateCookieExpiresWithClientCookie() throws Exception {
final Date date = new Date(System.currentTimeMillis() + 500_000);
final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
servlets.put(SetCookieExpires10Servlet.URL, SetCookieExpires10Servlet.class);
servlets.put(SetCookieExpires1000Servlet.URL, SetCookieExpires1000Servlet.class);
startWebServer("./", servlets);
try (WebClient webClient = getWebClient()) {
webClient.getCookieManager().clearCookies();
final Date expires = new Date(System.currentTimeMillis() + 10_000L);
Cookie cookie = new Cookie("localhost", "first", "1", null, expires, false, false);
webClient.getCookieManager().addCookie(cookie);
assertEquals(1, webClient.getCookieManager().getCookies().size());
cookie = webClient.getCookieManager().getCookies().iterator().next();
assertFalse("" + cookie.getExpires(), cookie.getExpires().after(date));
webClient.getPage("http://localhost:" + PORT + SetCookieExpires1000Servlet.URL);
assertEquals(1, webClient.getCookieManager().getCookies().size());
cookie = webClient.getCookieManager().getCookies().iterator().next();
assertTrue("" + cookie.getExpires(), cookie.getExpires().after(date));
}
}
/**
* Check the cookie expires gets overwritten.
*
* @throws Exception if the test fails
*/
@Test
public void updateCookieExpires() throws Exception {
final Date date = new Date(System.currentTimeMillis() + 500_000);
final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
servlets.put(SetCookieExpires10Servlet.URL, SetCookieExpires10Servlet.class);
servlets.put(SetCookieExpires1000Servlet.URL, SetCookieExpires1000Servlet.class);
startWebServer("./", servlets);
try (WebClient webClient = getWebClient()) {
webClient.getPage("http://localhost:" + PORT + SetCookieExpires10Servlet.URL);
assertEquals(1, webClient.getCookieManager().getCookies().size());
Cookie cookie = webClient.getCookieManager().getCookies().iterator().next();
assertFalse("" + cookie.getExpires(), cookie.getExpires().after(date));
webClient.getPage("http://localhost:" + PORT + SetCookieExpires1000Servlet.URL);
assertEquals(1, webClient.getCookieManager().getCookies().size());
cookie = webClient.getCookieManager().getCookies().iterator().next();
assertTrue("" + cookie.getExpires(), cookie.getExpires().after(date));
}
}
@Override
protected WebClient getWebClient() {
final WebClient webClient = super.getWebClient();
// set timeout to fail fast when the url not mapped
webClient.getOptions().setTimeout(1000);
return webClient;
}
/**
* Helper class for {@link #updateCookieExpires}.
*/
public abstract static class SetCookieExpiresServlet extends HttpServlet {
private static final String HTML_ = DOCTYPE_HTML
+ "<html><head></head>\n"
+ "<body>\n"
+ "</body></html>";
protected abstract long getTimeout();
/**
* {@inheritDoc}
*/
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
resp.setStatus(200);
final String expires = DateUtils.formatDate(new Date(System.currentTimeMillis() + getTimeout()));
resp.setHeader("Set-Cookie", "first=1; expires=" + expires + ";");
resp.getWriter().write(HTML_);
}
}
/**
* Helper class for {@link #updateCookieExpires}.
*/
public static class SetCookieExpires10Servlet extends SetCookieExpiresServlet {
static final String URL = "/test10";
@Override
protected long getTimeout() {
return 10_000L;
}
}
/**
* Helper class for {@link #updateCookieExpires}.
*/
public static class SetCookieExpires1000Servlet extends SetCookieExpiresServlet {
static final String URL = "/test1000";
@Override
protected long getTimeout() {
return 1_000_000L;
}
}
}