summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/xhr-response-url-worker.js
blob: 906ad5005b59a11305237fc590fdf38407b76379 (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
// Service worker for the xhr-response-url test.

self.addEventListener('fetch', event => {
  const url = new URL(event.request.url);
  const respondWith = url.searchParams.get('respondWith');
  if (!respondWith)
    return;

  if (respondWith == 'fetch') {
    const target = url.searchParams.get('url');
    event.respondWith(fetch(target));
    return;
  }

  if (respondWith == 'string') {
    const headers = {'content-type': 'text/plain'};
    event.respondWith(new Response('hello', {headers}));
    return;
  }

  if (respondWith == 'document') {
    const doc = `
        <!DOCTYPE html>
        <html>
        <title>hi</title>
        <body>hello</body>
        </html>`;
    const headers = {'content-type': 'text/html'};
    event.respondWith(new Response(doc, {headers}));
    return;
  }
});