Add download from custom protocol test (#11931)

This commit is contained in:
Paul Frazee 2018-02-15 13:39:52 -06:00 committed by John Kleinschmidt
parent bc76f35691
commit 01a6104727

View file

@ -275,6 +275,7 @@ describe('session module', () => {
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) => {
@ -289,11 +290,15 @@ describe('session module', () => {
})
const assertDownload = (event, state, url, mimeType,
receivedBytes, totalBytes, disposition,
filename, port, savePath) => {
filename, port, savePath, isCustom) => {
assert.equal(state, 'completed')
assert.equal(filename, 'mock.pdf')
assert.equal(savePath, path.join(__dirname, 'fixtures', 'mock.pdf'))
assert.equal(url, `http://127.0.0.1:${port}/`)
if (isCustom) {
assert.equal(url, `${protocolName}://item`)
} else {
assert.equal(url, `http://127.0.0.1:${port}/`)
}
assert.equal(mimeType, 'application/pdf')
assert.equal(receivedBytes, mockPDF.length)
assert.equal(totalBytes, mockPDF.length)
@ -318,6 +323,30 @@ describe('session module', () => {
})
})
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