summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/fetch-request-css-base-url-worker.js
blob: f3d6a73bdde8ad26e00125c655450ccdc0f0baf8 (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
let source;
let resolveDone;
let done = new Promise(resolve => resolveDone = resolve);

// The page messages this worker to ask for the result. Keep the worker alive
// via waitUntil() until the result is sent.
self.addEventListener('message', event => {
  source = event.data.port;
  source.postMessage('pong');
  event.waitUntil(done);
});

self.addEventListener('fetch', event => {
  const url = new URL(event.request.url);

  // For the CSS file, respond in a way that may change the response URL,
  // depending on |url.search|.
  const cssPath = 'request-url-path/fetch-request-css-base-url-style.css';
  if (url.pathname.indexOf(cssPath) != -1) {
    // Respond with a different URL, deleting "request-url-path/".
    if (url.search == '?fetch') {
      event.respondWith(fetch('fetch-request-css-base-url-style.css?fetch'));
    }
    // Respond with new Response().
    else if (url.search == '?newResponse') {
      const styleString = 'body { background-image: url("./sample.png");}';
      const headers = {'content-type': 'text/css'};
      event.respondWith(new Response(styleString, headers));
    }
  }

  // The image request indicates what the base URL of the CSS was. Message the
  // result back to the test page.
  else if (url.pathname.indexOf('sample.png') != -1) {
    // For some reason |source| is undefined here when running the test manually
    // in Firefox. The test author experimented with both using Client
    // (event.source) and MessagePort to try to get the test to pass, but
    // failed.
    source.postMessage({
      url: event.request.url,
      referrer: event.request.referrer
    });
    resolveDone();
  }
});