diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /devtools/client/netmonitor/test/browser_net_resend_csp.js | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/client/netmonitor/test/browser_net_resend_csp.js')
-rw-r--r-- | devtools/client/netmonitor/test/browser_net_resend_csp.js | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/devtools/client/netmonitor/test/browser_net_resend_csp.js b/devtools/client/netmonitor/test/browser_net_resend_csp.js new file mode 100644 index 0000000000..bb087470d6 --- /dev/null +++ b/devtools/client/netmonitor/test/browser_net_resend_csp.js @@ -0,0 +1,161 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests if resending an image request uses the same content type + * and hence is not blocked by the CSP of the page. + */ + +add_task(async function () { + if ( + Services.prefs.getBoolPref( + "devtools.netmonitor.features.newEditAndResend", + true + ) + ) { + ok( + true, + "Skip this test when pref is true, because this panel won't be default when that is the case." + ); + return; + } + const { tab, monitor } = await initNetMonitor(CSP_RESEND_URL, { + requestCount: 1, + }); + const { document, store, windowRequire } = monitor.panelWin; + const Actions = windowRequire("devtools/client/netmonitor/src/actions/index"); + store.dispatch(Actions.batchEnable(false)); + + // Executes 1 request + await performRequests(monitor, tab, 1); + + // Select the image request + const imgRequest = document.querySelectorAll(".request-list-item")[0]; + EventUtils.sendMouseEvent({ type: "mousedown" }, imgRequest); + + // Stores original request for comparison of values later + const { getSelectedRequest } = windowRequire( + "devtools/client/netmonitor/src/selectors/index" + ); + const origReq = getSelectedRequest(store.getState()); + + // Context Menu > "Resend" + EventUtils.sendMouseEvent({ type: "contextmenu" }, imgRequest); + + const waitForResentRequest = waitForNetworkEvents(monitor, 1); + await selectContextMenuItem(monitor, "request-list-context-resend-only"); + await waitForResentRequest; + + // Selects request that was resent + const selReq = getSelectedRequest(store.getState()); + + // Finally, some sanity checks + ok(selReq.url.endsWith("test-image.png"), "Correct request selected"); + Assert.strictEqual(origReq.url, selReq.url, "Orig and Sel url match"); + + Assert.strictEqual(selReq.cause.type, "img", "Correct type of selected"); + Assert.strictEqual( + origReq.cause.type, + selReq.cause.type, + "Orig and Sel type match" + ); + + const cspOBJ = await SpecialPowers.spawn(tab.linkedBrowser, [], async () => { + return JSON.parse(content.document.cspJSON); + }); + + const policies = cspOBJ["csp-policies"]; + is(policies.length, 1, "CSP: should be one policy"); + const policy = policies[0]; + is(`${policy["img-src"]}`, "*", "CSP: img-src should be *"); + + await teardown(monitor); +}); + +/** + * Tests if resending an image request uses the same content type + * and hence is not blocked by the CSP of the page. + */ + +add_task(async function () { + if ( + Services.prefs.getBoolPref( + "devtools.netmonitor.features.newEditAndResend", + true + ) + ) { + const { tab, monitor } = await initNetMonitor(CSP_RESEND_URL, { + requestCount: 1, + }); + const { document, store, windowRequire } = monitor.panelWin; + const Actions = windowRequire( + "devtools/client/netmonitor/src/actions/index" + ); + store.dispatch(Actions.batchEnable(false)); + + // Executes 1 request + await performRequests(monitor, tab, 1); + + // Select the image request + const imgRequest = document.querySelectorAll(".request-list-item")[0]; + EventUtils.sendMouseEvent({ type: "mousedown" }, imgRequest); + + // Stores original request for comparison of values later + const { getSelectedRequest } = windowRequire( + "devtools/client/netmonitor/src/selectors/index" + ); + const origReq = getSelectedRequest(store.getState()); + + // Context Menu > "Resend" + EventUtils.sendMouseEvent({ type: "contextmenu" }, imgRequest); + + info("Opening the new request panel"); + const waitForPanels = waitUntil( + () => + document.querySelector(".http-custom-request-panel") && + document.querySelector("#http-custom-request-send-button").disabled === + false + ); + + await selectContextMenuItem(monitor, "request-list-context-edit-resend"); + await waitForPanels; + + const waitForResentRequest = waitForNetworkEvents(monitor, 1); + const buttonSend = document.querySelector( + "#http-custom-request-send-button" + ); + buttonSend.click(); + await waitForResentRequest; + + // Selects request that was resent + const selReq = getSelectedRequest(store.getState()); + + // Finally, some sanity checks + ok(selReq.url.endsWith("test-image.png"), "Correct request selected"); + Assert.strictEqual(origReq.url, selReq.url, "Orig and Sel url match"); + + Assert.strictEqual(selReq.cause.type, "img", "Correct type of selected"); + Assert.strictEqual( + origReq.cause.type, + selReq.cause.type, + "Orig and Sel type match" + ); + + const cspOBJ = await SpecialPowers.spawn( + tab.linkedBrowser, + [], + async () => { + return JSON.parse(content.document.cspJSON); + } + ); + + const policies = cspOBJ["csp-policies"]; + is(policies.length, 1, "CSP: should be one policy"); + const policy = policies[0]; + is(`${policy["img-src"]}`, "*", "CSP: img-src should be *"); + + await teardown(monitor); + } +}); |