summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/worker-interception-redirect-webworker.js
blob: b7e6d81b091a9b9b92877f745abbac1ee660704f (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
// This is the (shared or dedicated) worker file for the
// worker-interception-redirect test. It should be served by the corresponding
// .py file instead of being served directly.
//
// This file is served from both resources/*webworker.py,
// resources/scope2/*webworker.py and resources/subdir/*webworker.py.
// Relative paths are used in `fetch()` and `importScripts()` to confirm that
// the correct base URLs are used.

// This greeting text is meant to be injected by the Python script that serves
// this file, to indicate how the script was served (from network or from
// service worker).
//
// We can't just use a sub pipe and name this file .sub.js since we want
// to serve the file from multiple URLs (see above).
let greeting = '%GREETING_TEXT%';
if (!greeting)
  greeting = 'the worker script was served from network';

// Call importScripts() which fills |echo_output| with a string indicating
// whether a service worker intercepted the importScripts() request.
let echo_output;
const import_scripts_msg = encodeURIComponent(
    'importScripts: served from network');
let import_scripts_greeting = 'not set';
try {
  importScripts(`import-scripts-echo.py?msg=${import_scripts_msg}`);
  import_scripts_greeting = echo_output;
} catch(e) {
  import_scripts_greeting = 'importScripts failed';
}

async function runTest(port) {
  port.postMessage(greeting);

  port.postMessage(import_scripts_greeting);

  const response = await fetch('simple.txt');
  const text = await response.text();
  port.postMessage('fetch(): ' + text);

  port.postMessage(self.location.href);
}

if ('DedicatedWorkerGlobalScope' in self &&
    self instanceof DedicatedWorkerGlobalScope) {
  runTest(self);
} else if (
    'SharedWorkerGlobalScope' in self &&
    self instanceof SharedWorkerGlobalScope) {
  self.onconnect = function(e) {
    const port = e.ports[0];
    port.start();
    runTest(port);
  };
}