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