summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/test/browser_net_copy_as_curl.js
blob: 4cce679fe9e4e7929a4f2b845a91260d4f57f95f (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Tests if Copy as cURL works.
 */

const POST_PAYLOAD = "Plaintext value as a payload";

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

  // Different quote chars are used for Windows and POSIX
  const QUOTE_WIN = '"';
  const QUOTE_POSIX = "'";

  const isWin = Services.appinfo.OS === "WINNT";
  const testData = isWin
    ? [
        {
          menuItemId: "request-list-context-copy-as-curl-win",
          data: buildTestData(QUOTE_WIN),
        },
        {
          menuItemId: "request-list-context-copy-as-curl-posix",
          data: buildTestData(QUOTE_POSIX),
        },
      ]
    : [
        {
          menuItemId: "request-list-context-copy-as-curl",
          data: buildTestData(QUOTE_POSIX),
        },
      ];

  await testForPlatform(tab, monitor, testData);

  await teardown(monitor);
});

function buildTestData(QUOTE) {
  // Quote a string, escape the quotes inside the string
  function quote(str) {
    return QUOTE + str.replace(new RegExp(QUOTE, "g"), `\\${QUOTE}`) + QUOTE;
  }

  // Header param is formatted as -H "Header: value" or -H 'Header: value'
  function header(h) {
    return "-H " + quote(h);
  }

  // Construct the expected command
  const SIMPLE_BASE = ["curl " + quote(HTTPS_SIMPLE_SJS)];
  const SLOW_BASE = ["curl " + quote(HTTPS_SLOW_SJS)];
  const BASE_RESULT = [
    "--compressed",
    header("User-Agent: " + navigator.userAgent),
    header("Accept: */*"),
    header("Accept-Language: " + navigator.language),
    header("X-Custom-Header-1: Custom value"),
    header("X-Custom-Header-2: 8.8.8.8"),
    header("X-Custom-Header-3: Mon, 3 Mar 2014 11:11:11 GMT"),
    header("Referer: " + HTTPS_CURL_URL),
    header("Connection: keep-alive"),
    header("Pragma: no-cache"),
    header("Cache-Control: no-cache"),
    header("Sec-Fetch-Dest: empty"),
    header("Sec-Fetch-Mode: cors"),
    header("Sec-Fetch-Site: same-origin"),
  ];

  const COOKIE_PARTIAL_RESULT = [header("Cookie: bob=true; tom=cool")];

  const POST_PARTIAL_RESULT = [
    "-X",
    "POST",
    "--data-raw " + quote(POST_PAYLOAD),
    header("Content-Type: text/plain;charset=UTF-8"),
  ];
  const ORIGIN_RESULT = [header("Origin: https://example.com")];

  const HEAD_PARTIAL_RESULT = ["-I"];

  return {
    SIMPLE_BASE,
    SLOW_BASE,
    BASE_RESULT,
    COOKIE_PARTIAL_RESULT,
    POST_PAYLOAD,
    POST_PARTIAL_RESULT,
    ORIGIN_RESULT,
    HEAD_PARTIAL_RESULT,
  };
}

