browser_net_copy_as_fetch.js (2700B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 /** 7 * Tests if Copy as Fetch works. 8 */ 9 10 add_task(async function () { 11 const { tab, monitor } = await initNetMonitor(HTTPS_CURL_URL, { 12 requestCount: 1, 13 }); 14 info("Starting test... "); 15 16 // GET request, no cookies (first request) 17 await performRequest("GET"); 18 await testClipboardContent(`await fetch("https://example.com/browser/devtools/client/netmonitor/test/sjs_simple-test-server.sjs", { 19 "credentials": "omit", 20 "headers": { 21 "User-Agent": "${navigator.userAgent}", 22 "Accept": "*/*", 23 "Accept-Language": "en-US", 24 "X-Custom-Header-1": "Custom value", 25 "X-Custom-Header-2": "8.8.8.8", 26 "X-Custom-Header-3": "Mon, 3 Mar 2014 11:11:11 GMT", 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 "referrer": "https://example.com/browser/devtools/client/netmonitor/test/html_copy-as-curl.html", 34 "method": "GET", 35 "mode": "cors" 36 });`); 37 38 await teardown(monitor); 39 40 async function performRequest(method, payload) { 41 const waitRequest = waitForNetworkEvents(monitor, 1); 42 await SpecialPowers.spawn( 43 tab.linkedBrowser, 44 [ 45 { 46 url: HTTPS_SIMPLE_SJS, 47 method_: method, 48 payload_: payload, 49 }, 50 ], 51 async function ({ url, method_, payload_ }) { 52 content.wrappedJSObject.performRequest(url, method_, payload_); 53 } 54 ); 55 await waitRequest; 56 } 57 58 async function testClipboardContent(expectedResult) { 59 const { document } = monitor.panelWin; 60 61 const items = document.querySelectorAll(".request-list-item"); 62 EventUtils.sendMouseEvent({ type: "mousedown" }, items[items.length - 1]); 63 EventUtils.sendMouseEvent( 64 { type: "contextmenu" }, 65 document.querySelectorAll(".request-list-item")[0] 66 ); 67 68 /* Ensure that the copy as fetch option is always visible */ 69 is( 70 !!getContextMenuItem(monitor, "request-list-context-copy-as-fetch"), 71 true, 72 'The "Copy as Fetch" context menu item should not be hidden.' 73 ); 74 75 await waitForClipboardPromise( 76 async function setup() { 77 await selectContextMenuItem( 78 monitor, 79 "request-list-context-copy-as-fetch" 80 ); 81 }, 82 function validate(result) { 83 if (typeof result !== "string") { 84 return false; 85 } 86 return expectedResult === result; 87 } 88 ); 89 90 info("Clipboard contains a fetch command for item " + (items.length - 1)); 91 } 92 });