summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/test/browser_net_response_CORS_blocked.js
blob: d3dd72e6f705446783834a70af53e9ed98c448b6 (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
114
115
116
117
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Test that requests blocked by CORS have a notification in the response panel
 * and that it is not present when requests are not blocked
 */

add_task(async function testCORSNotificationPresent() {
  info("Test that CORS notification is present");

  const { tab, monitor } = await initNetMonitor(HTTPS_CORS_URL, {
    requestCount: 1,
  });

  const { document, store, windowRequire } = monitor.panelWin;
  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");

  store.dispatch(Actions.batchEnable(false));

  const wait = waitForNetworkEvents(monitor, 1);

  info("making request to a origin that doesn't allow cross origin");
  const requestUrl = HTTPS_EXAMPLE_ORG_URL + "sjs_simple-test-server.sjs";
  await SpecialPowers.spawn(
    tab.linkedBrowser,
    [requestUrl],
    async function (url) {
      content.wrappedJSObject.performRequests(
        url,
        "triggering/preflight",
        "post-data"
      );
    }
  );

  info("Waiting until the requests appear in netmonitor");
  await wait;

  info("selecting first request");
  const firstItem = document.querySelectorAll(".request-list-item")[0];
  EventUtils.sendMouseEvent({ type: "mousedown" }, firstItem);

  const waitForRespPanel = waitForDOM(
    document,
    "#response-panel .notification"
  );

  info("switching to response panel");
  const respPanelButton = document.querySelector("#response-tab");
  EventUtils.sendMouseEvent({ type: "click" }, respPanelButton);
  await waitForRespPanel;

  info("selecting CORS notification");
  const CORSNotification = document.querySelector(
    '#response-panel .notification[data-key="CORS-error"] .messageText'
  );
  ok(CORSNotification, "CORS Notification Present");
  is(
    CORSNotification?.innerText,
    "Response body is not available to scripts (Reason: CORS Missing Allow Origin)",
    "Notification text is correct"
  );

  await teardown(monitor);
});

add_task(async function testCORSNotificationNotPresent() {
  info("Test that CORS notification is not present");
  const { tab, monitor } = await initNetMonitor(CORS_URL, {
    requestCount: 1,
  });

  const { document, store, windowRequire } = monitor.panelWin;
  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");

  store.dispatch(Actions.batchEnable(false));

  const wait = waitForNetworkEvents(monitor, 1);

  info("Making request to a origin that allows cross origin");
  const requestUrl = "https://test1.example.com" + CORS_SJS_PATH;
  await SpecialPowers.spawn(
    tab.linkedBrowser,
    [requestUrl],
    async function (url) {
      content.wrappedJSObject.performRequests(
        url,
        "triggering/preflight",
        "post-data"
      );
    }
  );
  info("waiting for requests to appear in netmonitor");
  await wait;

  info("selecting first request");
  const firstItem = document.querySelectorAll(".request-list-item")[0];
  EventUtils.sendMouseEvent({ type: "mousedown" }, firstItem);

  const waitForRespPanel = waitForDOM(document, "#response-panel");

  info("switching to response panel");
  const respPanelButton = document.querySelector("#response-tab");
  EventUtils.sendMouseEvent({ type: "click" }, respPanelButton);
  await waitForRespPanel;

  info("try to select CORS notification");
  const CORSNotification = document.querySelector(
    '#response-panel .notification[data-key="CORS-error"] .messageText'
  );
  ok(!CORSNotification, "CORS notification not present");

  await teardown(monitor);
});