summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/test/browser_net_copy_as_powershell.js
blob: 5785b899290ffab71aeac2f399ffdfff3794987c (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";

/**
 * Test the Copy as PowerShell command
 */
add_task(async function () {
  const { tab, monitor } = await initNetMonitor(HTTPS_CURL_URL, {
    requestCount: 1,
  });

  info("Starting test... ");
  info("Test powershell command for GET request without any cookies");
  await performRequest("GET");
  await testClipboardContentForRecentRequest(`Invoke-WebRequest -UseBasicParsing -Uri "https://example.com/browser/devtools/client/netmonitor/test/sjs_simple-test-server.sjs" \`
-UserAgent "${navigator.userAgent}" \`
-Headers @{
"Accept" = "*/*"
  "Accept-Language" = "en-US"
  "Accept-Encoding" = "gzip, deflate, br"
  "X-Custom-Header-1" = "Custom value"
  "X-Custom-Header-2" = "8.8.8.8"
  "X-Custom-Header-3" = "Mon, 3 Mar 2014 11:11:11 GMT"
  "Referer" = "https://example.com/browser/devtools/client/netmonitor/test/html_copy-as-curl.html"
  "Sec-Fetch-Dest" = "empty"
  "Sec-Fetch-Mode" = "cors"
  "Sec-Fetch-Site" = "same-origin"
  "Pragma" = "no-cache"
  "Cache-Control" = "no-cache"
}`);

  info("Test powershell command for GET request with cookies");
  await performRequest("GET");
  await testClipboardContentForRecentRequest(`$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.Cookies.Add((New-Object System.Net.Cookie("bob", "true", "/", "example.com")))
$session.Cookies.Add((New-Object System.Net.Cookie("tom", "cool", "/", "example.com")))
Invoke-WebRequest -UseBasicParsing -Uri "https://example.com/browser/devtools/client/netmonitor/test/sjs_simple-test-server.sjs" \`
-WebSession $session \`
-UserAgent "${navigator.userAgent}" \`
-Headers @{
"Accept" = "*/*"
  "Accept-Language" = "en-US"
  "Accept-Encoding" = "gzip, deflate, br"
  "X-Custom-Header-1" = "Custom value"
  "X-Custom-Header-2" = "8.8.8.8"
  "X-Custom-Header-3" = "Mon, 3 Mar 2014 11:11:11 GMT"
  "Referer" = "https://example.com/browser/devtools/client/netmonitor/test/html_copy-as-curl.html"
  "Sec-Fetch-Dest" = "empty"
  "Sec-Fetch-Mode" = "cors"
  "Sec-Fetch-Site" = "same-origin"
  "Pragma" = "no-cache"
  "Cache-Control" = "no-cache"
}`);

  info("Test powershell command for POST request with post body");
  await performRequest("POST", "Plaintext value as a payload");
  await testClipboardContentForRecentRequest(`$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.Cookies.Add((New-Object System.Net.Cookie("bob", "true", "/", "example.com")))
$session.Cookies.Add((New-Object System.Net.Cookie("tom", "cool", "/", "example.com")))
Invoke-WebRequest -UseBasicParsing -Uri "https://example.com/browser/devtools/client/netmonitor/test/sjs_simple-test-server.sjs" \`
-Method POST \`
-WebSession $session \`
-UserAgent "${navigator.userAgent}" \`
-Headers @{
"Accept" = "*/*"
  "Accept-Language" = "en-US"
  "Accept-Encoding" = "gzip, deflate, br"
  "X-Custom-Header-1" = "Custom value"
  "X-Custom-Header-2" = "8.8.8.8"
  "X-Custom-Header-3" = "Mon, 3 Mar 2014 11:11:11 GMT"
  "Origin" = "https://example.com"
  "Referer" = "https://example.com/browser/devtools/client/netmonitor/test/html_copy-as-curl.html"
  "Sec-Fetch-Dest" = "empty"
  "Sec-Fetch-Mode" = "cors"
  "Sec-Fetch-Site" = "same-origin"
  "Pragma" = "no-cache"
  "Cache-Control" = "no-cache"
} \`
-ContentType "text/plain;charset=UTF-8" \`
-Body "Plaintext value as a payload"`);

  info(
    "Test powershell command for POST request with post body which contains ASCII non printing characters"
  );
  await performRequest("POST", `TAB character included in payload \t`);
  await testClipboardContentForRecentRequest(`$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.Cookies.Add((New-Object System.Net.Cookie("bob", "true", "/", "example.com")))
$session.Cookies.Add((New-Object System.Net.Cookie("tom", "cool", "/", "example.com")))
Invoke-WebRequest -UseBasicParsing -Uri "https://example.com/browser/devtools/client/netmonitor/test/sjs_simple-test-server.sjs" \`
-Method POST \`
-WebSession $session \`
-UserAgent "${navigator.userAgent}" \`
-Headers @{
"Accept" = "*/*"
  "Accept-Language" = "en-US"
  "Accept-Encoding" = "gzip, deflate, br"
  "X-Custom-Header-1" = "Custom value"
  "X-Custom-Header-2" = "8.8.8.8"
  "X-Custom-Header-3" = "Mon, 3 Mar 2014 11:11:11 GMT"
  "Origin" = "https://example.com"
  "Referer" = "https://example.com/browser/devtools/client/netmonitor/test/html_copy-as-curl.html"
  "Sec-Fetch-Dest" = "empty"
  "Sec-Fetch-Mode" = "cors"
  "Sec-Fetch-Site" = "same-origin"
  "Pragma" = "no-cache"
  "Cache-Control" = "no-cache"
} \`
-ContentType "text/plain;charset=UTF-8" \`
-Body ([System.Text.Encoding]::UTF8.GetBytes("TAB character included in payload $([char]9)"))`);

  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 testClipboardContentForRecentRequest(expectedClipboardText) {
    const { document } = monitor.panelWin;

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

    /* Ensure that the copy as fetch option is always visible */
    is(
      !!getContextMenuItem(monitor, "request-list-context-copy-as-powershell"),
      true,
      'The "Copy as PowerShell" context menu item should not be hidden on windows'
    );

    await waitForClipboardPromise(
      async function setup() {
        await selectContextMenuItem(
          monitor,
          "request-list-context-copy-as-powershell"
        );
      },
      function validate(result) {
        if (typeof result !== "string") {
          return false;
        }
        return expectedClipboardText == result;
      }
    );

    info(
      "Clipboard contains a powershell command for item " + (items.length - 1)
    );
  }
});