Bug 1892052 - Do not persist custom network requests on PBM. r=devtools-reviewers,bomsy,nchevobbe

Differential Revision: https://phabricator.services.mozilla.com/D207762
This commit is contained in:
cypherpunks1 2024-05-13 10:24:32 +00:00
parent 7abae98680
commit e05e977a80
3 changed files with 162 additions and 77 deletions

View file

@ -122,10 +122,12 @@ class HTTPCustomRequestPanel extends Component {
async componentDidMount() { async componentDidMount() {
let { connector, request } = this.props; let { connector, request } = this.props;
const persistedCustomRequest = await asyncStorage.getItem( if (!connector.currentTarget?.targetForm?.isPrivate) {
"devtools.netmonitor.customRequest" const persistedCustomRequest = await asyncStorage.getItem(
); "devtools.netmonitor.customRequest"
request = request || persistedCustomRequest; );
request = request || persistedCustomRequest;
}
if (!request) { if (!request) {
this.setState({ _isStateDataReady: true }); this.setState({ _isStateDataReady: true });
@ -191,7 +193,9 @@ class HTTPCustomRequestPanel extends Component {
} }
componentWillUnmount() { componentWillUnmount() {
asyncStorage.setItem("devtools.netmonitor.customRequest", this.state); if (!this.props.connector.currentTarget?.targetForm?.isPrivate) {
asyncStorage.setItem("devtools.netmonitor.customRequest", this.state);
}
} }
handleChangeURL(event) { handleChangeURL(event) {

View file

@ -9,30 +9,14 @@ const asyncStorage = require("resource://devtools/shared/async-storage.js");
* Test if content is still persisted after the panel is closed * Test if content is still persisted after the panel is closed
*/ */
add_task(async function () { async function addCustomRequestTestContent(tab, monitor, document) {
// Turn true the pref info("Open the left panel");
await pushPref("devtools.netmonitor.features.newEditAndResend", true); const waitForPanels = waitForDOM(
// Reset the storage for the persisted custom request
await asyncStorage.removeItem("devtools.netmonitor.customRequest");
const { monitor } = await initNetMonitor(HTTPS_CUSTOM_GET_URL, {
requestCount: 1,
});
info("Starting test... ");
const { document, store, windowRequire } = monitor.panelWin;
// Action should be processed synchronously in tests.
const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
store.dispatch(Actions.batchEnable(false));
info("open the left panel");
let waitForPanels = waitForDOM(
document, document,
".monitor-panel .network-action-bar" ".monitor-panel .network-action-bar"
); );
let HTTPCustomRequestButton = document.querySelector( const HTTPCustomRequestButton = document.querySelector(
"#netmonitor-toolbar-container .devtools-http-custom-request-icon" "#netmonitor-toolbar-container .devtools-http-custom-request-icon"
); );
HTTPCustomRequestButton.click(); HTTPCustomRequestButton.click();
@ -59,91 +43,166 @@ add_task(async function () {
"#http-custom-query .map-add-new-inputs .http-custom-input-name" "#http-custom-query .map-add-new-inputs .http-custom-input-name"
); );
newParameterName.focus(); newParameterName.focus();
EventUtils.sendString("My-param"); EventUtils.sendString("My-param", monitor.panelWin);
info("Adding new headers"); info("Adding new headers");
const newHeaderName = document.querySelector( const newHeaderName = document.querySelector(
"#http-custom-headers .map-add-new-inputs .http-custom-input-name" "#http-custom-headers .map-add-new-inputs .http-custom-input-name"
); );
newHeaderName.focus(); newHeaderName.focus();
EventUtils.sendString("My-header"); EventUtils.sendString("My-header", monitor.panelWin);
const newHeaderValue = Array.from( const newHeaderValue = Array.from(
document.querySelectorAll( document.querySelectorAll(
"#http-custom-headers .http-custom-input .http-custom-input-value" "#http-custom-headers .http-custom-input .http-custom-input-value"
) )
).pop(); ).at(-1);
newHeaderValue.focus(); newHeaderValue.focus();
EventUtils.sendString("my-value"); EventUtils.sendString("my-value", monitor.panelWin);
const postValue = document.querySelector("#http-custom-postdata-value"); const postValue = document.querySelector("#http-custom-postdata-value");
postValue.focus(); postValue.focus();
EventUtils.sendString("{'Name': 'Value'}"); EventUtils.sendString("{'Name': 'Value'}", monitor.panelWin);
// Close the panel info("Close the panel");
const closePanel = document.querySelector( const closePanel = document.querySelector(
".network-action-bar .tabs-navigation .sidebar-toggle" ".network-action-bar .tabs-navigation .sidebar-toggle"
); );
closePanel.click(); closePanel.click();
}
// Open the panel again to see if the content is still there async function runTests(tab, monitor, document, isPrivate = false) {
waitForPanels = waitUntil( info("Open the panel again to see if the content is still there");
const waitForPanels = waitFor(
() => () =>
document.querySelector(".http-custom-request-panel") && document.querySelector(".http-custom-request-panel") &&
document.querySelector("#http-custom-request-send-button").disabled === document.querySelector("#http-custom-request-send-button").disabled ===
false isPrivate
); );
HTTPCustomRequestButton = document.querySelector( const HTTPCustomRequestButton = document.querySelector(
"#netmonitor-toolbar-container .devtools-http-custom-request-icon" "#netmonitor-toolbar-container .devtools-http-custom-request-icon"
); );
HTTPCustomRequestButton.click(); HTTPCustomRequestButton.click();
await waitForPanels; await waitForPanels;
is( // Wait a few seconds to make sure all the fields have been updated
methodValue.value, await wait(1500);
"POST",
"The content should still be there after the user close the panel and re-opened"
);
is( const customMethod = document.querySelector("#http-custom-method-value");
url.value, const customUrl = document.querySelector(".http-custom-url-value");
"https://www.example.com?My-param=", const customQuery = document.querySelectorAll(
"The url should still be there after the user close the panel and re-opened" "#http-custom-query .tabpanel-summary-container.http-custom-input textarea"
); );
const customHeaders = document.querySelectorAll(
"#http-custom-headers .tabpanel-summary-container.http-custom-input textarea"
);
const postDataValue = document.querySelector("#http-custom-postdata-value");
const [nameParam] = Array.from( if (isPrivate) {
document.querySelectorAll( is(
"#http-custom-query .tabpanel-summary-container.http-custom-input textarea" customMethod.value,
) "GET",
); "The method should not be persisted after the user close the panel and re-opened in PBM"
is( );
nameParam.value,
"My-param",
"The Parameter name should still be there after the user close the panel and re-opened"
);
const [name, value] = Array.from( is(
document.querySelectorAll( customUrl.value,
"#http-custom-headers .tabpanel-summary-container.http-custom-input textarea" "",
) "The url should not be there after the user close the panel and re-opened in PBM"
); );
is(
name.value,
"My-header",
"The header name should still be there after the user close the panel and re-opened"
);
is(
value.value,
"my-value",
"The header value should still be there after the user close the panel and re-opened"
);
is( is(
postValue.value, customQuery.length,
"{'Name': 'Value'}", 0,
"The content should still be there after the user close the panel and re-opened" "The Parameter should not be there after the user close the panel and re-opened in PBM"
); );
is(
customHeaders.length,
0,
"There should be no custom headers after the user close the panel and re-opened in PBM"
);
is(
postDataValue.value,
"",
"The post data should still be reset after the user close the panel and re-opened in PBM"
);
} else {
is(
customMethod.value,
"POST",
"The method should be persisted after the user close the panel and re-opened"
);
is(
customUrl.value,
"https://www.example.com?My-param=",
"The url should still be there after the user close the panel and re-opened"
);
const [nameParam] = Array.from(customQuery);
is(
nameParam.value,
"My-param",
"The Parameter name should still be there after the user close the panel and re-opened"
);
const [name, value] = Array.from(customHeaders);
is(
name.value,
"My-header",
"The header name should still be there after the user close the panel and re-opened"
);
is(
value.value,
"my-value",
"The header value should still be there after the user close the panel and re-opened"
);
is(
postDataValue.value,
"{'Name': 'Value'}",
"The content should still be there after the user close the panel and re-opened"
);
}
}
add_task(async function testRequestPanelPersistedContent() {
// Turn true the pref
await pushPref("devtools.netmonitor.features.newEditAndResend", true);
// Reset the storage for the persisted custom request
await asyncStorage.removeItem("devtools.netmonitor.customRequest");
const { tab, monitor } = await initNetMonitor(HTTPS_CUSTOM_GET_URL, {
requestCount: 1,
});
const { document, store, windowRequire } = monitor.panelWin;
const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
store.dispatch(Actions.batchEnable(false));
info("Starting test... ");
info("Add initial custom request test content");
await addCustomRequestTestContent(tab, monitor, document);
await runTests(tab, monitor, document);
await teardown(monitor); await teardown(monitor);
}); });
add_task(async function testRequestPanelPersistedContentInPrivateWindow() {
await pushPref("devtools.netmonitor.features.newEditAndResend", true);
const { tab, monitor, privateWindow } = await initNetMonitor(
HTTPS_CUSTOM_GET_URL,
{
requestCount: 1,
openInPrivateWindow: true,
}
);
const { document, store, windowRequire } = monitor.panelWin;
const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
store.dispatch(Actions.batchEnable(false));
info("Starting test in private window... ");
await runTests(tab, monitor, document, true);
await teardown(monitor, privateWindow);
});

View file

@ -321,6 +321,7 @@ function initNetMonitor(
expectedEventTimings, expectedEventTimings,
waitForLoad = true, waitForLoad = true,
enableCache = false, enableCache = false,
openInPrivateWindow = false,
} }
) { ) {
info("Initializing a network monitor pane."); info("Initializing a network monitor pane.");
@ -341,7 +342,22 @@ function initNetMonitor(
], ],
}); });
const tab = await addTab(url, { waitForLoad }); let tab = null;
let privateWindow = null;
if (openInPrivateWindow) {
privateWindow = await BrowserTestUtils.openNewBrowserWindow({
private: true,
});
ok(
PrivateBrowsingUtils.isContentWindowPrivate(privateWindow),
"window is private"
);
tab = BrowserTestUtils.addTab(privateWindow.gBrowser, url);
} else {
tab = await addTab(url, { waitForLoad });
}
info("Net tab added successfully: " + url); info("Net tab added successfully: " + url);
const toolbox = await gDevTools.showToolboxForTab(tab, { const toolbox = await gDevTools.showToolboxForTab(tab, {
@ -371,7 +387,7 @@ function initNetMonitor(
await clearNetworkEvents(monitor); await clearNetworkEvents(monitor);
} }
return { tab, monitor, toolbox }; return { tab, monitor, toolbox, privateWindow };
})(); })();
} }
@ -408,7 +424,7 @@ async function clearNetworkEvents(monitor) {
store.dispatch(Actions.clearRequests()); store.dispatch(Actions.clearRequests());
} }
function teardown(monitor) { function teardown(monitor, privateWindow) {
info("Destroying the specified network monitor."); info("Destroying the specified network monitor.");
return (async function () { return (async function () {
@ -419,6 +435,12 @@ function teardown(monitor) {
await monitor.toolbox.destroy(); await monitor.toolbox.destroy();
await removeTab(tab); await removeTab(tab);
if (privateWindow) {
const closed = BrowserTestUtils.windowClosed(privateWindow);
privateWindow.BrowserCommands.tryToCloseWindow();
await closed;
}
})(); })();
} }