2015-07-14 16:14:22 +00:00
|
|
|
assert = require 'assert'
|
|
|
|
http = require 'http'
|
|
|
|
path = require 'path'
|
2015-09-21 13:44:51 +00:00
|
|
|
fs = require 'fs'
|
2015-11-12 10:28:04 +00:00
|
|
|
|
|
|
|
{ipcRenderer, remote} = require 'electron'
|
2015-11-19 13:42:22 +00:00
|
|
|
{app, ipcMain, session, BrowserWindow} = remote
|
2015-06-16 02:37:39 +00:00
|
|
|
|
2015-07-02 14:12:18 +00:00
|
|
|
describe 'session module', ->
|
2015-07-14 15:59:06 +00:00
|
|
|
@timeout 10000
|
2015-06-16 09:23:29 +00:00
|
|
|
fixtures = path.resolve __dirname, 'fixtures'
|
2015-06-16 02:37:39 +00:00
|
|
|
w = null
|
2015-06-20 02:42:18 +00:00
|
|
|
url = "http://127.0.0.1"
|
2015-06-16 09:23:29 +00:00
|
|
|
|
2015-07-02 14:12:18 +00:00
|
|
|
beforeEach -> w = new BrowserWindow(show: false, width: 400, height: 400)
|
2015-06-16 02:37:39 +00:00
|
|
|
afterEach -> w.destroy()
|
|
|
|
|
|
|
|
it 'should get cookies', (done) ->
|
|
|
|
server = http.createServer (req, res) ->
|
2015-07-14 16:14:22 +00:00
|
|
|
res.setHeader('Set-Cookie', ['0=0'])
|
2015-06-16 02:37:39 +00:00
|
|
|
res.end('finished')
|
2015-06-20 02:42:18 +00:00
|
|
|
server.close()
|
2015-06-16 02:37:39 +00:00
|
|
|
|
2015-07-24 07:49:19 +00:00
|
|
|
server.listen 0, '127.0.0.1', ->
|
2015-06-16 02:37:39 +00:00
|
|
|
{port} = server.address()
|
2015-11-13 08:03:40 +00:00
|
|
|
w.loadURL "#{url}:#{port}"
|
2015-07-14 15:59:06 +00:00
|
|
|
w.webContents.on 'did-finish-load', ->
|
2015-07-14 16:14:22 +00:00
|
|
|
w.webContents.session.cookies.get {url: url}, (error, list) ->
|
2015-07-14 15:59:06 +00:00
|
|
|
return done(error) if error
|
2015-07-14 16:14:22 +00:00
|
|
|
for cookie in list when cookie.name is '0'
|
|
|
|
if cookie.value is '0'
|
|
|
|
return done()
|
|
|
|
else
|
|
|
|
return done("cookie value is #{cookie.value} while expecting 0")
|
|
|
|
done('Can not find cookie')
|
2015-06-16 02:37:39 +00:00
|
|
|
|
2015-07-14 15:59:06 +00:00
|
|
|
it 'should over-write the existent cookie', (done) ->
|
2015-11-19 13:42:22 +00:00
|
|
|
session.defaultSession.cookies.set {url: url, name: '1', value: '1'}, (error) ->
|
2015-07-14 16:14:22 +00:00
|
|
|
return done(error) if error
|
2015-11-19 13:42:22 +00:00
|
|
|
session.defaultSession.cookies.get {url: url}, (error, list) ->
|
2015-07-14 15:59:06 +00:00
|
|
|
return done(error) if error
|
2015-07-14 16:14:22 +00:00
|
|
|
for cookie in list when cookie.name is '1'
|
|
|
|
if cookie.value is '1'
|
|
|
|
return done()
|
|
|
|
else
|
|
|
|
return done("cookie value is #{cookie.value} while expecting 1")
|
|
|
|
done('Can not find cookie')
|
2015-06-16 02:37:39 +00:00
|
|
|
|
|
|
|
it 'should remove cookies', (done) ->
|
2015-11-19 13:42:22 +00:00
|
|
|
session.defaultSession.cookies.set {url: url, name: '2', value: '2'}, (error) ->
|
2015-07-14 16:14:22 +00:00
|
|
|
return done(error) if error
|
2015-11-19 13:42:22 +00:00
|
|
|
session.defaultSession.cookies.remove {url: url, name: '2'}, (error) ->
|
2015-07-14 15:59:06 +00:00
|
|
|
return done(error) if error
|
2015-11-19 13:42:22 +00:00
|
|
|
session.defaultSession.cookies.get {url: url}, (error, list) ->
|
2015-07-14 15:59:06 +00:00
|
|
|
return done(error) if error
|
2015-07-14 16:14:22 +00:00
|
|
|
for cookie in list when cookie.name is '2'
|
|
|
|
return done('Cookie not deleted')
|
|
|
|
done()
|
2015-07-02 14:12:18 +00:00
|
|
|
|
|
|
|
describe 'session.clearStorageData(options)', ->
|
|
|
|
fixtures = path.resolve __dirname, 'fixtures'
|
|
|
|
it 'clears localstorage data', (done) ->
|
2015-11-10 08:04:34 +00:00
|
|
|
ipcMain.on 'count', (event, count) ->
|
|
|
|
ipcMain.removeAllListeners 'count'
|
2015-07-02 14:12:18 +00:00
|
|
|
assert not count
|
|
|
|
done()
|
2015-11-13 08:03:40 +00:00
|
|
|
w.loadURL 'file://' + path.join(fixtures, 'api', 'localstorage.html')
|
2015-07-02 14:12:18 +00:00
|
|
|
w.webContents.on 'did-finish-load', ->
|
2015-07-13 22:22:40 +00:00
|
|
|
options =
|
|
|
|
origin: "file://",
|
|
|
|
storages: ['localstorage'],
|
|
|
|
quotas: ['persistent'],
|
|
|
|
w.webContents.session.clearStorageData options, ->
|
2015-07-02 14:12:18 +00:00
|
|
|
w.webContents.send 'getcount'
|
2015-09-21 13:44:51 +00:00
|
|
|
|
|
|
|
describe 'DownloadItem', ->
|
2015-09-24 07:55:45 +00:00
|
|
|
# A 5 MB mock pdf.
|
|
|
|
mockPDF = new Buffer 1024 * 1024 * 5
|
2015-09-21 13:44:51 +00:00
|
|
|
contentDisposition = 'inline; filename="mock.pdf"'
|
2015-09-24 07:55:45 +00:00
|
|
|
downloadFilePath = path.join fixtures, 'mock.pdf'
|
2015-09-21 13:44:51 +00:00
|
|
|
downloadServer = http.createServer (req, res) ->
|
|
|
|
res.writeHead 200, {
|
|
|
|
'Content-Length': mockPDF.length,
|
|
|
|
'Content-Type': 'application/pdf',
|
|
|
|
'Content-Disposition': contentDisposition
|
|
|
|
}
|
|
|
|
res.end mockPDF
|
|
|
|
downloadServer.close()
|
|
|
|
|
2015-12-03 02:40:39 +00:00
|
|
|
assertDownload = (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, port) ->
|
|
|
|
assert.equal state, 'completed'
|
|
|
|
assert.equal filename, 'mock.pdf'
|
|
|
|
assert.equal url, "http://127.0.0.1:#{port}/"
|
|
|
|
assert.equal mimeType, 'application/pdf'
|
|
|
|
assert.equal receivedBytes, mockPDF.length
|
|
|
|
assert.equal totalBytes, mockPDF.length
|
|
|
|
assert.equal disposition, contentDisposition
|
|
|
|
assert fs.existsSync downloadFilePath
|
|
|
|
fs.unlinkSync downloadFilePath
|
|
|
|
|
|
|
|
it 'can download using BrowserWindow.loadURL', (done) ->
|
2015-09-21 13:44:51 +00:00
|
|
|
downloadServer.listen 0, '127.0.0.1', ->
|
|
|
|
{port} = downloadServer.address()
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcRenderer.sendSync 'set-download-option', false
|
2015-11-13 08:03:40 +00:00
|
|
|
w.loadURL "#{url}:#{port}"
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcRenderer.once 'download-done', (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename) ->
|
2015-12-03 02:40:39 +00:00
|
|
|
assertDownload event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, port
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'can download using WebView.downloadURL', (done) ->
|
|
|
|
downloadServer.listen 0, '127.0.0.1', ->
|
|
|
|
{port} = downloadServer.address()
|
|
|
|
ipcRenderer.sendSync 'set-download-option', false
|
|
|
|
|
|
|
|
webview = new WebView
|
|
|
|
webview.src = "file://#{fixtures}/api/blank.html"
|
|
|
|
webview.addEventListener 'did-finish-load', ->
|
|
|
|
webview.downloadURL "#{url}:#{port}/"
|
|
|
|
|
|
|
|
ipcRenderer.once 'download-done', (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename) ->
|
|
|
|
assertDownload event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, port
|
|
|
|
document.body.removeChild(webview)
|
2015-09-24 07:55:45 +00:00
|
|
|
done()
|
2015-09-21 13:44:51 +00:00
|
|
|
|
2015-12-03 02:40:39 +00:00
|
|
|
document.body.appendChild webview
|
|
|
|
|
2015-09-21 13:44:51 +00:00
|
|
|
it 'can cancel download', (done) ->
|
|
|
|
downloadServer.listen 0, '127.0.0.1', ->
|
|
|
|
{port} = downloadServer.address()
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcRenderer.sendSync 'set-download-option', true
|
2015-11-13 08:03:40 +00:00
|
|
|
w.loadURL "#{url}:#{port}/"
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcRenderer.once 'download-done', (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename) ->
|
2015-09-24 07:55:45 +00:00
|
|
|
assert.equal state, 'cancelled'
|
2015-09-24 08:31:41 +00:00
|
|
|
assert.equal filename, 'mock.pdf'
|
2015-09-24 07:55:45 +00:00
|
|
|
assert.equal mimeType, 'application/pdf'
|
|
|
|
assert.equal receivedBytes, 0
|
|
|
|
assert.equal totalBytes, mockPDF.length
|
|
|
|
assert.equal disposition, contentDisposition
|
|
|
|
done()
|