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.BrowserRunner;
21 import org.htmlunit.junit.annotation.Alerts;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24
25
26
27
28
29
30 @RunWith(BrowserRunner.class)
31 public class PolyfillTest extends SimpleWebTestCase {
32
33
34
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
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 }