browser_net_ws-stacks.js (2749B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 // Test that we get stack traces for the network requests made when creating 7 // web sockets on the main or worker threads. 8 9 const TOP_FILE_NAME = "html_websocket-test-page.html"; 10 const HTTPS_EXAMPLE_URL_WITH_SUB_FOLDER = HTTPS_EXAMPLE_URL + "websockets/"; 11 const TOP_URL = HTTPS_EXAMPLE_URL_WITH_SUB_FOLDER + TOP_FILE_NAME; 12 const WORKER_FILE_NAME = "js_websocket-worker-test.js"; 13 14 const EXPECTED_REQUESTS = { 15 [TOP_URL]: { 16 method: "GET", 17 url: TOP_URL, 18 causeType: "document", 19 causeUri: null, 20 stack: false, 21 }, 22 "ws://localhost:8080/": { 23 method: "GET", 24 url: "ws://localhost:8080/", 25 causeType: "websocket", 26 causeUri: TOP_URL, 27 stack: [ 28 { fn: "openSocket", file: TOP_URL, line: 6 }, 29 { file: TOP_FILE_NAME, line: 3 }, 30 ], 31 }, 32 [HTTPS_EXAMPLE_URL_WITH_SUB_FOLDER + WORKER_FILE_NAME]: { 33 method: "GET", 34 url: HTTPS_EXAMPLE_URL_WITH_SUB_FOLDER + WORKER_FILE_NAME, 35 causeType: "script", 36 causeUri: TOP_URL, 37 stack: [{ file: TOP_URL, line: 9 }], 38 }, 39 "wss://localhost:8081/": { 40 method: "GET", 41 url: "wss://localhost:8081/", 42 causeType: "websocket", 43 causeUri: TOP_URL, 44 stack: [ 45 { 46 fn: "openWorkerSocket", 47 file: HTTPS_EXAMPLE_URL_WITH_SUB_FOLDER + WORKER_FILE_NAME, 48 line: 5, 49 }, 50 { file: WORKER_FILE_NAME, line: 2 }, 51 ], 52 }, 53 }; 54 55 add_task(async function () { 56 // Load a different URL first to instantiate the network monitor before we 57 // load the page we're really interested in. 58 const { monitor } = await initNetMonitor(SIMPLE_URL, { requestCount: 1 }); 59 60 const { store, windowRequire, connector } = monitor.panelWin; 61 const { getSortedRequests } = windowRequire( 62 "devtools/client/netmonitor/src/selectors/index" 63 ); 64 65 const onNetworkEvents = waitForNetworkEvents( 66 monitor, 67 Object.keys(EXPECTED_REQUESTS).length 68 ); 69 await navigateTo(TOP_URL); 70 await onNetworkEvents; 71 72 is( 73 store.getState().requests.requests.length, 74 Object.keys(EXPECTED_REQUESTS).length, 75 "All the page events should be recorded." 76 ); 77 78 const requests = getSortedRequests(store.getState()); 79 // The expected requests in the same order as the 80 // requests. The platform does not guarantee the order so the 81 // tests should not depend on that. 82 const expectedRequests = []; 83 84 // Wait for stack traces from all requests. 85 await Promise.all( 86 requests.map(request => { 87 expectedRequests.push(EXPECTED_REQUESTS[request.url]); 88 return connector.requestData(request.id, "stackTrace"); 89 }) 90 ); 91 92 await validateRequests(expectedRequests, monitor); 93 94 await teardown(monitor); 95 });