summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/fetch/index.html
blob: 693810c6fc0db13b4fbd533338cd8e0b91eee67c (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
  <title>Bug 94048 - test install event.</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<div id="style-test" style="background-color: white"></div>
<pre id="test"></pre>
<script class="testbody" type="text/javascript">
  function my_ok(result, msg) {
    window.opener.postMessage({status: "ok", result, message: msg}, "*");
  }

  function check_intercepted_script() {
    document.getElementById('intercepted-script').test_result =
      document.currentScript == document.getElementById('intercepted-script');
  }

  function fetchXHR(name, onload, onerror, headers) {
    gExpected++;

    onload = onload || function() {
      my_ok(false, "load should not complete successfully");
      finish();
    };
    onerror = onerror || function() {
      my_ok(false, "load should be intercepted successfully");
      finish();
    };

    var x = new XMLHttpRequest();
    x.open('GET', name, true);
    x.onload = function() { onload(x) };
    x.onerror = function() { onerror(x) };
    headers = headers || [];
    headers.forEach(function(header) {
      x.setRequestHeader(header[0], header[1]);
    });
    x.send();
  }

  var gExpected = 0;
  var gEncountered = 0;
  function finish() {
    gEncountered++;
    if (gEncountered == gExpected) {
      window.opener.postMessage({status: "done"}, "*");
    }
  }

  function test_onload(creator, complete) {
    gExpected++;
    var elem = creator();
    elem.onload = function() {
      complete.call(elem);
      finish();
    };
    elem.onerror = function() {
      my_ok(false, elem.tagName + " load should complete successfully");
      finish();
    };
    document.body.appendChild(elem);
  }

  function expectAsyncResult() {
    gExpected++;
  }

  my_ok(navigator.serviceWorker.controller != null, "should be controlled");
</script>
<script src="fetch_tests.js"></script>
<script>
  test_onload(function() {
    var elem = document.createElement('img');
    elem.src = "nonexistent_image.gifs";
    elem.id = 'intercepted-img';
    return elem;
  }, function() {
    my_ok(this.complete, "image should be complete");
    my_ok(this.naturalWidth == 1 && this.naturalHeight == 1, "image should be 1x1 gif");
  });

  test_onload(function() {
    var elem = document.createElement('script');
    elem.id = 'intercepted-script';
    elem.src = "nonexistent_script.js";
    return elem;
  }, function() {
    my_ok(this.test_result, "script load should be intercepted");
  });

  test_onload(function() {
    var elem = document.createElement('link');
    elem.href = "nonexistent_stylesheet.css";
    elem.rel = "stylesheet";
    return elem;
  }, function() {
    var styled = document.getElementById('style-test');
    my_ok(window.getComputedStyle(styled).backgroundColor == 'rgb(0, 0, 0)',
          "stylesheet load should be intercepted");
  });

  test_onload(function() {
    var elem = document.createElement('iframe');
    elem.id = 'intercepted-iframe';
    elem.src = "nonexistent_page.html";
    return elem;
  }, function() {
    my_ok(this.test_result, "iframe load should be intercepted");
  });

  test_onload(function() {
    var elem = document.createElement('iframe');
    elem.id = 'intercepted-iframe-2';
    elem.src = "navigate.html";
    return elem;
  }, function() {
    my_ok(this.test_result, "iframe should successfully load");
  });

  gExpected++;
  var xhr = new XMLHttpRequest();
  xhr.addEventListener("load", function(evt) {
    my_ok(evt.target.responseXML === null, "Load synthetic cross origin XML Document should be allowed");
    finish();
  });
  xhr.responseType = "document";
  xhr.open("GET", "load_cross_origin_xml_document_synthetic.xml");
  xhr.send();

  gExpected++;
  fetch(
    "load_cross_origin_xml_document_cors.xml",
    {mode: "same-origin"}
  ).then(function(response) {
    // issue: https://github.com/whatwg/fetch/issues/629
    my_ok(false, "Load CORS cross origin XML Document should not be allowed");
    finish();
  }, function(error) {
    my_ok(true, "Load CORS cross origin XML Document should not be allowed");
    finish();
  });

  gExpected++;
  fetch(
    "load_cross_origin_xml_document_opaque.xml",
    {mode: "same-origin"}
  ).then(function(response) {
    my_ok(false, "Load opaque cross origin XML Document should not be allowed");
    finish();
  }, function(error) {
    my_ok(true, "Load opaque cross origin XML Document should not be allowed");
    finish();
  });

  gExpected++;
  var worker = new Worker('nonexistent_worker_script.js');
  worker.onmessage = function(e) {
    my_ok(e.data == "worker-intercept-success", "worker load intercepted");
    finish();
  };
  worker.onerror = function() {
    my_ok(false, "worker load should be intercepted");
  };

  gExpected++;
  var worker = new Worker('fetch_worker_script.js');
  worker.onmessage = function(e) {
    if (e.data == "finish") {
      finish();
    } else if (e.data == "expect") {
      gExpected++;
    } else if (e.data.type == "ok") {
      my_ok(e.data.value, "Fetch test on worker: " + e.data.msg);
    }
  };
  worker.onerror = function() {
    my_ok(false, "worker should not cause any errors");
  };
</script>
</pre>
</body>
</html>