summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/fetch-event-respond-with-custom-response-worker.js
blob: ff24aed1282c8cf69b801cb4841594aef19db613 (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
'use strict';

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

  if (!type) return;

  if (type === 'string') {
    event.respondWith(new Response('PASS'));
  }
  else if (type === 'blob') {
    event.respondWith(
      new Response(new Blob(['PASS']))
    );
  }
  else if (type === 'buffer-view') {
    const encoder = new TextEncoder();
    event.respondWith(
      new Response(encoder.encode('PASS'))
    );
  }
  else if (type === 'buffer') {
    const encoder = new TextEncoder();
    event.respondWith(
      new Response(encoder.encode('PASS').buffer)
    );
  }
  else if (type === 'form-data') {
    const body = new FormData();
    body.set('result', 'PASS');
    event.respondWith(
      new Response(body)
    );
  }
  else if (type === 'search-params') {
    const body = new URLSearchParams();
    body.set('result', 'PASS');
    event.respondWith(
      new Response(body, {
        headers: { 'Content-Type': 'text/plain' }
      })
    );
  }
});