summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_webconsole_network_requests_from_chrome.js
blob: 8cc98024e98625a18322acbaade5dce9ff3aa50a (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Tests that network requests from chrome don't cause the Web Console to
// throw exceptions. See Bug 597136.

"use strict";

const TEST_URI = "http://example.com/";

add_task(async function () {
  // Start a listener on the console service.
  let good = true;
  const listener = {
    QueryInterface: ChromeUtils.generateQI(["nsIObserver"]),
    observe(subject) {
      if (
        subject instanceof Ci.nsIScriptError &&
        subject.category === "XPConnect JavaScript" &&
        subject.sourceName.includes("webconsole")
      ) {
        good = false;
      }
    },
  };
  Services.console.registerListener(listener);

  // trigger a lazy-load of the HUD Service
  BrowserConsoleManager;

  await sendRequestFromChrome();

  ok(
    good,
    "No exception was thrown when sending a network request from a chrome window"
  );

  Services.console.unregisterListener(listener);
});

function sendRequestFromChrome() {
  return new Promise(resolve => {
    const xhr = new XMLHttpRequest();

    xhr.addEventListener(
      "load",
      () => {
        window.setTimeout(resolve, 0);
      },
      { once: true }
    );

    xhr.open("GET", TEST_URI, true);
    xhr.send(null);
  });
}