2018-11-08 14:51:06 +00:00
|
|
|
const chai = require('chai')
|
2016-03-25 20:03:49 +00:00
|
|
|
const http = require('http')
|
2016-10-24 07:12:49 +00:00
|
|
|
const https = require('https')
|
2016-03-25 20:03:49 +00:00
|
|
|
const path = require('path')
|
|
|
|
const fs = require('fs')
|
2016-11-24 15:16:39 +00:00
|
|
|
const send = require('send')
|
2016-11-30 20:02:17 +00:00
|
|
|
const auth = require('basic-auth')
|
2018-11-29 18:51:13 +00:00
|
|
|
const ChildProcess = require('child_process')
|
2018-09-13 16:10:51 +00:00
|
|
|
const { closeWindow } = require('./window-helpers')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { ipcRenderer, remote } = require('electron')
|
|
|
|
const { ipcMain, session, BrowserWindow, net } = remote
|
2018-11-08 14:51:06 +00:00
|
|
|
const { expect } = chai
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2017-11-23 22:22:43 +00:00
|
|
|
/* The whole session API doesn't use standard callbacks */
|
|
|
|
/* eslint-disable standard/no-callback-literal */
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('session module', () => {
|
2019-05-28 21:12:59 +00:00
|
|
|
const fixtures = path.resolve(__dirname, 'fixtures')
|
2017-10-27 20:45:58 +00:00
|
|
|
let w = null
|
|
|
|
let webview = null
|
|
|
|
const url = 'http://127.0.0.1'
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
beforeEach(() => {
|
2016-02-17 01:39:11 +00:00
|
|
|
w = new BrowserWindow({
|
2016-01-12 02:40:23 +00:00
|
|
|
show: false,
|
|
|
|
width: 400,
|
2019-01-07 19:19:27 +00:00
|
|
|
height: 400,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true
|
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
afterEach(() => {
|
2016-12-20 00:37:47 +00:00
|
|
|
if (webview != null) {
|
|
|
|
if (!document.body.contains(webview)) {
|
|
|
|
document.body.appendChild(webview)
|
|
|
|
}
|
|
|
|
webview.remove()
|
2016-12-19 23:25:56 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
return closeWindow(w).then(() => { w = null })
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-01-13 09:19:53 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('DownloadItem', () => {
|
2017-11-23 22:22:31 +00:00
|
|
|
const mockPDF = Buffer.alloc(1024 * 1024 * 5)
|
2018-02-15 19:39:52 +00:00
|
|
|
const protocolName = 'custom-dl'
|
2017-10-27 20:45:58 +00:00
|
|
|
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'
|
2016-01-12 02:40:23 +00:00
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Length': mockPDF.length,
|
|
|
|
'Content-Type': 'application/pdf',
|
|
|
|
'Content-Disposition': contentDisposition
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
res.end(mockPDF)
|
|
|
|
downloadServer.close()
|
|
|
|
})
|
2018-07-10 11:38:11 +00:00
|
|
|
|
|
|
|
const isPathEqual = (path1, path2) => {
|
|
|
|
return path.relative(path1, path2) === ''
|
|
|
|
}
|
2017-10-27 20:45:58 +00:00
|
|
|
const assertDownload = (event, state, url, mimeType,
|
2018-09-13 16:10:51 +00:00
|
|
|
receivedBytes, totalBytes, disposition,
|
|
|
|
filename, port, savePath, isCustom) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
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()
|
2018-02-15 19:39:52 +00:00
|
|
|
if (isCustom) {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(url).to.be.equal(`${protocolName}://item`)
|
2018-02-15 19:39:52 +00:00
|
|
|
} else {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(url).to.be.equal(`http://127.0.0.1:${port}/`)
|
2018-02-15 19:39:52 +00:00
|
|
|
}
|
2019-05-20 17:04:18 +00:00
|
|
|
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()
|
2016-03-25 20:03:49 +00:00
|
|
|
fs.unlinkSync(downloadFilePath)
|
|
|
|
}
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can download using WebContents.downloadURL', (done) => {
|
|
|
|
downloadServer.listen(0, '127.0.0.1', () => {
|
|
|
|
const port = downloadServer.address().port
|
2016-03-25 20:03:49 +00:00
|
|
|
ipcRenderer.sendSync('set-download-option', false, false)
|
2017-10-27 20:45:58 +00:00
|
|
|
w.webContents.downloadURL(`${url}:${port}`)
|
|
|
|
ipcRenderer.once('download-done', (event, state, url,
|
2018-09-13 16:10:51 +00:00
|
|
|
mimeType, receivedBytes,
|
|
|
|
totalBytes, disposition,
|
|
|
|
filename, savePath) => {
|
2016-11-24 15:16:39 +00:00
|
|
|
assertDownload(event, state, url, mimeType, receivedBytes,
|
2018-09-13 16:10:51 +00:00
|
|
|
totalBytes, disposition, filename, port, savePath)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2018-02-15 19:39:52 +00:00
|
|
|
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) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
callback({ url: `${url}:${port}` })
|
2018-02-15 19:39:52 +00:00
|
|
|
}
|
|
|
|
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,
|
2018-09-13 16:10:51 +00:00
|
|
|
mimeType, receivedBytes,
|
|
|
|
totalBytes, disposition,
|
|
|
|
filename, savePath) => {
|
2018-02-15 19:39:52 +00:00
|
|
|
assertDownload(event, state, url, mimeType, receivedBytes,
|
2018-09-13 16:10:51 +00:00
|
|
|
totalBytes, disposition, filename, port, savePath,
|
|
|
|
true)
|
2018-02-15 19:39:52 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can download using WebView.downloadURL', (done) => {
|
|
|
|
downloadServer.listen(0, '127.0.0.1', () => {
|
|
|
|
const port = downloadServer.address().port
|
2016-03-25 20:03:49 +00:00
|
|
|
ipcRenderer.sendSync('set-download-option', false, false)
|
2016-12-19 23:25:56 +00:00
|
|
|
webview = new WebView()
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.addEventListener('did-finish-load', () => {
|
|
|
|
webview.downloadURL(`${url}:${port}/`)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-12-19 06:44:03 +00:00
|
|
|
webview.src = `file://${fixtures}/api/blank.html`
|
2017-10-27 20:45:58 +00:00
|
|
|
ipcRenderer.once('download-done', (event, state, url,
|
2018-09-13 16:10:51 +00:00
|
|
|
mimeType, receivedBytes,
|
|
|
|
totalBytes, disposition,
|
|
|
|
filename, savePath) => {
|
2016-11-24 15:16:39 +00:00
|
|
|
assertDownload(event, state, url, mimeType, receivedBytes,
|
2018-09-13 16:10:51 +00:00
|
|
|
totalBytes, disposition, filename, port, savePath)
|
2016-03-25 20:03:49 +00:00
|
|
|
document.body.removeChild(webview)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
document.body.appendChild(webview)
|
|
|
|
})
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can cancel download', (done) => {
|
|
|
|
downloadServer.listen(0, '127.0.0.1', () => {
|
|
|
|
const port = downloadServer.address().port
|
2016-03-25 20:03:49 +00:00
|
|
|
ipcRenderer.sendSync('set-download-option', true, false)
|
2017-10-27 20:45:58 +00:00
|
|
|
w.webContents.downloadURL(`${url}:${port}/`)
|
|
|
|
ipcRenderer.once('download-done', (event, state, url,
|
2018-09-13 16:10:51 +00:00
|
|
|
mimeType, receivedBytes,
|
|
|
|
totalBytes, disposition,
|
|
|
|
filename) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
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)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-08-25 21:04:26 +00:00
|
|
|
|
2017-11-15 21:05:46 +00:00
|
|
|
it('can generate a default filename', function (done) {
|
|
|
|
if (process.env.APPVEYOR === 'True') {
|
|
|
|
// FIXME(alexeykuzmin): Skip the test.
|
|
|
|
// this.skip()
|
|
|
|
return done()
|
|
|
|
}
|
2016-09-19 08:11:42 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
downloadServer.listen(0, '127.0.0.1', () => {
|
|
|
|
const port = downloadServer.address().port
|
2016-09-10 12:13:27 +00:00
|
|
|
ipcRenderer.sendSync('set-download-option', true, false)
|
2017-10-27 20:45:58 +00:00
|
|
|
w.webContents.downloadURL(`${url}:${port}/?testFilename`)
|
|
|
|
ipcRenderer.once('download-done', (event, state, url,
|
2018-09-13 16:10:51 +00:00
|
|
|
mimeType, receivedBytes,
|
|
|
|
totalBytes, disposition,
|
|
|
|
filename) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
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)
|
2016-09-10 12:13:27 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-11-08 14:51:06 +00:00
|
|
|
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()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
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) => {
|
2016-08-25 21:04:26 +00:00
|
|
|
ipcRenderer.sendSync('set-download-option', false, false)
|
|
|
|
ipcRenderer.once('download-done', (event, state) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(state).to.equal('interrupted')
|
2016-08-25 21:04:26 +00:00
|
|
|
done()
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
w.webContents.downloadURL(`file://${path.join(__dirname, 'does-not-exist.txt')}`)
|
2016-08-25 21:04:26 +00:00
|
|
|
})
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-06-08 14:31:27 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('ses.createInterruptedDownload(options)', () => {
|
|
|
|
it('can create an interrupted download item', (done) => {
|
2016-11-24 15:16:39 +00:00
|
|
|
ipcRenderer.sendSync('set-download-option', true, false)
|
|
|
|
const filePath = path.join(__dirname, 'fixtures', 'mock.pdf')
|
|
|
|
const options = {
|
|
|
|
path: filePath,
|
2016-12-09 17:36:44 +00:00
|
|
|
urlChain: ['http://127.0.0.1/'],
|
2016-11-24 15:16:39 +00:00
|
|
|
mimeType: 'application/pdf',
|
|
|
|
offset: 0,
|
|
|
|
length: 5242880
|
|
|
|
}
|
|
|
|
w.webContents.session.createInterruptedDownload(options)
|
2017-10-27 20:45:58 +00:00
|
|
|
ipcRenderer.once('download-created', (event, state, urlChain,
|
2018-09-13 16:10:51 +00:00
|
|
|
mimeType, receivedBytes,
|
|
|
|
totalBytes, filename,
|
|
|
|
savePath) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
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)
|
2016-11-24 15:16:39 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can be resumed', (done) => {
|
2016-11-24 15:16:39 +00:00
|
|
|
const fixtures = path.join(__dirname, 'fixtures')
|
|
|
|
const downloadFilePath = path.join(fixtures, 'logo.png')
|
2017-10-27 20:45:58 +00:00
|
|
|
const rangeServer = http.createServer((req, res) => {
|
2018-10-02 01:56:31 +00:00
|
|
|
const options = { root: fixtures }
|
2016-11-24 15:16:39 +00:00
|
|
|
send(req, req.url, options)
|
2018-09-13 16:10:51 +00:00
|
|
|
.on('error', (error) => { done(error) }).pipe(res)
|
2016-11-24 15:16:39 +00:00
|
|
|
})
|
|
|
|
ipcRenderer.sendSync('set-download-option', true, false, downloadFilePath)
|
2017-10-27 20:45:58 +00:00
|
|
|
rangeServer.listen(0, '127.0.0.1', () => {
|
2016-11-24 15:16:39 +00:00
|
|
|
const port = rangeServer.address().port
|
|
|
|
const downloadUrl = `http://127.0.0.1:${port}/assets/logo.png`
|
2017-10-27 20:45:58 +00:00
|
|
|
const callback = (event, state, url, mimeType,
|
2018-09-13 16:10:51 +00:00
|
|
|
receivedBytes, totalBytes, disposition,
|
2018-11-08 14:51:06 +00:00
|
|
|
filename, savePath, dialogOptions, urlChain,
|
2018-09-13 16:10:51 +00:00
|
|
|
lastModifiedTime, eTag) => {
|
2016-11-24 15:16:39 +00:00
|
|
|
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 {
|
2019-05-20 17:04:18 +00:00
|
|
|
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()
|
2016-11-24 15:16:39 +00:00
|
|
|
fs.unlinkSync(downloadFilePath)
|
|
|
|
rangeServer.close()
|
|
|
|
ipcRenderer.removeListener('download-done', callback)
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ipcRenderer.on('download-done', callback)
|
|
|
|
w.webContents.downloadURL(downloadUrl)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-11-30 20:02:17 +00:00
|
|
|
|
2016-12-19 23:25:56 +00:00
|
|
|
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()
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.addEventListener('ipc-message', (e) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(e.channel).to.equal('message')
|
|
|
|
expect(e.args).to.deep.equal(['SecurityError'])
|
2016-12-19 23:25:56 +00:00
|
|
|
done()
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
webview.src = `file://${fixtures}/pages/permissions/midi-sysex.html`
|
2016-12-19 23:25:56 +00:00
|
|
|
webview.partition = 'permissionTest'
|
|
|
|
webview.setAttribute('nodeintegration', 'on')
|
|
|
|
document.body.appendChild(webview)
|
|
|
|
})
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|