summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/fetch-request-css-cross-origin-worker.js
blob: a71e91216c10893dafb72fd01de822ef29e40e48 (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
importScripts('/common/get-host-info.sub.js');
importScripts('test-helpers.sub.js');

const HOST_INFO = get_host_info();
const REMOTE_ORIGIN = HOST_INFO.HTTPS_REMOTE_ORIGIN;
const BASE_PATH = base_path();
const CSS_FILE = 'fetch-request-css-cross-origin-mime-check-cross.css';
const HTML_FILE = 'fetch-request-css-cross-origin-mime-check-cross.html';

function add_pipe_header(url_str, header) {
  if (url_str.indexOf('?pipe=') == -1) {
    url_str += '?pipe=';
  } else {
    url_str += '|';
  }
  url_str += `header${header}`;
  return url_str;
}

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

    const use_mime =
        (url.searchParams.get('mime') != 'no');
    const mime_header = '(Content-Type, text/css)';

    const use_cors =
        (url.searchParams.has('cors'));
    const cors_header = '(Access-Control-Allow-Origin, *)';

    const file = url.pathname.substring(url.pathname.lastIndexOf('/') + 1);

    // Respond with a cross-origin CSS resource, using CORS if desired.
    if (file == 'cross-origin-css.css') {
      let fetch_url =  REMOTE_ORIGIN + BASE_PATH + CSS_FILE;
      if (use_mime)
        fetch_url = add_pipe_header(fetch_url, mime_header);
      if (use_cors)
        fetch_url = add_pipe_header(fetch_url, cors_header);
      const mode = use_cors ? 'cors' : 'no-cors';
      event.respondWith(fetch(fetch_url, {'mode': mode}));
      return;
    }

    // Respond with a cross-origin CSS resource with an HTML name. This is only
    // used in the MIME sniffing test, so MIME is never added.
    if (file == 'cross-origin-html.css') {
      const fetch_url = REMOTE_ORIGIN + BASE_PATH + HTML_FILE;
      event.respondWith(fetch(fetch_url, {mode: 'no-cors'}));
      return;
    }

    // Respond with synthetic CSS.
    if (file == 'synthetic.css') {
      let headers = {};
      if (use_mime) {
        headers['Content-Type'] = 'text/css';
      }

      event.respondWith(new Response("#synthetic { color: blue; }", {headers}));
      return;
    }

    // Otherwise, fallback to network.
  });