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.javascript.host.html;
16  
17  import static org.junit.Assert.fail;
18  
19  import java.net.URL;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.htmlunit.CollectingAlertHandler;
24  import org.htmlunit.CookieManager;
25  import org.htmlunit.ScriptException;
26  import org.htmlunit.SimpleWebTestCase;
27  import org.htmlunit.WebClient;
28  import org.htmlunit.html.HtmlPage;
29  import org.htmlunit.junit.BrowserRunner;
30  import org.htmlunit.junit.annotation.Alerts;
31  import org.htmlunit.util.Cookie;
32  import org.junit.Test;
33  import org.junit.runner.RunWith;
34  
35  /**
36   * Tests for {@link HTMLDocument}.
37   *
38   * @author Ahmed Ashour
39   * @author Marc Guillemot
40   * @author Ronald Brill
41   */
42  @RunWith(BrowserRunner.class)
43  public class HTMLDocument2Test extends SimpleWebTestCase {
44  
45      /**
46       * @throws Exception if the test fails
47       */
48      @Test
49      @Alerts({"www.gargoylesoftware.com", "gargoylesoftware.com"})
50      public void domain() throws Exception {
51          final String html = DOCTYPE_HTML
52              + "<html><head><title>foo</title><script>\n"
53              + "function doTest() {\n"
54              + "  alert(document.domain);\n"
55              + "  document.domain = 'gargoylesoftware.com';\n"
56              + "  alert(document.domain);\n"
57              + "}\n"
58              + "</script></head>\n"
59              + "<body onload='doTest()'>\n"
60              + "</body></html>";
61  
62          loadPageWithAlerts(html, new URL("http://www.gargoylesoftware.com/"), null);
63      }
64  
65      /**
66       * @throws Exception if the test fails
67       */
68      @Test
69      @Alerts({"localhost", "gargoylesoftware.com"})
70      public void domainFromLocalhost() throws Exception {
71          final String html = DOCTYPE_HTML
72              + "<html><head><title>foo</title><script>\n"
73              + "function doTest() {\n"
74              + "  alert(document.domain);\n"
75              + "  document.domain = 'gargoylesoftware.com';\n"
76              + "  alert(document.domain);\n"
77              + "}\n"
78              + "</script></head>\n"
79              + "<body onload='doTest()'>\n"
80              + "</body></html>";
81  
82          getMockWebConnection().setDefaultResponse(html);
83          loadPageWithAlerts(html, new URL("http://localhost"), null);
84      }
85  
86      /**
87       * @throws Exception if the test fails
88       */
89      @Test
90      @Alerts({"www.gargoylesoftware.com", "gargoylesoftware.com"})
91      public void domainMixedCaseNetscape() throws Exception {
92          final URL urlGargoyleUpperCase = new URL("http://WWW.GARGOYLESOFTWARE.COM/");
93  
94          final String html = DOCTYPE_HTML
95              + "<html><head><title>foo</title><script>\n"
96              + "function doTest() {\n"
97              + "  alert(document.domain);\n"
98              + "  document.domain = 'GaRgOyLeSoFtWaRe.CoM';\n"
99              + "  alert(document.domain);\n"
100             + "}\n"
101             + "</script></head>\n"
102             + "<body onload='doTest()'>\n"
103             + "</body></html>";
104 
105         getMockWebConnection().setDefaultResponse(html);
106         loadPageWithAlerts(html, urlGargoyleUpperCase, null);
107     }
108 
109     /**
110      * @throws Exception if the test fails
111      */
112     @Test
113     @Alerts({"www.gargoylesoftware.com", "gargoylesoftware.com"})
114     public void domainMixedCase() throws Exception {
115         final String html = DOCTYPE_HTML
116             + "<html><head><title>foo</title><script>\n"
117             + "function doTest() {\n"
118             + "  alert(document.domain);\n"
119             + "  document.domain = 'GaRgOyLeSoFtWaRe.CoM';\n"
120             + "  alert(document.domain);\n"
121             + "}\n"
122             + "</script></head>\n"
123             + "<body onload='doTest()'>\n"
124             + "</body></html>";
125 
126         loadPageWithAlerts(html, new URL("http://www.gargoylesoftware.com/"), null);
127     }
128 
129     /**
130      * @throws Exception if the test fails
131      */
132     @Test
133     @Alerts({"d4.d3.d2.d1.gargoylesoftware.com", "d4.d3.d2.d1.gargoylesoftware.com", "d1.gargoylesoftware.com"})
134     public void domainLong() throws Exception {
135         final String html = DOCTYPE_HTML
136             + "<html><head><title>foo</title><script>\n"
137             + "function doTest() {\n"
138             + "  alert(document.domain);\n"
139             + "  document.domain = 'd4.d3.d2.d1.gargoylesoftware.com';\n"
140             + "  alert(document.domain);\n"
141             + "  document.domain = 'd1.gargoylesoftware.com';\n"
142             + "  alert(document.domain);\n"
143             + "}\n"
144             + "</script></head>\n"
145             + "<body onload='doTest()'>\n"
146             + "</body></html>";
147 
148         getMockWebConnection().setDefaultResponse(html);
149         loadPageWithAlerts(html, new URL("http://d4.d3.d2.d1.gargoylesoftware.com"), null);
150     }
151 
152     /**
153      * @throws Exception if the test fails
154      */
155     @Test
156     @Alerts({"localhost", "localhost"})
157     public void domainSetSelf() throws Exception {
158         final String html = DOCTYPE_HTML
159             + "<html><head><title>foo</title><script>\n"
160             + "function doTest() {\n"
161             + "  alert(document.domain);\n"
162             + "  document.domain = 'localhost';\n"
163             + "  alert(document.domain);\n"
164             + "}\n"
165             + "</script></head>\n"
166             + "<body onload='doTest()'>\n"
167             + "</body></html>";
168 
169         getMockWebConnection().setDefaultResponse(html);
170         loadPageWithAlerts(html, new URL("http://localhost"), null);
171     }
172 
173     /**
174      * @throws Exception if the test fails
175      */
176     @Test
177     public void domainTooShort() throws Exception {
178         final String html = DOCTYPE_HTML
179             + "<html><head><title>foo</title><script>\n"
180             + "function doTest() {\n"
181             + "  alert(document.domain);\n"
182             + "  document.domain = 'com';\n"
183             + "  alert(document.domain);\n"
184             + "}\n"
185             + "</script></head>\n"
186             + "<body onload='doTest()'>\n"
187             + "</body></html>";
188 
189         final List<String> collectedAlerts = new ArrayList<>();
190         try {
191             loadPage(getWebClient(), html, collectedAlerts);
192         }
193         catch (final ScriptException ex) {
194             return;
195         }
196         fail();
197     }
198 
199     /**
200      * @throws Exception if the test fails
201      */
202     @Test
203     @Alerts({"www.gargoylesoftware.com", "www.gargoylesoftware.com"})
204     public void domain_set_for_about_blank() throws Exception {
205         final String html = DOCTYPE_HTML
206             + "<html><head><title>foo</title><script>\n"
207             + "function doTest() {\n"
208             + "  var domain = document.domain;\n"
209             + "  alert(domain);\n"
210             + "  var frameDoc = frames[0].document;\n"
211             + "  alert(frameDoc.domain);\n"
212             + "  try {\n"
213             + "    frameDoc.domain = domain;\n"
214             + "  } catch(e) { alert('exception'); }\n"
215             + "}\n"
216             + "</script></head>\n"
217             + "<body onload='doTest()'>\n"
218             + "<iframe src='about:blank'></iframe>\n"
219             + "</body></html>";
220 
221         loadPageWithAlerts(html, new URL("http://www.gargoylesoftware.com/"), null);
222     }
223 
224     /**
225      * @throws Exception if the test fails
226      */
227     @Test
228     @Alerts({"one", "two", "three", "four"})
229     public void cookie_read() throws Exception {
230         final WebClient webClient = getWebClientWithMockWebConnection();
231 
232         final String html = DOCTYPE_HTML
233             + "<html><head><title>First</title><script>\n"
234             + "function doTest() {\n"
235             + "  var cookieSet = document.cookie.split('; ');\n"
236             + "  var setSize = cookieSet.length;\n"
237             + "  var crumbs;\n"
238             + "  for (var x = 0; x < setSize; x++) {\n"
239             + "    crumbs = cookieSet[x].split('=');\n"
240             + "    alert(crumbs[0]);\n"
241             + "    alert(crumbs[1]);\n"
242             + "  }\n"
243             + "}\n"
244             + "</script></head><body onload='doTest()'>\n"
245             + "</body></html>";
246 
247         final URL url = URL_FIRST;
248         getMockWebConnection().setResponse(url, html);
249 
250         final CookieManager mgr = webClient.getCookieManager();
251         mgr.addCookie(new Cookie(url.getHost(), "one", "two", "/", null, false));
252         mgr.addCookie(new Cookie(url.getHost(), "three", "four", "/", null, false));
253 
254         final List<String> collectedAlerts = new ArrayList<>();
255         webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
256 
257         final HtmlPage firstPage = webClient.getPage(url);
258         assertEquals("First", firstPage.getTitleText());
259 
260         assertEquals(getExpectedAlerts(), collectedAlerts);
261     }
262 
263     /**
264      * This one can't be tested with WebDriver.
265      * @throws Exception if an error occurs
266      */
267     @Test
268     @Alerts({"false", "", "", ""})
269     public void cookie_write_cookiesDisabled() throws Exception {
270         final String html = DOCTYPE_HTML
271               + "html><head><script>\n"
272               + "  alert(navigator.cookieEnabled);\n"
273               + "  alert(document.cookie);\n"
274               + "  document.cookie = 'foo=bar';\n"
275               + "  alert(document.cookie);\n"
276               + "  document.cookie = 'foo=hello world';\n"
277               + "  alert(document.cookie);\n"
278               + "</script>\n"
279               + "</head>\n"
280               + "<body>abc</body>\n"
281               + "</html>";
282 
283         final WebClient client = getWebClientWithMockWebConnection();
284         client.getCookieManager().setCookiesEnabled(false);
285 
286         loadPage(html);
287     }
288 
289     /**
290      * Verifies that cookies work when working with local files (not remote sites with real domains).
291      * Required for local testing of Dojo 1.1.1.
292      * @throws Exception if an error occurs
293      */
294     @Test
295     @Alerts({"", "", "blah=bleh"})
296     public void cookieInLocalFile() throws Exception {
297         final WebClient client = getWebClient();
298 
299         final List<String> actual = new ArrayList<>();
300         client.setAlertHandler(new CollectingAlertHandler(actual));
301 
302         final URL url = getClass().getResource("HTMLDocumentTest_cookieInLocalFile.html");
303         client.getPage(url);
304 
305         assertEquals(getExpectedAlerts(), actual);
306     }
307 
308 }