summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/fetch_event_worker.js
blob: b022ca4175ef23b8c2f4f2e791ca279d750a2522 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// eslint-disable-next-line complexity
onfetch = function (ev) {
  if (ev.request.url.includes("ignore")) {
    return;
  }

  if (ev.request.url.includes("bare-synthesized.txt")) {
    ev.respondWith(
      Promise.resolve(new Response("synthesized response body", {}))
    );
  } else if (ev.request.url.includes("file_CrossSiteXHR_server.sjs")) {
    // N.B. this response would break the rules of CORS if it were allowed, but
    //      this test relies upon the preflight request not being intercepted and
    //      thus this response should not be used.
    if (ev.request.method == "OPTIONS") {
      ev.respondWith(
        new Response("", {
          headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Headers": "X-Unsafe",
          },
        })
      );
    } else if (ev.request.url.includes("example.org")) {
      ev.respondWith(fetch(ev.request));
    }
  } else if (ev.request.url.includes("synthesized-404.txt")) {
    ev.respondWith(
      Promise.resolve(
        new Response("synthesized response body", { status: 404 })
      )
    );
  } else if (ev.request.url.includes("synthesized-headers.txt")) {
    ev.respondWith(
      Promise.resolve(
        new Response("synthesized response body", {
          headers: {
            "X-Custom-Greeting": "Hello",
          },
        })
      )
    );
  } else if (ev.request.url.includes("test-respondwith-response.txt")) {
    ev.respondWith(new Response("test-respondwith-response response body", {}));
  } else if (ev.request.url.includes("synthesized-redirect-real-file.txt")) {
    ev.respondWith(Promise.resolve(Response.redirect("fetch/real-file.txt")));
  } else if (
    ev.request.url.includes("synthesized-redirect-twice-real-file.txt")
  ) {
    ev.respondWith(
      Promise.resolve(Response.redirect("synthesized-redirect-real-file.txt"))
    );
  } else if (ev.request.url.includes("synthesized-redirect-synthesized.txt")) {
    ev.respondWith(Promise.resolve(Response.redirect("bare-synthesized.txt")));
  } else if (
    ev.request.url.includes("synthesized-redirect-twice-synthesized.txt")
  ) {
    ev.respondWith(
      Promise.resolve(Response.redirect("synthesized-redirect-synthesized.txt"))
    );
  } else if (ev.request.url.includes("rejected.txt")) {
    ev.respondWith(Promise.reject());
  } else if (ev.request.url.includes("nonresponse.txt")) {
    ev.respondWith(Promise.resolve(5));
  } else if (ev.request.url.includes("nonresponse2.txt")) {
    ev.respondWith(Promise.resolve({}));
  } else if (ev.request.url.includes("nonpromise.txt")) {
    try {
      // This should coerce to Promise(5) instead of throwing
      ev.respondWith(5);
    } catch (e) {
      // test is expecting failure, so return a success if we get a thrown
      // exception
      ev.respondWith(new Response("respondWith(5) threw " + e));
    }
  } else if (ev.request.url.includes("headers.txt")) {
    var ok = true;
    ok &= ev.request.headers.get("X-Test1") == "header1";
    ok &= ev.request.headers.get("X-Test2") == "header2";
    ev.respondWith(Promise.resolve(new Response(ok.toString(), {})));
  } else if (ev.request.url.includes("readable-stream.txt")) {
    ev.respondWith(
      new Response(
        new ReadableStream({
          start(controller) {
            controller.enqueue(
              new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21])
            );
            controller.close();
          },
        })
      )
    );
  } else if (ev.request.url.includes("readable-stream-locked.txt")) {
    let stream = new ReadableStream({
      start(controller) {
        controller.enqueue(
          new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21])
        );
        controller.close();
      },
    });

    ev.respondWith(new Response(stream));

    // This locks the stream.
    stream.getReader();
  } else if (ev.request.url.includes("readable-stream-with-exception.txt")) {
    ev.respondWith(
      new Response(
        new ReadableStream({
          start(controller) {},
          pull() {
            throw "EXCEPTION!";
          },
        })
      )
    );
  } else if (ev.request.url.includes("readable-stream-with-exception2.txt")) {
    ev.respondWith(
      new Response(
        new ReadableStream({
          _controller: null,
          _count: 0,

          start(controller) {
            this._controller = controller;
          },
          pull() {
            if (++this._count == 5) {
              throw "EXCEPTION 2!";
            }
            this._controller.enqueue(new Uint8Array([this._count]));
          },
        })
      )
    );
  } else if (ev.request.url.includes("readable-stream-already-consumed.txt")) {
    let r = new Response(
      new ReadableStream({
        start(controller) {
          controller.enqueue(
            new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21])
          );
          controller.close();
        },
      })
    );

    r.blob();

    ev.respondWith(r);
  } else if (ev.request.url.includes("user-pass")) {
    ev.respondWith(new Response(ev.request.url));
  } else if (ev.request.url.includes("nonexistent_image.gif")) {
    var imageAsBinaryString = atob(
      "R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs"
    );
    var imageLength = imageAsBinaryString.length;

    // If we just pass |imageAsBinaryString| to the Response constructor, an
    // encoding conversion occurs that corrupts the image. Instead, we need to
    // convert it to a typed array.
    // typed array.
    var imageAsArray = new Uint8Array(imageLength);
    for (var i = 0; i < imageLength; ++i) {
      imageAsArray[i] = imageAsBinaryString.charCodeAt(i);
    }

    ev.respondWith(
      Promise.resolve(
        new Response(imageAsArray, { headers: { "Content-Type": "image/gif" } })
      )
    );
  } else if (ev.request.url.includes("nonexistent_script.js")) {
    ev.respondWith(
      Promise.resolve(new Response("check_intercepted_script();", {}))
    );
  } else if (ev.request.url.includes("nonexistent_stylesheet.css")) {
    ev.respondWith(
      Promise.resolve(
        new Response("#style-test { background-color: black !important; }", {
          headers: {
            "Content-Type": "text/css",
          },
        })
      )
    );
  } else if (ev.request.url.includes("nonexistent_page.html")) {
    ev.respondWith(
      Promise.resolve(
        new Response(
          "<script>window.frameElement.test_result = true;</script>",
          {
            headers: {
              "Content-Type": "text/html",
            },
          }
        )
      )
    );
  } else if (ev.request.url.includes("navigate.html")) {
    var requests = [
      // should not throw
      new Request(ev.request),
      new Request(ev.request, undefined),
      new Request(ev.request, null),
      new Request(ev.request, {}),
      new Request(ev.request, { someUnrelatedProperty: 42 }),
      new Request(ev.request, { method: "GET" }),
    ];
    ev.respondWith(
      Promise.resolve(
        new Response(
          "<script>window.frameElement.test_result = true;</script>",
          {
            headers: {
              "Content-Type": "text/html",
            },
          }
        )
      )
    );
  } else if (ev.request.url.includes("nonexistent_worker_script.js")) {
    ev.respondWith(
      Promise.resolve(
        new Response("postMessage('worker-intercept-success')", {
          headers: { "Content-Type": "text/javascript" },
        })
      )
    );
  } else if (ev.request.url.includes("nonexistent_imported_script.js")) {
    ev.respondWith(
      Promise.resolve(
        new Response("check_intercepted_script();", {
          headers: { "Content-Type": "text/javascript" },
        })
      )
    );
  } else if (ev.request.url.includes("deliver-gzip")) {
    // Don't handle the request, this will make Necko perform a network request, at
    // which point SetApplyConversion must be re-enabled, otherwise the request
    // will fail.
    return;
  } else if (ev.request.url.includes("hello.gz")) {
    ev.respondWith(fetch("fetch/deliver-gzip.sjs"));
  } else if (ev.request.url.includes("hello-after-extracting.gz")) {
    ev.respondWith(
      fetch("fetch/deliver-gzip.sjs").then(function (res) {
        return res.text().then(function (body) {
          return new Response(body, {
            status: res.status,
            statusText: res.statusText,
            headers: res.headers,
          });
        });
      })
    );
  } else if (ev.request.url.includes("opaque-on-same-origin")) {
    var url =
      "http://example.com/tests/dom/security/test/cors/file_CrossSiteXHR_server.sjs?status=200";
    ev.respondWith(fetch(url, { mode: "no-cors" }));
  } else if (ev.request.url.includes("opaque-no-cors")) {
    if (ev.request.mode != "no-cors") {
      ev.respondWith(Promise.reject());
      return;
    }

    var url =
      "http://example.com/tests/dom/security/test/cors/file_CrossSiteXHR_server.sjs?status=200";
    ev.respondWith(fetch(url, { mode: ev.request.mode }));
  } else if (ev.request.url.includes("cors-for-no-cors")) {
    if (ev.request.mode != "no-cors") {
      ev.respondWith(Promise.reject());
      return;
    }

    var url =
      "http://example.com/tests/dom/security/test/cors/file_CrossSiteXHR_server.sjs?status=200&allowOrigin=*";
    ev.respondWith(fetch(url));
  } else if (ev.request.url.includes("example.com")) {
    ev.respondWith(fetch(ev.request));
  } else if (ev.request.url.includes("body-")) {
    ev.respondWith(
      ev.request.text().then(function (body) {
        return new Response(body + body);
      })
    );
  } else if (ev.request.url.includes("something.txt")) {
    ev.respondWith(Response.redirect("fetch/somethingelse.txt"));
  } else if (ev.request.url.includes("somethingelse.txt")) {
    ev.respondWith(new Response("something else response body", {}));
  } else if (ev.request.url.includes("redirect_serviceworker.sjs")) {
    // The redirect_serviceworker.sjs server-side JavaScript file redirects to
    // 'http://mochi.test:8888/tests/dom/serviceworkers/test/worker.js'
    // The redirected fetch should not go through the SW since the original
    // fetch was initiated from a SW.
    ev.respondWith(fetch("redirect_serviceworker.sjs"));
  } else if (
    ev.request.url.includes("load_cross_origin_xml_document_synthetic.xml")
  ) {
    ev.respondWith(
      Promise.resolve(
        new Response("<response>body</response>", {
          headers: { "Content-Type": "text/xtml" },
        })
      )
    );
  } else if (
    ev.request.url.includes("load_cross_origin_xml_document_cors.xml")
  ) {
    if (ev.request.mode != "same-origin") {
      ev.respondWith(Promise.reject());
      return;
    }

    var url =
      "http://example.com/tests/dom/security/test/cors/file_CrossSiteXHR_server.sjs?status=200&allowOrigin=*";
    ev.respondWith(fetch(url, { mode: "cors" }));
  } else if (
    ev.request.url.includes("load_cross_origin_xml_document_opaque.xml")
  ) {
    if (ev.request.mode != "same-origin") {
      Promise.resolve(
        new Response("<error>Invalid Request mode</error>", {
          headers: { "Content-Type": "text/xtml" },
        })
      );
      return;
    }

    var url =
      "http://example.com/tests/dom/security/test/cors/file_CrossSiteXHR_server.sjs?status=200";
    ev.respondWith(fetch(url, { mode: "no-cors" }));
  } else if (ev.request.url.includes("xhr-method-test.txt")) {
    ev.respondWith(new Response("intercepted " + ev.request.method));
  } else if (ev.request.url.includes("empty-header")) {
    if (
      !ev.request.headers.has("emptyheader") ||
      ev.request.headers.get("emptyheader") !== ""
    ) {
      ev.respondWith(Promise.reject());
      return;
    }
    ev.respondWith(new Response("emptyheader"));
  } else if (ev.request.url.includes("fetchevent-extendable")) {
    if (ev instanceof ExtendableEvent) {
      ev.respondWith(new Response("extendable"));
    } else {
      ev.respondWith(Promise.reject());
    }
  } else if (ev.request.url.includes("fetchevent-request")) {
    var threw = false;
    try {
      new FetchEvent("foo");
    } catch (e) {
      if (e.name == "TypeError") {
        threw = true;
      }
    } finally {
      ev.respondWith(new Response(threw ? "non-nullable" : "nullable"));
    }
  }
};