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.polyfill;
16  
17  import java.net.URL;
18  
19  import org.htmlunit.SimpleWebTestCase;
20  import org.htmlunit.junit.BrowserRunner;
21  import org.htmlunit.junit.annotation.Alerts;
22  import org.junit.Test;
23  import org.junit.runner.RunWith;
24  
25  /**
26   * Tests for polyfill support.
27   *
28   * @author Ronald Brill
29   */
30  @RunWith(BrowserRunner.class)
31  public class PolyfillTest extends SimpleWebTestCase {
32  
33      /**
34       * @throws Exception if the test fails
35       */
36      @Test
37      @Alerts("Content fetched")
38      public void fetch() throws Exception {
39          final String html = DOCTYPE_HTML
40              + "<html><head>\n"
41              + "<script>\n"
42              + "function test() {\n"
43              + "  if (typeof fetch == 'function') {\n"
44              + "    fetch('fetch.txt')\n"
45              + "      .then(response => response.text())\n"
46              + "      .then(data => alert(data));\n"
47              + "  }\n"
48              + "}\n"
49              + "</script>\n"
50              + "</head>\n"
51              + "<body onload='test()'></body></html>";
52  
53          final URL fetchUrl = new URL(URL_FIRST, "fetch.txt");
54          getMockWebConnection().setResponse(fetchUrl, "Content fetched");
55  
56          getWebClientWithMockWebConnection().getOptions().setFetchPolyfillEnabled(true);
57          loadPageWithAlerts(html, URL_FIRST, DEFAULT_WAIT_TIME);
58      }
59  
60      /**
61       * @throws Exception if the test fails
62       */
63      @Test
64      @Alerts("false")
65      public void fetchPolyfillDisabled() throws Exception {
66          final String html = DOCTYPE_HTML
67              + "<html><head>\n"
68              + "<script>\n"
69              + "  function test() {\n"
70              + "    alert(typeof fetch == 'function');\n"
71              + "  }\n"
72              + "</script>\n"
73              + "</head>\n"
74              + "<body onload='test()'></body></html>";
75  
76          final URL fetchUrl = new URL(URL_FIRST, "fetch.txt");
77          getMockWebConnection().setResponse(fetchUrl, "Content fetched");
78  
79          loadPageWithAlerts(html);
80      }
81  }