summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/content-type/script.window.js
blob: 31598957ef281cf91987a9c6b4c3c17768330790 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
promise_test(() => {
  return fetch("resources/script-content-types.json").then(res => res.json()).then(runTests);
}, "Loading JSON…");

self.stringFromExecutedScript = undefined;

function runTests(allTestData) {
  allTestData.forEach(testData => {
    runScriptTest(testData, false);
    if (testData.contentType.length > 1) {
      runScriptTest(testData, true);
    }
  });
}

function runScriptTest(testData, singleHeader) {
  async_test(t => {
    const script = document.createElement("script");
      t.add_cleanup(() => {
      script.remove()
      self.stringFromExecutedScript = undefined;
    });
    script.src = getURL(testData.contentType, singleHeader);
    document.head.appendChild(script);
    if (testData.executes) {
      script.onload = t.step_func_done(() => {
        assert_equals(self.stringFromExecutedScript, testData.encoding === "windows-1252" ? "€" : "€");
      });
      script.onerror = t.unreached_func("onerror");
    } else {
      script.onerror = t.step_func_done();
      script.onload = t.unreached_func("onload");
    }
  }, (singleHeader ? "combined" : "separate") + " " + testData.contentType.join(" "));
}

function getURL(input, singleHeader) {
  // Edge does not support URLSearchParams
  let url = "resources/content-type.py?"
  if (singleHeader) {
    url += "single_header&"
  }
  input.forEach(val => {
    url += "value=" + encodeURIComponent(val) + "&";
  });
  url += "&content=" + encodeURIComponent("self.stringFromExecutedScript = \"€\"");
  return url;
}