browser_net_edit_resend_xhr.js (7150B)
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 editing and resending a XHR request works and the 8 * cloned request retains the same cause type. 9 */ 10 11 add_task(async function () { 12 if ( 13 Services.prefs.getBoolPref( 14 "devtools.netmonitor.features.newEditAndResend", 15 true 16 ) 17 ) { 18 ok( 19 true, 20 "Skip this test when pref is true, because this panel won't be default when that is the case." 21 ); 22 return; 23 } 24 25 const { tab, monitor } = await initNetMonitor(POST_RAW_URL, { 26 requestCount: 1, 27 }); 28 29 const { document, store, windowRequire } = monitor.panelWin; 30 const Actions = windowRequire("devtools/client/netmonitor/src/actions/index"); 31 store.dispatch(Actions.batchEnable(false)); 32 33 // Executes 1 XHR request 34 await performRequests(monitor, tab, 1); 35 36 // Selects 1st XHR request 37 const xhrRequest = document.querySelectorAll(".request-list-item")[0]; 38 EventUtils.sendMouseEvent({ type: "mousedown" }, xhrRequest); 39 40 // Stores original request for comparison of values later 41 const { getSelectedRequest } = windowRequire( 42 "devtools/client/netmonitor/src/selectors/index" 43 ); 44 const original = getSelectedRequest(store.getState()); 45 46 // Context Menu > "Edit & Resend" 47 EventUtils.sendMouseEvent({ type: "contextmenu" }, xhrRequest); 48 await selectContextMenuItem(monitor, "request-list-context-edit-resend"); 49 50 // 1) Wait for "Edit & Resend" panel to appear 51 // 2) Click the "Send" button 52 // 3) Wait till the new request appears in the list 53 await waitUntil(() => document.querySelector(".custom-request-panel")); 54 document.querySelector("#custom-request-send-button").click(); 55 await waitForNetworkEvents(monitor, 1); 56 57 // Selects cloned request 58 const clonedRequest = document.querySelectorAll(".request-list-item")[1]; 59 EventUtils.sendMouseEvent({ type: "mousedown" }, clonedRequest); 60 const cloned = getSelectedRequest(store.getState()); 61 62 // Compares if the requests have the same cause type (XHR) 63 Assert.strictEqual( 64 original.cause.type, 65 cloned.cause.type, 66 "Both requests retain the same cause type" 67 ); 68 69 await teardown(monitor); 70 }); 71 72 /** 73 * Tests if editing and resending a XHR request works and the 74 * new request retains the same cause type. 75 */ 76 77 add_task(async function () { 78 if ( 79 Services.prefs.getBoolPref( 80 "devtools.netmonitor.features.newEditAndResend", 81 true 82 ) 83 ) { 84 const { tab, monitor } = await initNetMonitor(POST_RAW_URL, { 85 requestCount: 1, 86 }); 87 88 const { document, store, windowRequire } = monitor.panelWin; 89 const Actions = windowRequire( 90 "devtools/client/netmonitor/src/actions/index" 91 ); 92 store.dispatch(Actions.batchEnable(false)); 93 94 // Executes 1 XHR request 95 await performRequests(monitor, tab, 1); 96 97 // Selects 1st XHR request 98 const xhrRequest = document.querySelectorAll(".request-list-item")[0]; 99 EventUtils.sendMouseEvent({ type: "mousedown" }, xhrRequest); 100 101 // Stores original request for comparison of values later 102 const { getSelectedRequest } = windowRequire( 103 "devtools/client/netmonitor/src/selectors/index" 104 ); 105 const original = getSelectedRequest(store.getState()); 106 107 // Context Menu > "Edit & Resend" 108 EventUtils.sendMouseEvent({ type: "contextmenu" }, xhrRequest); 109 await selectContextMenuItem(monitor, "request-list-context-edit-resend"); 110 111 // 1) Wait for "Edit & Resend" panel to appear 112 // 2) Wait for the Send button to be enabled (i.e all the data is loaded) 113 // 2) Click the "Send" button 114 // 3) Wait till the new request appears in the list 115 await waitUntil( 116 () => 117 document.querySelector(".http-custom-request-panel") && 118 document.querySelector("#http-custom-request-send-button").disabled === 119 false 120 ); 121 document.querySelector("#http-custom-request-send-button").click(); 122 await waitForNetworkEvents(monitor, 1); 123 124 // Selects new request 125 const newRequest = document.querySelectorAll(".request-list-item")[1]; 126 EventUtils.sendMouseEvent({ type: "mousedown" }, newRequest); 127 const request = getSelectedRequest(store.getState()); 128 129 Assert.strictEqual( 130 original.cause.type, 131 request.cause.type, 132 "Both requests retain the same cause type" 133 ); 134 135 await teardown(monitor); 136 } 137 }); 138 139 /** 140 * Tests that resending a XHR request uses the same security deatils as the 141 * original request. 142 */ 143 144 add_task(async function () { 145 if ( 146 Services.prefs.getBoolPref( 147 "devtools.netmonitor.features.newEditAndResend", 148 true 149 ) 150 ) { 151 const { tab, monitor } = await initNetMonitor(HTTPS_CORS_URL, { 152 requestCount: 1, 153 }); 154 155 const { document, store, windowRequire, connector } = monitor.panelWin; 156 const Actions = windowRequire( 157 "devtools/client/netmonitor/src/actions/index" 158 ); 159 store.dispatch(Actions.batchEnable(false)); 160 161 info("Performing a CORS request"); 162 const requestUrl = "https://example.com" + CORS_SJS_PATH; 163 164 const wait = waitForNetworkEvents(monitor, 1); 165 await SpecialPowers.spawn( 166 tab.linkedBrowser, 167 [requestUrl], 168 async function (url) { 169 content.wrappedJSObject.performRequests(url); 170 } 171 ); 172 173 info("Waiting until the requests appear in netmonitor"); 174 await wait; 175 176 const { getSelectedRequest } = windowRequire( 177 "devtools/client/netmonitor/src/selectors/index" 178 ); 179 180 info("Select XHR request"); 181 const xhrRequest = document.querySelectorAll(".request-list-item")[0]; 182 EventUtils.sendMouseEvent({ type: "mousedown" }, xhrRequest); 183 184 info("Fetch the Headers for the original XHR request"); 185 let originalRequest = getSelectedRequest(store.getState()); 186 await connector.requestData(originalRequest.id, "requestHeaders"); 187 await waitForRequestData(store, ["requestHeaders"]); 188 originalRequest = getSelectedRequest(store.getState()); 189 190 info("Resend the XHR request"); 191 const waitForResentRequest = waitForNetworkEvents(monitor, 1); 192 EventUtils.sendMouseEvent({ type: "contextmenu" }, xhrRequest); 193 await selectContextMenuItem(monitor, "request-list-context-resend-only"); 194 await waitForResentRequest; 195 196 info("Fetch the Headers for the resent XHR request"); 197 let resentRequest = getSelectedRequest(store.getState()); 198 await connector.requestData(resentRequest.id, "requestHeaders"); 199 await waitForRequestData(store, ["requestHeaders"]); 200 resentRequest = getSelectedRequest(store.getState()); 201 202 const originalRequestSecFetchModeHeader = 203 originalRequest.requestHeaders.headers.find( 204 header => header.name == "Sec-Fetch-Mode" 205 ); 206 const resentRequestSecFetchModeHeader = 207 resentRequest.requestHeaders.headers.find( 208 header => header.name == "Sec-Fetch-Mode" 209 ); 210 211 info("Assert the security mode for the original amd resent request"); 212 Assert.strictEqual( 213 originalRequestSecFetchModeHeader.value, 214 resentRequestSecFetchModeHeader.value, 215 "Both requests retain the same security mode" 216 ); 217 218 await teardown(monitor); 219 } 220 });