Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / devtools / client / netmonitor / test / browser_net_copy_as_powershell.js
blobb580372f757cfb13588dfe78b0d080e444de19ca
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
4 "use strict";
6 /**
7 * Test the Copy as PowerShell command
8 */
9 add_task(async function () {
10 const { tab, monitor } = await initNetMonitor(HTTPS_CURL_URL, {
11 requestCount: 1,
12 });
14 info("Starting test... ");
15 info("Test powershell command for GET request without any cookies");
16 await performRequest("GET");
17 await testClipboardContentForRecentRequest(`Invoke-WebRequest -UseBasicParsing -Uri "https://example.com/browser/devtools/client/netmonitor/test/sjs_simple-test-server.sjs" \`
18 -UserAgent "${navigator.userAgent}" \`
19 -Headers @{
20 "Accept" = "*/*"
21 "Accept-Language" = "en-US"
22 "Accept-Encoding" = "gzip, deflate, br, zstd"
23 "X-Custom-Header-1" = "Custom value"
24 "X-Custom-Header-2" = "8.8.8.8"
25 "X-Custom-Header-3" = "Mon, 3 Mar 2014 11:11:11 GMT"
26 "Referer" = "https://example.com/browser/devtools/client/netmonitor/test/html_copy-as-curl.html"
27 "Sec-Fetch-Dest" = "empty"
28 "Sec-Fetch-Mode" = "cors"
29 "Sec-Fetch-Site" = "same-origin"
30 "Pragma" = "no-cache"
31 "Cache-Control" = "no-cache"
32 }`);
34 info("Test powershell command for GET request with cookies");
35 await performRequest("GET");
36 await testClipboardContentForRecentRequest(`$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
37 $session.Cookies.Add((New-Object System.Net.Cookie("bob", "true", "/", "example.com")))
38 $session.Cookies.Add((New-Object System.Net.Cookie("tom", "cool", "/", "example.com")))
39 Invoke-WebRequest -UseBasicParsing -Uri "https://example.com/browser/devtools/client/netmonitor/test/sjs_simple-test-server.sjs" \`
40 -WebSession $session \`
41 -UserAgent "${navigator.userAgent}" \`
42 -Headers @{
43 "Accept" = "*/*"
44 "Accept-Language" = "en-US"
45 "Accept-Encoding" = "gzip, deflate, br, zstd"
46 "X-Custom-Header-1" = "Custom value"
47 "X-Custom-Header-2" = "8.8.8.8"
48 "X-Custom-Header-3" = "Mon, 3 Mar 2014 11:11:11 GMT"
49 "Referer" = "https://example.com/browser/devtools/client/netmonitor/test/html_copy-as-curl.html"
50 "Sec-Fetch-Dest" = "empty"
51 "Sec-Fetch-Mode" = "cors"
52 "Sec-Fetch-Site" = "same-origin"
53 "Pragma" = "no-cache"
54 "Cache-Control" = "no-cache"
55 }`);
57 // disable sending idempotency header for POST request
58 Services.prefs.setBoolPref("network.http.idempotencyKey.enabled", false);
59 info("Test powershell command for POST request with post body");
60 await performRequest("POST", "Plaintext value as a payload");
61 await testClipboardContentForRecentRequest(`$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
62 $session.Cookies.Add((New-Object System.Net.Cookie("bob", "true", "/", "example.com")))
63 $session.Cookies.Add((New-Object System.Net.Cookie("tom", "cool", "/", "example.com")))
64 Invoke-WebRequest -UseBasicParsing -Uri "https://example.com/browser/devtools/client/netmonitor/test/sjs_simple-test-server.sjs" \`
65 -Method POST \`
66 -WebSession $session \`
67 -UserAgent "${navigator.userAgent}" \`
68 -Headers @{
69 "Accept" = "*/*"
70 "Accept-Language" = "en-US"
71 "Accept-Encoding" = "gzip, deflate, br, zstd"
72 "X-Custom-Header-1" = "Custom value"
73 "X-Custom-Header-2" = "8.8.8.8"
74 "X-Custom-Header-3" = "Mon, 3 Mar 2014 11:11:11 GMT"
75 "Origin" = "https://example.com"
76 "Referer" = "https://example.com/browser/devtools/client/netmonitor/test/html_copy-as-curl.html"
77 "Sec-Fetch-Dest" = "empty"
78 "Sec-Fetch-Mode" = "cors"
79 "Sec-Fetch-Site" = "same-origin"
80 "Pragma" = "no-cache"
81 "Cache-Control" = "no-cache"
82 } \`
83 -ContentType "text/plain;charset=UTF-8" \`
84 -Body "Plaintext value as a payload"`);
86 info(
87 "Test powershell command for POST request with post body which contains ASCII non printing characters"
89 await performRequest("POST", `TAB character included in payload \t`);
90 await testClipboardContentForRecentRequest(`$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
91 $session.Cookies.Add((New-Object System.Net.Cookie("bob", "true", "/", "example.com")))
92 $session.Cookies.Add((New-Object System.Net.Cookie("tom", "cool", "/", "example.com")))
93 Invoke-WebRequest -UseBasicParsing -Uri "https://example.com/browser/devtools/client/netmonitor/test/sjs_simple-test-server.sjs" \`
94 -Method POST \`
95 -WebSession $session \`
96 -UserAgent "${navigator.userAgent}" \`
97 -Headers @{
98 "Accept" = "*/*"
99 "Accept-Language" = "en-US"
100 "Accept-Encoding" = "gzip, deflate, br, zstd"
101 "X-Custom-Header-1" = "Custom value"
102 "X-Custom-Header-2" = "8.8.8.8"
103 "X-Custom-Header-3" = "Mon, 3 Mar 2014 11:11:11 GMT"
104 "Origin" = "https://example.com"
105 "Referer" = "https://example.com/browser/devtools/client/netmonitor/test/html_copy-as-curl.html"
106 "Sec-Fetch-Dest" = "empty"
107 "Sec-Fetch-Mode" = "cors"
108 "Sec-Fetch-Site" = "same-origin"
109 "Pragma" = "no-cache"
110 "Cache-Control" = "no-cache"
111 } \`
112 -ContentType "text/plain;charset=UTF-8" \`
113 -Body ([System.Text.Encoding]::UTF8.GetBytes("TAB character included in payload $([char]9)"))`);
115 async function performRequest(method, payload) {
116 const waitRequest = waitForNetworkEvents(monitor, 1);
117 await SpecialPowers.spawn(
118 tab.linkedBrowser,
121 url: HTTPS_SIMPLE_SJS,
122 method_: method,
123 payload_: payload,
126 async function ({ url, method_, payload_ }) {
127 content.wrappedJSObject.performRequest(url, method_, payload_);
130 await waitRequest;
133 async function testClipboardContentForRecentRequest(expectedClipboardText) {
134 const { document } = monitor.panelWin;
136 const items = document.querySelectorAll(".request-list-item");
137 EventUtils.sendMouseEvent({ type: "mousedown" }, items[items.length - 1]);
138 EventUtils.sendMouseEvent(
139 { type: "contextmenu" },
140 document.querySelectorAll(".request-list-item")[0]
143 /* Ensure that the copy as fetch option is always visible */
145 !!getContextMenuItem(monitor, "request-list-context-copy-as-powershell"),
146 true,
147 'The "Copy as PowerShell" context menu item should not be hidden on windows'
150 await waitForClipboardPromise(
151 async function setup() {
152 await selectContextMenuItem(
153 monitor,
154 "request-list-context-copy-as-powershell"
157 function validate(result) {
158 if (typeof result !== "string") {
159 return false;
161 return expectedClipboardText == result;
165 info(
166 "Clipboard contains a powershell command for item " + (items.length - 1)