1
2
3
4
5
6
7
8
9
10
11
12
13
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
25
26
27
28 public class PolyfillTest extends SimpleWebTestCase {
29
30
31
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
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 }