async function testForPlatform(tab, monitor, testData) {
  // GET request, no cookies (first request)
  await performRequest("GET");
  for (const test of testData) {
    await testClipboardContent(test.menuItemId, [
      ...test.data.SIMPLE_BASE,
      ...test.data.BASE_RESULT,
    ]);
  }
  // Check to make sure it is still OK after we view the response (bug#1452442)
  await selectIndexAndWaitForSourceEditor(monitor, 0);
  for (const test of testData) {
    await testClipboardContent(test.menuItemId, [
      ...test.data.SIMPLE_BASE,
      ...test.data.BASE_RESULT,
    ]);
  }

  // GET request, cookies set by previous response
  await performRequest("GET");
  for (const test of testData) {
    await testClipboardContent(test.menuItemId, [
      ...test.data.SIMPLE_BASE,
      ...test.data.BASE_RESULT,
      ...test.data.COOKIE_PARTIAL_RESULT,
    ]);
  }

  // Unfinished request (bug#1378464, bug#1420513)
  const waitSlow = waitForNetworkEvents(monitor, 0);
  await SpecialPowers.spawn(
    tab.linkedBrowser,
    [HTTPS_SLOW_SJS],
    async function (url) {
      content.wrappedJSObject.performRequest(url, "GET", null);
    }
  );
  await waitSlow;
  for (const test of testData) {
    await testClipboardContent(test.menuItemId, [
      ...test.data.SLOW_BASE,
      ...test.data.BASE_RESULT,
      ...test.data.COOKIE_PARTIAL_RESULT,
    ]);
  }

  // POST request
  await performRequest("POST", POST_PAYLOAD);
  for (const test of testData) {
    await testClipboardContent(test.menuItemId, [
      ...test.data.SIMPLE_BASE,
      ...test.data.BASE_RESULT,
      ...test.data.COOKIE_PARTIAL_RESULT,
      ...test.data.POST_PARTIAL_RESULT,
      ...test.data.ORIGIN_RESULT,
    ]);
  }

  // HEAD request
  await performRequest("HEAD");
  for (const test of testData) {
    await testClipboardContent(test.menuItemId, [
      ...test.data.SIMPLE_BASE,
      ...test.data.BASE_RESULT,
      ...test.data.COOKIE_PARTIAL_RESULT,
      ...test.data.HEAD_PARTIAL_RESULT,
    ]);
  }

  async function performRequest(method, payload) {
    const waitRequest = waitForNetworkEvents(monitor, 1);
    await SpecialPowers.spawn(
      tab.linkedBrowser,
      [
        {
          url: HTTPS_SIMPLE_SJS,
          method_: method,
          payload_: payload,
        },
      ],
      async function ({ url, method_, payload_ }) {
        content.wrappedJSObject.performRequest(url, method_, payload_);
      }
    );
    await waitRequest;
  }

  async function testClipboardContent(menuItemId, expectedResult) {
    const { document } = monitor.panelWin;

    const items = document.querySelectorAll(".request-list-item");
    const itemIndex = items.length - 1;
    EventUtils.sendMouseEvent({ type: "mousedown" }, items[itemIndex]);
    EventUtils.sendMouseEvent(
      { type: "contextmenu" },
      document.querySelectorAll(".request-list-item")[0]
    );

    /* Ensure that the copy as cURL option is always visible */
    is(
      !!getContextMenuItem(monitor, menuItemId),
      true,
      `The "Copy as cURL" context menu item "${menuItemId}" should not be hidden.`
    );

    await waitForClipboardPromise(
      async function setup() {
        await selectContextMenuItem(monitor, menuItemId);
      },
      function validate(result) {
        if (typeof result !== "string") {
          return false;
        }

        // Different setups may produce the same command, but with the
        // parameters in a different order in the commandline (which is fine).
        // Here we confirm that the commands are the same even in that case.

        // This monster regexp parses the command line into an array of arguments,
        // recognizing quoted args with matching quotes and escaped quotes inside:
        // [ "curl 'url'", "--standalone-arg", "-arg-with-quoted-string 'value\'s'" ]
        const matchRe = /[-A-Za-z1-9]+(?: ([\"'])(?:\\\1|.)*?\1)?/g;

        const actual = result.match(matchRe);
        // Must begin with the same "curl 'URL'" segment
        if (!actual || expectedResult[0] != actual[0]) {
          return false;
        }

        // Must match each of the params in the middle (headers)
        return (
          expectedResult.length === actual.length &&
          expectedResult.some(param => actual.includes(param))
        );
      }
    );

    info(
      `Clipboard contains a cURL command for item ${itemIndex} by "${menuItemId}"`
    );
  }
}