summaryrefslogtreecommitdiffstats
path: root/dom/media/test/complete_length_worker.js
blob: ceda63fdd53fadc483c5734b68250b8831d76f83 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"use strict";

let client;
function is(got, expected, name) {
  client.postMessage({ type: "is", got, expected, name });
}

self.onactivate = e =>
  e.waitUntil(
    (async () => {
      await self.clients.claim();
      const allClients = await self.clients.matchAll();
      client = allClients[0];
      is(allClients.length, 1, "allClients.length");
    })()
  );

let expected_start = 0;
let response_data = [
  // One Array element for each response in order:
  {
    complete_length: "*",
    body: "O",
  },
  {
    complete_length: "3",
    body: "g",
  },
  {
    // Extend length to test that the remainder is fetched.
    complete_length: "6",
    body: "g",
  },
  {
    // Reduce length to test that no more is fetched.
    complete_length: "4",
    body: "S",
  },
];

self.onfetch = e => {
  if (!e.request.url.endsWith("/media-resource")) {
    return; // fall back to network fetch
  }
  is(
    response_data.length >= 1,
    true,
    `response_data.length (${response_data.length}) > 0`
  );
  const { complete_length, body } = response_data.shift();
  const range = e.request.headers.get("Range");
  const match = range.match(/^bytes=(\d+)-/);
  is(Array.isArray(match), true, `Array.isArray(match) for ${range}`);
  const first = parseInt(match[1]);
  is(first, expected_start, "first");
  const last = first + body.length - 1; // inclusive
  expected_start = last + 1;
  const init = {
    status: 206, // Partial Content
    headers: {
      "Accept-Ranges": "bytes",
      "Content-Type": "audio/ogg",
      "Content-Range": `bytes ${first}-${last}/${complete_length}`,
      "Content-Length": body.length,
    },
  };
  e.respondWith(new Response(body, init));
};

self.onmessage = e => {
  switch (e.data.type) {
    case "got error event":
      // Check that all expected requests were received.
      is(response_data.length, 0, "missing fetch count");
      client.postMessage({ type: "done" });
      return;
    default:
      is(e.data.type, "__KNOWN__", "e.data.type");
  }
};