browser_net_copy_as_powershell.js (6502B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 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 }); 13 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 }`); 33 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 }`); 56 57 // disable sending idempotency header for POST request 58 await pushPref("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"`); 85 86 info( 87 "Test powershell command for POST request with post body which contains ASCII non printing characters" 88 ); 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)"))`); 114 115 async function performRequest(method, payload) { 116 const waitRequest = waitForNetworkEvents(monitor, 1); 117 await SpecialPowers.spawn( 118 tab.linkedBrowser, 119 [ 120 { 121 url: HTTPS_SIMPLE_SJS, 122 method_: method, 123 payload_: payload, 124 }, 125 ], 126 async function ({ url, method_, payload_ }) { 127 content.wrappedJSObject.performRequest(url, method_, payload_); 128 } 129 ); 130 await waitRequest; 131 } 132 133 async function testClipboardContentForRecentRequest(expectedClipboardText) { 134 const { document } = monitor.panelWin; 135 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] 141 ); 142 143 /* Ensure that the copy as fetch option is always visible */ 144 is( 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' 148 ); 149 150 await waitForClipboardPromise( 151 async function setup() { 152 await selectContextMenuItem( 153 monitor, 154 "request-list-context-copy-as-powershell" 155 ); 156 }, 157 function validate(result) { 158 if (typeof result !== "string") { 159 return false; 160 } 161 return expectedClipboardText == result; 162 } 163 ); 164 165 info( 166 "Clipboard contains a powershell command for item " + (items.length - 1) 167 ); 168 } 169 });