feat: Session#clearData API (#40983)

* WIP: Session.clearBrowsingData API

* impl API method

* clean up

* tidy types and comments

* add docs

* add barebones test

* forgot a `#` :(

* tidy: address review comments

* use format macro for cross-platform build

* add another test

* amend docs to disambiguate

* Rename to `clearData`
This commit is contained in:
Calvin 2024-02-25 17:39:17 -07:00 committed by GitHub
commit 12d7a8ff66
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 106 additions and 0 deletions

View file

@ -1607,4 +1607,36 @@ describe('session module', () => {
await expect(request()).to.be.rejectedWith(/ERR_SSL_VERSION_OR_CIPHER_MISMATCH/);
});
});
describe('ses.clearData()', () => {
afterEach(closeAllWindows);
// NOTE: This API clears more than localStorage, but localStorage is a
// convenient test target for this API
it('clears localstorage data', async () => {
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
await w.loadFile(path.join(fixtures, 'api', 'localstorage.html'));
expect(await w.webContents.executeJavaScript('localStorage.length')).to.be.greaterThan(0);
await w.webContents.session.clearData();
expect(await w.webContents.executeJavaScript('localStorage.length')).to.equal(0);
});
it('clears localstorage data when called twice in parallel', async () => {
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
await w.loadFile(path.join(fixtures, 'api', 'localstorage.html'));
expect(await w.webContents.executeJavaScript('localStorage.length')).to.be.greaterThan(0);
// This first call is not awaited immediately
const clearDataPromise = w.webContents.session.clearData();
await w.webContents.session.clearData();
expect(await w.webContents.executeJavaScript('localStorage.length')).to.equal(0);
// Await the first promise so it doesn't creep into another test
await clearDataPromise;
});
});
});