summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/test/browser_net_copy_params.js
blob: 0d26d9cd0ce77b9be0d21b97c5d8dfd7d6c2f3d8 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Tests whether copying a request item's parameters works.
 */

add_task(async function () {
  const { tab, monitor } = await initNetMonitor(PARAMS_URL, {
    requestCount: 1,
  });
  info("Starting test... ");

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

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

  // Execute requests.
  await performRequests(monitor, tab, 7);

  await testCopyUrlParamsHidden(0, false);
  await testCopyUrlParams(0, "a");
  await testCopyPostDataHidden(0, false);
  await testCopyPostData(0, '{ "foo": "bar" }');

  await testCopyUrlParamsHidden(1, false);
  await testCopyUrlParams(1, "a=b");
  await testCopyPostDataHidden(1, false);
  await testCopyPostData(1, '{ "foo": "bar" }');

  await testCopyUrlParamsHidden(2, false);
  await testCopyUrlParams(2, "a=b");
  await testCopyPostDataHidden(2, false);
  await testCopyPostData(2, "foo=bar");

  await testCopyUrlParamsHidden(3, false);
  await testCopyUrlParams(3, "a");
  await testCopyPostDataHidden(3, false);
  await testCopyPostData(3, '{ "foo": "bar" }');

  await testCopyUrlParamsHidden(4, false);
  await testCopyUrlParams(4, "a=b");
  await testCopyPostDataHidden(4, false);
  await testCopyPostData(4, '{ "foo": "bar" }');

  await testCopyUrlParamsHidden(5, false);
  await testCopyUrlParams(5, "a=b");
  await testCopyPostDataHidden(5, false);
  await testCopyPostData(5, "?foo=bar");
  testCopyRequestDataLabel(5, "POST");

  await testCopyUrlParamsHidden(6, true);
  await testCopyPostDataHidden(6, true);

  await testCopyPostDataHidden(7, false);
  testCopyRequestDataLabel(7, "PATCH");

  await testCopyPostDataHidden(8, false);
  testCopyRequestDataLabel(8, "PUT");

  return teardown(monitor);

  async function testCopyUrlParamsHidden(index, hidden) {
    EventUtils.sendMouseEvent(
      { type: "mousedown" },
      document.querySelectorAll(".request-list-item")[index]
    );
    EventUtils.sendMouseEvent(
      { type: "contextmenu" },
      document.querySelectorAll(".request-list-item")[index]
    );

    is(
      !!getContextMenuItem(monitor, "request-list-context-copy-url-params"),
      !hidden,
      'The "Copy URL Parameters" context menu item should' +
        (hidden ? " " : " not ") +
        "be hidden."
    );
  }

  async function testCopyUrlParams(index, queryString) {
    EventUtils.sendMouseEvent(
      { type: "mousedown" },
      document.querySelectorAll(".request-list-item")[index]
    );
    EventUtils.sendMouseEvent(
      { type: "contextmenu" },
      document.querySelectorAll(".request-list-item")[index]
    );
    await waitForClipboardPromise(async function setup() {
      await selectContextMenuItem(
        monitor,
        "request-list-context-copy-url-params"
      );
    }, queryString);
    ok(true, "The url query string copied from the selected item is correct.");
  }

  async function testCopyPostDataHidden(index, hidden) {
    EventUtils.sendMouseEvent(
      { type: "mousedown" },
      document.querySelectorAll(".request-list-item")[index]
    );
    EventUtils.sendMouseEvent(
      { type: "contextmenu" },
      document.querySelectorAll(".request-list-item")[index]
    );
    is(
      !!getContextMenuItem(monitor, "request-list-context-copy-post-data"),
      !hidden,
      'The "Copy POST Data" context menu item should' +
        (hidden ? " " : " not ") +
        "be hidden."
    );
  }

  function testCopyRequestDataLabel(index, method) {
    EventUtils.sendMouseEvent(
      { type: "mousedown" },
      document.querySelectorAll(".request-list-item")[index]
    );
    EventUtils.sendMouseEvent(
      { type: "contextmenu" },
      document.querySelectorAll(".request-list-item")[index]
    );
    const copyPostDataNode = getContextMenuItem(
      monitor,
      "request-list-context-copy-post-data"
    );
    is(
      copyPostDataNode.attributes.label.value,
      "Copy " + method + " Data",
      'The "Copy Data" context menu item should have label - Copy ' +
        method +
        " Data"
    );
  }

  async function testCopyPostData(index, postData) {
    // Wait for formDataSections and requestPostData state are ready in redux store
    // since copyPostData API needs to read these state.
    await waitUntil(() => {
      const { requests } = store.getState().requests;
      const { formDataSections, requestPostData } = requests[index];
      return formDataSections && requestPostData;
    });
    EventUtils.sendMouseEvent(
      { type: "mousedown" },
      document.querySelectorAll(".request-list-item")[index]
    );
    EventUtils.sendMouseEvent(
      { type: "contextmenu" },
      document.querySelectorAll(".request-list-item")[index]
    );
    await waitForClipboardPromise(async function setup() {
      await selectContextMenuItem(
        monitor,
        "request-list-context-copy-post-data"
      );
    }, postData);
    ok(true, "The post data string copied from the selected item is correct.");
  }
});