2016-01-19 22:49:40 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const http = require('http');
|
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-19 22:49:40 +00:00
|
|
|
const ipcRenderer = require('electron').ipcRenderer;
|
|
|
|
const remote = require('electron').remote;
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-19 22:49:40 +00:00
|
|
|
const ipcMain = remote.ipcMain;
|
|
|
|
const session = remote.session;
|
|
|
|
const BrowserWindow = remote.BrowserWindow;
|
2016-01-12 02:40:23 +00:00
|
|
|
|
|
|
|
describe('session module', function() {
|
|
|
|
this.timeout(10000);
|
2016-02-17 01:09:41 +00:00
|
|
|
|
|
|
|
var fixtures = path.resolve(__dirname, 'fixtures');
|
|
|
|
var w = null;
|
|
|
|
var url = "http://127.0.0.1";
|
|
|
|
|
2016-01-12 02:40:23 +00:00
|
|
|
beforeEach(function() {
|
2016-02-17 01:39:11 +00:00
|
|
|
w = new BrowserWindow({
|
2016-01-12 02:40:23 +00:00
|
|
|
show: false,
|
|
|
|
width: 400,
|
|
|
|
height: 400
|
|
|
|
});
|
|
|
|
});
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2016-01-12 02:40:23 +00:00
|
|
|
afterEach(function() {
|
2016-02-17 01:39:11 +00:00
|
|
|
w.destroy();
|
2016-01-12 02:40:23 +00:00
|
|
|
});
|
2016-01-13 09:19:53 +00:00
|
|
|
|
|
|
|
describe('session.cookies', function() {
|
|
|
|
it('should get cookies', function(done) {
|
2016-02-17 17:27:25 +00:00
|
|
|
var server = http.createServer(function(req, res) {
|
2016-01-13 09:19:53 +00:00
|
|
|
res.setHeader('Set-Cookie', ['0=0']);
|
|
|
|
res.end('finished');
|
2016-02-17 01:39:11 +00:00
|
|
|
server.close();
|
2016-01-13 09:19:53 +00:00
|
|
|
});
|
2016-02-17 01:39:11 +00:00
|
|
|
server.listen(0, '127.0.0.1', function() {
|
2016-02-17 17:27:25 +00:00
|
|
|
var port = server.address().port;
|
2016-01-13 09:19:53 +00:00
|
|
|
w.loadURL(url + ":" + port);
|
2016-02-17 01:39:11 +00:00
|
|
|
w.webContents.on('did-finish-load', function() {
|
|
|
|
w.webContents.session.cookies.get({
|
2016-01-13 09:19:53 +00:00
|
|
|
url: url
|
|
|
|
}, function(error, list) {
|
|
|
|
var cookie, i, len;
|
|
|
|
if (error) {
|
|
|
|
return done(error);
|
|
|
|
}
|
|
|
|
for (i = 0, len = list.length; i < len; i++) {
|
|
|
|
cookie = list[i];
|
|
|
|
if (cookie.name === '0') {
|
|
|
|
if (cookie.value === '0') {
|
|
|
|
return done();
|
|
|
|
} else {
|
|
|
|
return done("cookie value is " + cookie.value + " while expecting 0");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-17 01:39:11 +00:00
|
|
|
done('Can not find cookie');
|
2016-01-13 09:19:53 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-01-12 02:40:23 +00:00
|
|
|
});
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2016-01-13 09:19:53 +00:00
|
|
|
it('should over-write the existent cookie', function(done) {
|
2016-02-17 01:39:11 +00:00
|
|
|
session.defaultSession.cookies.set({
|
2016-01-13 09:19:53 +00:00
|
|
|
url: url,
|
|
|
|
name: '1',
|
|
|
|
value: '1'
|
|
|
|
}, function(error) {
|
|
|
|
if (error) {
|
|
|
|
return done(error);
|
|
|
|
}
|
2016-02-17 01:39:11 +00:00
|
|
|
session.defaultSession.cookies.get({
|
2016-01-12 02:40:23 +00:00
|
|
|
url: url
|
|
|
|
}, function(error, list) {
|
|
|
|
var cookie, i, len;
|
|
|
|
if (error) {
|
|
|
|
return done(error);
|
|
|
|
}
|
|
|
|
for (i = 0, len = list.length; i < len; i++) {
|
|
|
|
cookie = list[i];
|
2016-01-13 09:19:53 +00:00
|
|
|
if (cookie.name === '1') {
|
|
|
|
if (cookie.value === '1') {
|
2016-01-12 02:40:23 +00:00
|
|
|
return done();
|
|
|
|
} else {
|
2016-01-13 09:19:53 +00:00
|
|
|
return done("cookie value is " + cookie.value + " while expecting 1");
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-17 01:39:11 +00:00
|
|
|
done('Can not find cookie');
|
2016-01-12 02:40:23 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2016-01-13 09:19:53 +00:00
|
|
|
it('should remove cookies', function(done) {
|
2016-02-17 01:39:11 +00:00
|
|
|
session.defaultSession.cookies.set({
|
2016-01-13 09:19:53 +00:00
|
|
|
url: url,
|
|
|
|
name: '2',
|
|
|
|
value: '2'
|
|
|
|
}, function(error) {
|
2016-01-12 02:40:23 +00:00
|
|
|
if (error) {
|
|
|
|
return done(error);
|
|
|
|
}
|
2016-02-17 01:39:11 +00:00
|
|
|
session.defaultSession.cookies.remove(url, '2', function() {
|
|
|
|
session.defaultSession.cookies.get({
|
2016-01-13 09:19:53 +00:00
|
|
|
url: url
|
|
|
|
}, function(error, list) {
|
|
|
|
var cookie, i, len;
|
|
|
|
if (error) {
|
|
|
|
return done(error);
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-01-13 09:19:53 +00:00
|
|
|
for (i = 0, len = list.length; i < len; i++) {
|
|
|
|
cookie = list[i];
|
|
|
|
if (cookie.name === '2') {
|
|
|
|
return done('Cookie not deleted');
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-02-17 01:39:11 +00:00
|
|
|
done();
|
2016-01-13 09:19:53 +00:00
|
|
|
});
|
2016-01-12 02:40:23 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-01-13 09:19:53 +00:00
|
|
|
|
2016-01-12 02:40:23 +00:00
|
|
|
describe('session.clearStorageData(options)', function() {
|
|
|
|
fixtures = path.resolve(__dirname, 'fixtures');
|
2016-02-17 01:39:11 +00:00
|
|
|
it('clears localstorage data', function(done) {
|
2016-01-12 02:40:23 +00:00
|
|
|
ipcMain.on('count', function(event, count) {
|
|
|
|
ipcMain.removeAllListeners('count');
|
|
|
|
assert(!count);
|
2016-02-17 01:39:11 +00:00
|
|
|
done();
|
2016-01-12 02:40:23 +00:00
|
|
|
});
|
|
|
|
w.loadURL('file://' + path.join(fixtures, 'api', 'localstorage.html'));
|
2016-02-17 01:39:11 +00:00
|
|
|
w.webContents.on('did-finish-load', function() {
|
2016-02-17 17:27:25 +00:00
|
|
|
var options = {
|
2016-01-12 02:40:23 +00:00
|
|
|
origin: "file://",
|
|
|
|
storages: ['localstorage'],
|
|
|
|
quotas: ['persistent']
|
|
|
|
};
|
2016-02-17 01:39:11 +00:00
|
|
|
w.webContents.session.clearStorageData(options, function() {
|
|
|
|
w.webContents.send('getcount');
|
2016-01-12 02:40:23 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-02-02 10:24:51 +00:00
|
|
|
|
|
|
|
describe('session will-download event', function() {
|
|
|
|
var w = null;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
width: 400,
|
|
|
|
height: 400
|
|
|
|
});
|
|
|
|
});
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2016-02-02 10:24:51 +00:00
|
|
|
afterEach(function() {
|
2016-02-17 01:39:11 +00:00
|
|
|
w.destroy();
|
2016-02-02 10:24:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can cancel default download behavior', function(done) {
|
|
|
|
const mockFile = new Buffer(1024);
|
|
|
|
const contentDisposition = 'inline; filename="mockFile.txt"';
|
|
|
|
const downloadServer = http.createServer(function(req, res) {
|
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Length': mockFile.length,
|
|
|
|
'Content-Type': 'application/plain',
|
|
|
|
'Content-Disposition': contentDisposition
|
|
|
|
});
|
|
|
|
res.end(mockFile);
|
|
|
|
downloadServer.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
downloadServer.listen(0, '127.0.0.1', function() {
|
|
|
|
const port = downloadServer.address().port;
|
|
|
|
const url = "http://127.0.0.1:" + port + '/';
|
|
|
|
|
|
|
|
ipcRenderer.sendSync('set-download-option', false, true);
|
|
|
|
w.loadURL(url);
|
|
|
|
ipcRenderer.once('download-error', function(event, downloadUrl, filename, error) {
|
|
|
|
assert.equal(downloadUrl, url);
|
|
|
|
assert.equal(filename, 'mockFile.txt');
|
|
|
|
assert.equal(error, 'Object has been destroyed');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-02-17 01:39:11 +00:00
|
|
|
describe('DownloadItem', function() {
|
|
|
|
var mockPDF = new Buffer(1024 * 1024 * 5);
|
|
|
|
var contentDisposition = 'inline; filename="mock.pdf"';
|
|
|
|
var downloadFilePath = path.join(fixtures, 'mock.pdf');
|
|
|
|
var downloadServer = http.createServer(function(req, res) {
|
2016-01-12 02:40:23 +00:00
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Length': mockPDF.length,
|
|
|
|
'Content-Type': 'application/pdf',
|
|
|
|
'Content-Disposition': contentDisposition
|
|
|
|
});
|
|
|
|
res.end(mockPDF);
|
2016-02-17 01:39:11 +00:00
|
|
|
downloadServer.close();
|
2016-01-12 02:40:23 +00:00
|
|
|
});
|
2016-02-17 01:39:11 +00:00
|
|
|
var assertDownload = function(event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, port) {
|
2016-01-12 02:40:23 +00:00
|
|
|
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));
|
2016-02-17 01:39:11 +00:00
|
|
|
fs.unlinkSync(downloadFilePath);
|
2016-01-12 02:40:23 +00:00
|
|
|
};
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2016-01-12 02:40:23 +00:00
|
|
|
it('can download using BrowserWindow.loadURL', function(done) {
|
2016-02-17 01:39:11 +00:00
|
|
|
downloadServer.listen(0, '127.0.0.1', function() {
|
|
|
|
var port = downloadServer.address().port;
|
2016-02-02 10:24:51 +00:00
|
|
|
ipcRenderer.sendSync('set-download-option', false, false);
|
2016-01-12 02:40:23 +00:00
|
|
|
w.loadURL(url + ":" + port);
|
2016-02-17 01:39:11 +00:00
|
|
|
ipcRenderer.once('download-done', function(event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename) {
|
2016-01-12 02:40:23 +00:00
|
|
|
assertDownload(event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, port);
|
2016-02-17 01:39:11 +00:00
|
|
|
done();
|
2016-01-12 02:40:23 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2016-01-12 02:40:23 +00:00
|
|
|
it('can download using WebView.downloadURL', function(done) {
|
2016-02-17 01:39:11 +00:00
|
|
|
downloadServer.listen(0, '127.0.0.1', function() {
|
|
|
|
var port = downloadServer.address().port;
|
2016-02-02 10:24:51 +00:00
|
|
|
ipcRenderer.sendSync('set-download-option', false, false);
|
2016-02-17 01:39:11 +00:00
|
|
|
var webview = new WebView;
|
2016-01-12 02:40:23 +00:00
|
|
|
webview.src = "file://" + fixtures + "/api/blank.html";
|
|
|
|
webview.addEventListener('did-finish-load', function() {
|
2016-02-17 01:39:11 +00:00
|
|
|
webview.downloadURL(url + ":" + port + "/");
|
2016-01-12 02:40:23 +00:00
|
|
|
});
|
|
|
|
ipcRenderer.once('download-done', function(event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename) {
|
|
|
|
assertDownload(event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, port);
|
|
|
|
document.body.removeChild(webview);
|
2016-02-17 01:39:11 +00:00
|
|
|
done();
|
2016-01-12 02:40:23 +00:00
|
|
|
});
|
2016-02-17 01:39:11 +00:00
|
|
|
document.body.appendChild(webview);
|
2016-01-12 02:40:23 +00:00
|
|
|
});
|
|
|
|
});
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2016-02-02 10:24:51 +00:00
|
|
|
it('can cancel download', function(done) {
|
2016-02-17 01:39:11 +00:00
|
|
|
downloadServer.listen(0, '127.0.0.1', function() {
|
|
|
|
var port = downloadServer.address().port;
|
2016-02-02 10:24:51 +00:00
|
|
|
ipcRenderer.sendSync('set-download-option', true, false);
|
2016-01-12 02:40:23 +00:00
|
|
|
w.loadURL(url + ":" + port + "/");
|
2016-02-17 01:39:11 +00:00
|
|
|
ipcRenderer.once('download-done', function(event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename) {
|
2016-01-12 02:40:23 +00:00
|
|
|
assert.equal(state, 'cancelled');
|
|
|
|
assert.equal(filename, 'mock.pdf');
|
|
|
|
assert.equal(mimeType, 'application/pdf');
|
|
|
|
assert.equal(receivedBytes, 0);
|
|
|
|
assert.equal(totalBytes, mockPDF.length);
|
|
|
|
assert.equal(disposition, contentDisposition);
|
2016-02-17 01:39:11 +00:00
|
|
|
done();
|
2016-01-12 02:40:23 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|