test: fixup flaky session tests (#44565)
test: fixup broken tests in 32-x-y (#44389)
* test: fixup broken tests in 32-x-y
* test: fixup additional failing test
(cherry picked from commit c61bb1654e
)
Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
This commit is contained in:
parent
bd81baaf04
commit
743fe6f4b4
1 changed files with 58 additions and 103 deletions
|
@ -12,7 +12,7 @@ import * as https from 'node:https';
|
||||||
import * as path from 'node:path';
|
import * as path from 'node:path';
|
||||||
import { setTimeout } from 'node:timers/promises';
|
import { setTimeout } from 'node:timers/promises';
|
||||||
|
|
||||||
import { defer, listen } from './lib/spec-helpers';
|
import { defer, ifit, listen } from './lib/spec-helpers';
|
||||||
import { closeAllWindows } from './lib/window-helpers';
|
import { closeAllWindows } from './lib/window-helpers';
|
||||||
|
|
||||||
describe('session module', () => {
|
describe('session module', () => {
|
||||||
|
@ -841,19 +841,13 @@ describe('session module', () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
describe('session.downloadURL', () => {
|
describe('session.downloadURL', () => {
|
||||||
it('can perform a download', (done) => {
|
it('can perform a download', async () => {
|
||||||
session.defaultSession.once('will-download', function (e, item) {
|
const willDownload = once(session.defaultSession, 'will-download');
|
||||||
item.savePath = downloadFilePath;
|
|
||||||
item.on('done', function (e, state) {
|
|
||||||
try {
|
|
||||||
assertDownload(state, item);
|
|
||||||
done();
|
|
||||||
} catch (e) {
|
|
||||||
done(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
session.defaultSession.downloadURL(`${url}:${port}`);
|
session.defaultSession.downloadURL(`${url}:${port}`);
|
||||||
|
const [, item] = await willDownload;
|
||||||
|
item.savePath = downloadFilePath;
|
||||||
|
const [, state] = await once(item, 'done');
|
||||||
|
assertDownload(state, item);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can perform a download with a valid auth header', async () => {
|
it('can perform a download with a valid auth header', async () => {
|
||||||
|
@ -963,20 +957,14 @@ describe('session module', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('webContents.downloadURL', () => {
|
describe('webContents.downloadURL', () => {
|
||||||
it('can perform a download', (done) => {
|
it('can perform a download', async () => {
|
||||||
const w = new BrowserWindow({ show: false });
|
const w = new BrowserWindow({ show: false });
|
||||||
w.webContents.session.once('will-download', function (e, item) {
|
const willDownload = once(w.webContents.session, 'will-download');
|
||||||
item.savePath = downloadFilePath;
|
|
||||||
item.on('done', function (e, state) {
|
|
||||||
try {
|
|
||||||
assertDownload(state, item);
|
|
||||||
done();
|
|
||||||
} catch (e) {
|
|
||||||
done(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
w.webContents.downloadURL(`${url}:${port}`);
|
w.webContents.downloadURL(`${url}:${port}`);
|
||||||
|
const [, item] = await willDownload;
|
||||||
|
item.savePath = downloadFilePath;
|
||||||
|
const [, state] = await once(item, 'done');
|
||||||
|
assertDownload(state, item);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can perform a download with a valid auth header', async () => {
|
it('can perform a download with a valid auth header', async () => {
|
||||||
|
@ -1079,73 +1067,51 @@ describe('session module', () => {
|
||||||
expect(item.getTotalBytes()).to.equal(0);
|
expect(item.getTotalBytes()).to.equal(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can download from custom protocols', (done) => {
|
it('can download from custom protocols', async () => {
|
||||||
const protocol = session.defaultSession.protocol;
|
const protocol = session.defaultSession.protocol;
|
||||||
const handler = (ignoredError: any, callback: Function) => {
|
const handler = (ignoredError: any, callback: Function) => {
|
||||||
callback({ url: `${url}:${port}` });
|
callback({ url: `${url}:${port}` });
|
||||||
};
|
};
|
||||||
protocol.registerHttpProtocol(protocolName, handler);
|
protocol.registerHttpProtocol(protocolName, handler);
|
||||||
const w = new BrowserWindow({ show: false });
|
const w = new BrowserWindow({ show: false });
|
||||||
w.webContents.session.once('will-download', function (e, item) {
|
const willDownload = once(w.webContents.session, 'will-download');
|
||||||
item.savePath = downloadFilePath;
|
|
||||||
item.on('done', function (e, state) {
|
|
||||||
try {
|
|
||||||
assertDownload(state, item, true);
|
|
||||||
done();
|
|
||||||
} catch (e) {
|
|
||||||
done(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
w.webContents.downloadURL(`${protocolName}://item`);
|
w.webContents.downloadURL(`${protocolName}://item`);
|
||||||
|
const [, item] = await willDownload;
|
||||||
|
item.savePath = downloadFilePath;
|
||||||
|
const [, state] = await once(item, 'done');
|
||||||
|
assertDownload(state, item, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can cancel download', (done) => {
|
it('can cancel download', async () => {
|
||||||
const w = new BrowserWindow({ show: false });
|
const w = new BrowserWindow({ show: false });
|
||||||
w.webContents.session.once('will-download', function (e, item) {
|
const willDownload = once(w.webContents.session, 'will-download');
|
||||||
item.savePath = downloadFilePath;
|
|
||||||
item.on('done', function (e, state) {
|
|
||||||
try {
|
|
||||||
expect(state).to.equal('cancelled');
|
|
||||||
expect(item.getFilename()).to.equal('mock.pdf');
|
|
||||||
expect(item.getMimeType()).to.equal('application/pdf');
|
|
||||||
expect(item.getReceivedBytes()).to.equal(0);
|
|
||||||
expect(item.getTotalBytes()).to.equal(mockPDF.length);
|
|
||||||
expect(item.getContentDisposition()).to.equal(contentDisposition);
|
|
||||||
done();
|
|
||||||
} catch (e) {
|
|
||||||
done(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
item.cancel();
|
|
||||||
});
|
|
||||||
w.webContents.downloadURL(`${url}:${port}/`);
|
w.webContents.downloadURL(`${url}:${port}/`);
|
||||||
|
const [, item] = await willDownload;
|
||||||
|
item.savePath = downloadFilePath;
|
||||||
|
const itemDone = once(item, 'done');
|
||||||
|
item.cancel();
|
||||||
|
const [, state] = await itemDone;
|
||||||
|
expect(state).to.equal('cancelled');
|
||||||
|
expect(item.getFilename()).to.equal('mock.pdf');
|
||||||
|
expect(item.getMimeType()).to.equal('application/pdf');
|
||||||
|
expect(item.getReceivedBytes()).to.equal(0);
|
||||||
|
expect(item.getTotalBytes()).to.equal(mockPDF.length);
|
||||||
|
expect(item.getContentDisposition()).to.equal(contentDisposition);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can generate a default filename', function (done) {
|
ifit(process.platform !== 'win32')('can generate a default filename', async () => {
|
||||||
if (process.env.APPVEYOR === 'True') {
|
|
||||||
// FIXME(alexeykuzmin): Skip the test.
|
|
||||||
// this.skip()
|
|
||||||
return done();
|
|
||||||
}
|
|
||||||
|
|
||||||
const w = new BrowserWindow({ show: false });
|
const w = new BrowserWindow({ show: false });
|
||||||
w.webContents.session.once('will-download', function (e, item) {
|
const willDownload = once(w.webContents.session, 'will-download');
|
||||||
item.savePath = downloadFilePath;
|
|
||||||
item.on('done', function () {
|
|
||||||
try {
|
|
||||||
expect(item.getFilename()).to.equal('download.pdf');
|
|
||||||
done();
|
|
||||||
} catch (e) {
|
|
||||||
done(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
item.cancel();
|
|
||||||
});
|
|
||||||
w.webContents.downloadURL(`${url}:${port}/?testFilename`);
|
w.webContents.downloadURL(`${url}:${port}/?testFilename`);
|
||||||
|
const [, item] = await willDownload;
|
||||||
|
item.savePath = downloadFilePath;
|
||||||
|
const itemDone = once(item, 'done');
|
||||||
|
item.cancel();
|
||||||
|
await itemDone;
|
||||||
|
expect(item.getFilename()).to.equal('download.pdf');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can set options for the save dialog', (done) => {
|
it('can set options for the save dialog', async () => {
|
||||||
const filePath = path.join(__dirname, 'fixtures', 'mock.pdf');
|
const filePath = path.join(__dirname, 'fixtures', 'mock.pdf');
|
||||||
const options = {
|
const options = {
|
||||||
window: null,
|
window: null,
|
||||||
|
@ -1164,40 +1130,29 @@ describe('session module', () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const w = new BrowserWindow({ show: false });
|
const w = new BrowserWindow({ show: false });
|
||||||
w.webContents.session.once('will-download', function (e, item) {
|
const willDownload = once(w.webContents.session, 'will-download');
|
||||||
item.setSavePath(filePath);
|
|
||||||
item.setSaveDialogOptions(options);
|
|
||||||
item.on('done', function () {
|
|
||||||
try {
|
|
||||||
expect(item.getSaveDialogOptions()).to.deep.equal(options);
|
|
||||||
done();
|
|
||||||
} catch (e) {
|
|
||||||
done(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
item.cancel();
|
|
||||||
});
|
|
||||||
w.webContents.downloadURL(`${url}:${port}`);
|
w.webContents.downloadURL(`${url}:${port}`);
|
||||||
|
const [, item] = await willDownload;
|
||||||
|
item.setSavePath(filePath);
|
||||||
|
item.setSaveDialogOptions(options);
|
||||||
|
const itemDone = once(item, 'done');
|
||||||
|
item.cancel();
|
||||||
|
await itemDone;
|
||||||
|
expect(item.getSaveDialogOptions()).to.deep.equal(options);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('when a save path is specified and the URL is unavailable', () => {
|
describe('when a save path is specified and the URL is unavailable', () => {
|
||||||
it('does not display a save dialog and reports the done state as interrupted', (done) => {
|
it('does not display a save dialog and reports the done state as interrupted', async () => {
|
||||||
const w = new BrowserWindow({ show: false });
|
const w = new BrowserWindow({ show: false });
|
||||||
w.webContents.session.once('will-download', function (e, item) {
|
const willDownload = once(w.webContents.session, 'will-download');
|
||||||
item.savePath = downloadFilePath;
|
|
||||||
if (item.getState() === 'interrupted') {
|
|
||||||
item.resume();
|
|
||||||
}
|
|
||||||
item.on('done', function (e, state) {
|
|
||||||
try {
|
|
||||||
expect(state).to.equal('interrupted');
|
|
||||||
done();
|
|
||||||
} catch (e) {
|
|
||||||
done(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
w.webContents.downloadURL(`file://${path.join(__dirname, 'does-not-exist.txt')}`);
|
w.webContents.downloadURL(`file://${path.join(__dirname, 'does-not-exist.txt')}`);
|
||||||
|
const [, item] = await willDownload;
|
||||||
|
item.savePath = downloadFilePath;
|
||||||
|
if (item.getState() === 'interrupted') {
|
||||||
|
item.resume();
|
||||||
|
}
|
||||||
|
const [, state] = await once(item, 'done');
|
||||||
|
expect(state).to.equal('interrupted');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue