summaryrefslogtreecommitdiffstats
path: root/devtools/shared/network-observer/test/browser/browser_networkobserver_serviceworker.js
blob: 1680dc6005e1b18b6aeeba5073034c55c93cd56d (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Tests that all the expected service worker requests are received
// by the network observer.
add_task(async function testServiceWorkerSuccessRequests() {
  await addTab(URL_ROOT + "doc_network-observer.html");

  const REQUEST_URL =
    URL_ROOT + `sjs_network-observer-test-server.sjs?sts=200&fmt=`;

  const EXPECTED_REQUESTS = [
    // The main service worker script request
    `https://example.com/browser/devtools/shared/network-observer/test/browser/serviceworker.js`,
    // The requests intercepted by the service worker
    REQUEST_URL + "js",
    REQUEST_URL + "css",
    // The request initiated by the service worker
    REQUEST_URL + "json",
  ];

  const onNetworkEvents = waitForNetworkEvents(null, 4);

  info("Register the service worker and send requests...");
  await SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [REQUEST_URL],
    async url => {
      await content.wrappedJSObject.registerServiceWorker();
      content.wrappedJSObject.fetch(url + "js");
      content.wrappedJSObject.fetch(url + "css");
    }
  );
  const events = await onNetworkEvents;

  is(events.length, 4, "Received the expected number of network events");
  for (const { options, channel } of events) {
    info(`Assert the info for the request from ${channel.URI.spec}`);
    ok(
      EXPECTED_REQUESTS.includes(channel.URI.spec),
      `The request for ${channel.URI.spec} is an expected service worker request`
    );
    Assert.notStrictEqual(
      channel.loadInfo.browsingContextID,
      0,
      `The request for ${channel.URI.spec} has a Browsing Context ID of ${channel.loadInfo.browsingContextID}`
    );
    // The main service worker script request is not from the service worker
    if (channel.URI.spec.includes("serviceworker.js")) {
      ok(
        !options.fromServiceWorker,
        `The request for ${channel.URI.spec} is not from the service worker\n`
      );
    } else {
      ok(
        options.fromServiceWorker,
        `The request for ${channel.URI.spec} is from the service worker\n`
      );
    }
  }

  info("Unregistering the service worker...");
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
    await content.wrappedJSObject.unregisterServiceWorker();
  });
});

// Tests that the expected failed service worker request is received by the network observer.
add_task(async function testServiceWorkerFailedRequests() {
  await addTab(URL_ROOT + "doc_network-observer-missing-service-worker.html");

  const REQUEST_URL =
    URL_ROOT + `sjs_network-observer-test-server.sjs?sts=200&fmt=js`;

  const EXPECTED_REQUESTS = [
    // The main service worker script request which should be missing
    "https://example.com/browser/devtools/shared/network-observer/test/browser/serviceworker-missing.js",
    // A notrmal request
    "https://example.com/browser/devtools/shared/network-observer/test/browser/sjs_network-observer-test-server.sjs?sts=200&fmt=js",
  ];

  const onNetworkEvents = waitForNetworkEvents(null, 2);
  await SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [REQUEST_URL],
    async url => {
      await content.wrappedJSObject.registerServiceWorker();
      content.wrappedJSObject.fetch(url);
    }
  );

  const events = await onNetworkEvents;
  is(events.length, 2, "Received the expected number of network events");

  for (const { options, channel } of events) {
    info(`Assert the info for the request from ${channel.URI.spec}`);
    ok(
      EXPECTED_REQUESTS.includes(channel.URI.spec),
      `The request for ${channel.URI.spec} is an expected request`
    );
    Assert.notStrictEqual(
      channel.loadInfo.browsingContextID,
      0,
      `The request for ${channel.URI.spec} has a Browsing Context ID of ${channel.loadInfo.browsingContextID}`
    );
    ok(
      !options.fromServiceWorker,
      `The request for ${channel.URI.spec} is not from the service worker\n`
    );
  }
});