test: move download-related session specs to main runner (#18508)
This commit is contained in:
parent
554ee92b39
commit
ac35f41e8d
4 changed files with 349 additions and 485 deletions
|
@ -1,331 +0,0 @@
|
|||
const chai = require('chai')
|
||||
const http = require('http')
|
||||
const https = require('https')
|
||||
const path = require('path')
|
||||
const fs = require('fs')
|
||||
const send = require('send')
|
||||
const auth = require('basic-auth')
|
||||
const ChildProcess = require('child_process')
|
||||
const { closeWindow } = require('./window-helpers')
|
||||
|
||||
const { ipcRenderer, remote } = require('electron')
|
||||
const { ipcMain, session, BrowserWindow, net } = remote
|
||||
const { expect } = chai
|
||||
|
||||
/* The whole session API doesn't use standard callbacks */
|
||||
/* eslint-disable standard/no-callback-literal */
|
||||
|
||||
describe('session module', () => {
|
||||
const fixtures = path.resolve(__dirname, 'fixtures')
|
||||
let w = null
|
||||
let webview = null
|
||||
const url = 'http://127.0.0.1'
|
||||
|
||||
beforeEach(() => {
|
||||
w = new BrowserWindow({
|
||||
show: false,
|
||||
width: 400,
|
||||
height: 400,
|
||||
webPreferences: {
|
||||
nodeIntegration: true
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
if (webview != null) {
|
||||
if (!document.body.contains(webview)) {
|
||||
document.body.appendChild(webview)
|
||||
}
|
||||
webview.remove()
|
||||
}
|
||||
|
||||
return closeWindow(w).then(() => { w = null })
|
||||
})
|
||||
|
||||
describe('DownloadItem', () => {
|
||||
const mockPDF = Buffer.alloc(1024 * 1024 * 5)
|
||||
const protocolName = 'custom-dl'
|
||||
let contentDisposition = 'inline; filename="mock.pdf"'
|
||||
const downloadFilePath = path.join(fixtures, 'mock.pdf')
|
||||
const downloadServer = http.createServer((req, res) => {
|
||||
if (req.url === '/?testFilename') contentDisposition = 'inline'
|
||||
res.writeHead(200, {
|
||||
'Content-Length': mockPDF.length,
|
||||
'Content-Type': 'application/pdf',
|
||||
'Content-Disposition': contentDisposition
|
||||
})
|
||||
res.end(mockPDF)
|
||||
downloadServer.close()
|
||||
})
|
||||
|
||||
const isPathEqual = (path1, path2) => {
|
||||
return path.relative(path1, path2) === ''
|
||||
}
|
||||
const assertDownload = (event, state, url, mimeType,
|
||||
receivedBytes, totalBytes, disposition,
|
||||
filename, port, savePath, isCustom) => {
|
||||
expect(state).to.equal('completed')
|
||||
expect(filename).to.equal('mock.pdf')
|
||||
expect(path.isAbsolute(savePath)).to.be.true()
|
||||
expect(isPathEqual(savePath, path.join(__dirname, 'fixtures', 'mock.pdf'))).to.be.true()
|
||||
if (isCustom) {
|
||||
expect(url).to.be.equal(`${protocolName}://item`)
|
||||
} else {
|
||||
expect(url).to.be.equal(`http://127.0.0.1:${port}/`)
|
||||
}
|
||||
expect(mimeType).to.equal('application/pdf')
|
||||
expect(receivedBytes).to.equal(mockPDF.length)
|
||||
expect(totalBytes).to.equal(mockPDF.length)
|
||||
expect(disposition).to.equal(contentDisposition)
|
||||
expect(fs.existsSync(downloadFilePath)).to.be.true()
|
||||
fs.unlinkSync(downloadFilePath)
|
||||
}
|
||||
|
||||
it('can download using WebContents.downloadURL', (done) => {
|
||||
downloadServer.listen(0, '127.0.0.1', () => {
|
||||
const port = downloadServer.address().port
|
||||
ipcRenderer.sendSync('set-download-option', false, false)
|
||||
w.webContents.downloadURL(`${url}:${port}`)
|
||||
ipcRenderer.once('download-done', (event, state, url,
|
||||
mimeType, receivedBytes,
|
||||
totalBytes, disposition,
|
||||
filename, savePath) => {
|
||||
assertDownload(event, state, url, mimeType, receivedBytes,
|
||||
totalBytes, disposition, filename, port, savePath)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('can download from custom protocols using WebContents.downloadURL', (done) => {
|
||||
const protocol = session.defaultSession.protocol
|
||||
downloadServer.listen(0, '127.0.0.1', () => {
|
||||
const port = downloadServer.address().port
|
||||
const handler = (ignoredError, callback) => {
|
||||
callback({ url: `${url}:${port}` })
|
||||
}
|
||||
protocol.registerHttpProtocol(protocolName, handler, (error) => {
|
||||
if (error) return done(error)
|
||||
ipcRenderer.sendSync('set-download-option', false, false)
|
||||
w.webContents.downloadURL(`${protocolName}://item`)
|
||||
ipcRenderer.once('download-done', (event, state, url,
|
||||
mimeType, receivedBytes,
|
||||
totalBytes, disposition,
|
||||
filename, savePath) => {
|
||||
assertDownload(event, state, url, mimeType, receivedBytes,
|
||||
totalBytes, disposition, filename, port, savePath,
|
||||
true)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('can download using WebView.downloadURL', (done) => {
|
||||
downloadServer.listen(0, '127.0.0.1', () => {
|
||||
const port = downloadServer.address().port
|
||||
ipcRenderer.sendSync('set-download-option', false, false)
|
||||
webview = new WebView()
|
||||
webview.addEventListener('did-finish-load', () => {
|
||||
webview.downloadURL(`${url}:${port}/`)
|
||||
})
|
||||
webview.src = `file://${fixtures}/api/blank.html`
|
||||
ipcRenderer.once('download-done', (event, state, url,
|
||||
mimeType, receivedBytes,
|
||||
totalBytes, disposition,
|
||||
filename, savePath) => {
|
||||
assertDownload(event, state, url, mimeType, receivedBytes,
|
||||
totalBytes, disposition, filename, port, savePath)
|
||||
document.body.removeChild(webview)
|
||||
done()
|
||||
})
|
||||
document.body.appendChild(webview)
|
||||
})
|
||||
})
|
||||
|
||||
it('can cancel download', (done) => {
|
||||
downloadServer.listen(0, '127.0.0.1', () => {
|
||||
const port = downloadServer.address().port
|
||||
ipcRenderer.sendSync('set-download-option', true, false)
|
||||
w.webContents.downloadURL(`${url}:${port}/`)
|
||||
ipcRenderer.once('download-done', (event, state, url,
|
||||
mimeType, receivedBytes,
|
||||
totalBytes, disposition,
|
||||
filename) => {
|
||||
expect(state).to.equal('cancelled')
|
||||
expect(filename).to.equal('mock.pdf')
|
||||
expect(mimeType).to.equal('application/pdf')
|
||||
expect(receivedBytes).to.equal(0)
|
||||
expect(totalBytes).to.equal(mockPDF.length)
|
||||
expect(disposition).to.equal(contentDisposition)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('can generate a default filename', function (done) {
|
||||
if (process.env.APPVEYOR === 'True') {
|
||||
// FIXME(alexeykuzmin): Skip the test.
|
||||
// this.skip()
|
||||
return done()
|
||||
}
|
||||
|
||||
downloadServer.listen(0, '127.0.0.1', () => {
|
||||
const port = downloadServer.address().port
|
||||
ipcRenderer.sendSync('set-download-option', true, false)
|
||||
w.webContents.downloadURL(`${url}:${port}/?testFilename`)
|
||||
ipcRenderer.once('download-done', (event, state, url,
|
||||
mimeType, receivedBytes,
|
||||
totalBytes, disposition,
|
||||
filename) => {
|
||||
expect(state).to.equal('cancelled')
|
||||
expect(filename).to.equal('download.pdf')
|
||||
expect(mimeType).to.equal('application/pdf')
|
||||
expect(receivedBytes).to.equal(0)
|
||||
expect(totalBytes).to.equal(mockPDF.length)
|
||||
expect(disposition).to.equal(contentDisposition)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('can set options for the save dialog', (done) => {
|
||||
downloadServer.listen(0, '127.0.0.1', () => {
|
||||
const filePath = path.join(__dirname, 'fixtures', 'mock.pdf')
|
||||
const port = downloadServer.address().port
|
||||
const options = {
|
||||
window: null,
|
||||
title: 'title',
|
||||
message: 'message',
|
||||
buttonLabel: 'buttonLabel',
|
||||
nameFieldLabel: 'nameFieldLabel',
|
||||
defaultPath: '/',
|
||||
filters: [{
|
||||
name: '1', extensions: ['.1', '.2']
|
||||
}, {
|
||||
name: '2', extensions: ['.3', '.4', '.5']
|
||||
}],
|
||||
showsTagField: true,
|
||||
securityScopedBookmarks: true
|
||||
}
|
||||
|
||||
ipcRenderer.sendSync('set-download-option', true, false, filePath, options)
|
||||
w.webContents.downloadURL(`${url}:${port}`)
|
||||
ipcRenderer.once('download-done', (event, state, url,
|
||||
mimeType, receivedBytes,
|
||||
totalBytes, disposition,
|
||||
filename, savePath, dialogOptions) => {
|
||||
expect(dialogOptions).to.deep.equal(options)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
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) => {
|
||||
ipcRenderer.sendSync('set-download-option', false, false)
|
||||
ipcRenderer.once('download-done', (event, state) => {
|
||||
expect(state).to.equal('interrupted')
|
||||
done()
|
||||
})
|
||||
w.webContents.downloadURL(`file://${path.join(__dirname, 'does-not-exist.txt')}`)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('ses.createInterruptedDownload(options)', () => {
|
||||
it('can create an interrupted download item', (done) => {
|
||||
ipcRenderer.sendSync('set-download-option', true, false)
|
||||
const filePath = path.join(__dirname, 'fixtures', 'mock.pdf')
|
||||
const options = {
|
||||
path: filePath,
|
||||
urlChain: ['http://127.0.0.1/'],
|
||||
mimeType: 'application/pdf',
|
||||
offset: 0,
|
||||
length: 5242880
|
||||
}
|
||||
w.webContents.session.createInterruptedDownload(options)
|
||||
ipcRenderer.once('download-created', (event, state, urlChain,
|
||||
mimeType, receivedBytes,
|
||||
totalBytes, filename,
|
||||
savePath) => {
|
||||
expect(state).to.equal('interrupted')
|
||||
expect(urlChain).to.deep.equal(['http://127.0.0.1/'])
|
||||
expect(mimeType).to.equal('application/pdf')
|
||||
expect(receivedBytes).to.equal(0)
|
||||
expect(totalBytes).to.equal(5242880)
|
||||
expect(savePath).to.equal(filePath)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('can be resumed', (done) => {
|
||||
const fixtures = path.join(__dirname, 'fixtures')
|
||||
const downloadFilePath = path.join(fixtures, 'logo.png')
|
||||
const rangeServer = http.createServer((req, res) => {
|
||||
const options = { root: fixtures }
|
||||
send(req, req.url, options)
|
||||
.on('error', (error) => { done(error) }).pipe(res)
|
||||
})
|
||||
ipcRenderer.sendSync('set-download-option', true, false, downloadFilePath)
|
||||
rangeServer.listen(0, '127.0.0.1', () => {
|
||||
const port = rangeServer.address().port
|
||||
const downloadUrl = `http://127.0.0.1:${port}/assets/logo.png`
|
||||
const callback = (event, state, url, mimeType,
|
||||
receivedBytes, totalBytes, disposition,
|
||||
filename, savePath, dialogOptions, urlChain,
|
||||
lastModifiedTime, eTag) => {
|
||||
if (state === 'cancelled') {
|
||||
const options = {
|
||||
path: savePath,
|
||||
urlChain: urlChain,
|
||||
mimeType: mimeType,
|
||||
offset: receivedBytes,
|
||||
length: totalBytes,
|
||||
lastModified: lastModifiedTime,
|
||||
eTag: eTag
|
||||
}
|
||||
ipcRenderer.sendSync('set-download-option', false, false, downloadFilePath)
|
||||
w.webContents.session.createInterruptedDownload(options)
|
||||
} else {
|
||||
expect(state).to.equal('completed')
|
||||
expect(filename).to.equal('logo.png')
|
||||
expect(savePath).to.equal(downloadFilePath)
|
||||
expect(url).to.equal(downloadUrl)
|
||||
expect(mimeType).to.equal('image/png')
|
||||
expect(receivedBytes).to.equal(14022)
|
||||
expect(totalBytes).to.equal(14022)
|
||||
expect(fs.existsSync(downloadFilePath)).to.be.true()
|
||||
fs.unlinkSync(downloadFilePath)
|
||||
rangeServer.close()
|
||||
ipcRenderer.removeListener('download-done', callback)
|
||||
done()
|
||||
}
|
||||
}
|
||||
ipcRenderer.on('download-done', callback)
|
||||
w.webContents.downloadURL(downloadUrl)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('ses.setPermissionRequestHandler(handler)', () => {
|
||||
it('cancels any pending requests when cleared', (done) => {
|
||||
const ses = session.fromPartition('permissionTest')
|
||||
ses.setPermissionRequestHandler(() => {
|
||||
ses.setPermissionRequestHandler(null)
|
||||
})
|
||||
|
||||
webview = new WebView()
|
||||
webview.addEventListener('ipc-message', (e) => {
|
||||
expect(e.channel).to.equal('message')
|
||||
expect(e.args).to.deep.equal(['SecurityError'])
|
||||
done()
|
||||
})
|
||||
webview.src = `file://${fixtures}/pages/permissions/midi-sysex.html`
|
||||
webview.partition = 'permissionTest'
|
||||
webview.setAttribute('nodeintegration', 'on')
|
||||
document.body.appendChild(webview)
|
||||
})
|
||||
})
|
||||
})
|
|
@ -1278,6 +1278,11 @@ describe('chromium feature', () => {
|
|||
|
||||
it('should download a pdf when plugins are disabled', (done) => {
|
||||
this.createBrowserWindow({ plugins: false, preload: 'preload-pdf-loaded.js' })
|
||||
// NOTE(nornagon): this test has been skipped for ages, so there's no way
|
||||
// to refactor it confidently. The 'set-download-option' ipc was removed
|
||||
// around May 2019, so if you're working on the pdf viewer and arrive at
|
||||
// this test and want to know what 'set-download-option' did, look here:
|
||||
// https://github.com/electron/electron/blob/d87b3ead760ae2d20f2401a8dac4ce548f8cd5f5/spec/static/main.js#L164
|
||||
ipcRenderer.sendSync('set-download-option', false, false)
|
||||
ipcRenderer.once('download-done', (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename) => {
|
||||
expect(state).to.equal('completed')
|
||||
|
|
|
@ -158,58 +158,6 @@ app.on('ready', function () {
|
|||
process.exit(1)
|
||||
})
|
||||
|
||||
// For session's download test, listen 'will-download' event in browser, and
|
||||
// reply the result to renderer for verifying
|
||||
const downloadFilePath = path.join(__dirname, '..', 'fixtures', 'mock.pdf')
|
||||
ipcMain.on('set-download-option', function (event, needCancel, preventDefault, filePath = downloadFilePath, dialogOptions = {}) {
|
||||
window.webContents.session.once('will-download', function (e, item) {
|
||||
window.webContents.send('download-created',
|
||||
item.getState(),
|
||||
item.getURLChain(),
|
||||
item.getMimeType(),
|
||||
item.getReceivedBytes(),
|
||||
item.getTotalBytes(),
|
||||
item.getFilename(),
|
||||
item.getSavePath())
|
||||
if (preventDefault) {
|
||||
e.preventDefault()
|
||||
const url = item.getURL()
|
||||
const filename = item.getFilename()
|
||||
setImmediate(function () {
|
||||
try {
|
||||
item.getURL()
|
||||
} catch (err) {
|
||||
window.webContents.send('download-error', url, filename, err.message)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
if (item.getState() === 'interrupted' && !needCancel) {
|
||||
item.resume()
|
||||
} else {
|
||||
item.setSavePath(filePath)
|
||||
item.setSaveDialogOptions(dialogOptions)
|
||||
}
|
||||
item.on('done', function (e, state) {
|
||||
window.webContents.send('download-done',
|
||||
state,
|
||||
item.getURL(),
|
||||
item.getMimeType(),
|
||||
item.getReceivedBytes(),
|
||||
item.getTotalBytes(),
|
||||
item.getContentDisposition(),
|
||||
item.getFilename(),
|
||||
item.getSavePath(),
|
||||
item.getSaveDialogOptions(),
|
||||
item.getURLChain(),
|
||||
item.getLastModifiedTime(),
|
||||
item.getETag())
|
||||
})
|
||||
if (needCancel) item.cancel()
|
||||
}
|
||||
})
|
||||
event.returnValue = 'done'
|
||||
})
|
||||
|
||||
ipcMain.on('prevent-next-input-event', (event, key, id) => {
|
||||
webContents.fromId(id).once('before-input-event', (event, input) => {
|
||||
if (key === input.key) event.preventDefault()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue