This commit is contained in:
deepak1556 2016-11-24 20:46:39 +05:30 committed by Kevin Sawicki
parent d944219b28
commit f124732431
4 changed files with 125 additions and 12 deletions

View file

@ -3,6 +3,7 @@ const http = require('http')
const https = require('https')
const path = require('path')
const fs = require('fs')
const send = require('send')
const {closeWindow} = require('./window-helpers')
const {ipcRenderer, remote} = require('electron')
@ -288,7 +289,9 @@ describe('session module', function () {
res.end(mockPDF)
downloadServer.close()
})
var assertDownload = function (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, port, savePath) {
var assertDownload = function (event, state, url, mimeType,
receivedBytes, totalBytes, disposition,
filename, port, savePath) {
assert.equal(state, 'completed')
assert.equal(filename, 'mock.pdf')
assert.equal(savePath, path.join(__dirname, 'fixtures', 'mock.pdf'))
@ -306,8 +309,12 @@ describe('session module', function () {
var port = downloadServer.address().port
ipcRenderer.sendSync('set-download-option', false, false)
w.loadURL(url + ':' + port)
ipcRenderer.once('download-done', function (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, savePath) {
assertDownload(event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, port, savePath)
ipcRenderer.once('download-done', function (event, state, url,
mimeType, receivedBytes,
totalBytes, disposition,
filename, savePath) {
assertDownload(event, state, url, mimeType, receivedBytes,
totalBytes, disposition, filename, port, savePath)
done()
})
})
@ -322,8 +329,12 @@ describe('session module', function () {
webview.addEventListener('did-finish-load', function () {
webview.downloadURL(url + ':' + port + '/')
})
ipcRenderer.once('download-done', function (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, savePath) {
assertDownload(event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, port, savePath)
ipcRenderer.once('download-done', function (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()
})
@ -336,7 +347,10 @@ describe('session module', function () {
var port = downloadServer.address().port
ipcRenderer.sendSync('set-download-option', true, false)
w.loadURL(url + ':' + port + '/')
ipcRenderer.once('download-done', function (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename) {
ipcRenderer.once('download-done', function (event, state, url,
mimeType, receivedBytes,
totalBytes, disposition,
filename) {
assert.equal(state, 'cancelled')
assert.equal(filename, 'mock.pdf')
assert.equal(mimeType, 'application/pdf')
@ -356,7 +370,10 @@ describe('session module', function () {
var port = downloadServer.address().port
ipcRenderer.sendSync('set-download-option', true, false)
w.loadURL(url + ':' + port + '/?testFilename')
ipcRenderer.once('download-done', function (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename) {
ipcRenderer.once('download-done', function (event, state, url,
mimeType, receivedBytes,
totalBytes, disposition,
filename) {
assert.equal(state, 'cancelled')
assert.equal(filename, 'download.pdf')
assert.equal(mimeType, 'application/pdf')
@ -565,4 +582,84 @@ describe('session module', function () {
w.loadURL(url)
})
})
describe('ses.createInterruptedDownload(options)', function () {
it('can create an interrupted download item', function (done) {
ipcRenderer.sendSync('set-download-option', true, false)
const filePath = path.join(__dirname, 'fixtures', 'mock.pdf')
const urlChain = ['http://127.0.0.1/']
const options = {
path: filePath,
urlChain: urlChain,
mimeType: 'application/pdf',
offset: 0,
length: 5242880
}
w.webContents.session.createInterruptedDownload(options)
ipcRenderer.once('download-created', function (event, state, urlChain,
mimeType, receivedBytes,
totalBytes, filename,
savePath) {
assert.equal(state, 'interrupted')
assert.equal(urlChain, urlChain)
assert.equal(mimeType, 'application/pdf')
assert.equal(receivedBytes, 0)
assert.equal(totalBytes, 5242880)
assert.equal(savePath, filePath)
done()
})
})
it('can be resumed', function (done) {
const fixtures = path.join(__dirname, 'fixtures')
const downloadFilePath = path.join(fixtures, 'logo.png')
const rangeServer = http.createServer(function (req, res) {
let options = {
root: fixtures
}
send(req, req.url, options)
.on('error', function (error) {
done(error)
}).pipe(res)
})
ipcRenderer.sendSync('set-download-option', true, false, downloadFilePath)
rangeServer.listen(0, '127.0.0.1', function () {
const port = rangeServer.address().port
const downloadUrl = `http://127.0.0.1:${port}/assets/logo.png`
const callback = function (event, state, url, mimeType,
receivedBytes, totalBytes, disposition,
filename, savePath, 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 {
assert.equal(state, 'completed')
assert.equal(filename, 'logo.png')
assert.equal(savePath, downloadFilePath)
assert.equal(url, downloadUrl)
assert.equal(mimeType, 'image/png')
assert.equal(receivedBytes, 14022)
assert.equal(totalBytes, 14022)
assert(fs.existsSync(downloadFilePath))
fs.unlinkSync(downloadFilePath)
rangeServer.close()
ipcRenderer.removeListener('download-done', callback)
done()
}
}
ipcRenderer.on('download-done', callback)
w.webContents.downloadURL(downloadUrl)
})
})
})
})