browser_net_resend_hidden_headers.js (2475B)
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 that custom request headers are sent even without clicking on the original request (bug 1583397) 8 */ 9 10 add_task(async function () { 11 const { monitor } = await initNetMonitor(HTTPS_SIMPLE_URL, { 12 requestCount: 1, 13 }); 14 info("Starting test... "); 15 16 const { store, windowRequire, connector } = monitor.panelWin; 17 const Actions = windowRequire("devtools/client/netmonitor/src/actions/index"); 18 19 const { getSortedRequests } = windowRequire( 20 "devtools/client/netmonitor/src/selectors/index" 21 ); 22 23 store.dispatch(Actions.batchEnable(false)); 24 25 const requestUrl = HTTPS_SIMPLE_SJS; 26 const requestHeaders = [ 27 { name: "Accept", value: "application/vnd.example+json" }, 28 ]; 29 30 const originalRequest = waitForNetworkEvents(monitor, 1); 31 connector.networkCommand.sendHTTPRequest({ 32 url: requestUrl, 33 method: "GET", 34 headers: requestHeaders, 35 cause: { 36 loadingDocumentUri: "https://example.com", 37 stacktraceAvailable: true, 38 type: "xhr", 39 }, 40 }); 41 await originalRequest; 42 43 info("Sent original request"); 44 45 const originalItem = getSortedRequests(store.getState())[0]; 46 47 store.dispatch(Actions.cloneRequest(originalItem.id)); 48 49 const clonedRequest = waitForNetworkEvents(monitor, 1); 50 51 store.dispatch(Actions.sendCustomRequest(originalItem.id)); 52 53 await clonedRequest; 54 55 info("Resent request"); 56 57 let clonedItem = getSortedRequests(store.getState())[1]; 58 59 await waitForRequestData(store, ["requestHeaders"], clonedItem.id); 60 61 clonedItem = getSortedRequests(store.getState())[1]; 62 63 for (const { name, value } of clonedItem.requestHeaders.headers) { 64 info(`Request header: ${name}: ${value}`); 65 } 66 67 function hasRequestHeader(name, value) { 68 const { headers } = clonedItem.requestHeaders; 69 return headers.some(h => h.name === name && h.value === value); 70 } 71 72 function hasNotRequestHeader(name) { 73 const { headers } = clonedItem.requestHeaders; 74 return headers.every(h => h.name !== name); 75 } 76 77 for (const { name, value } of requestHeaders) { 78 ok(hasRequestHeader(name, value), `The ${name} header has the right value`); 79 } 80 81 // Check that the Cookie header was not added silently (i.e., that the request is 82 // anonymous. 83 for (const name of ["Cookie"]) { 84 ok(hasNotRequestHeader(name), `The ${name} header is not present`); 85 } 86 87 return teardown(monitor); 88 });