2016-03-25 20:03:49 +00:00
|
|
|
const assert = require('assert')
|
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', () => {
|
|
|
|
let fixtures = path.resolve(__dirname, 'fixtures')
|
|
|
|
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('session.defaultSession', () => {
|
|
|
|
it('returns the default session', () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(session.defaultSession, session.fromPartition(''))
|
2016-07-12 13:24:09 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('session.fromPartition(partition, options)', () => {
|
|
|
|
it('returns existing session with same partition', () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(session.fromPartition('test'), session.fromPartition('test'))
|
2016-07-12 13:38:50 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('created session is ref-counted', () => {
|
2016-07-12 13:38:50 +00:00
|
|
|
const partition = 'test2'
|
|
|
|
const userAgent = 'test-agent'
|
|
|
|
const ses1 = session.fromPartition(partition)
|
|
|
|
ses1.setUserAgent(userAgent)
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(ses1.getUserAgent(), userAgent)
|
2016-07-12 13:38:50 +00:00
|
|
|
ses1.destroy()
|
|
|
|
const ses2 = session.fromPartition(partition)
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.notStrictEqual(ses2.getUserAgent(), userAgent)
|
2016-07-12 13:38:50 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('ses.cookies', () => {
|
2019-01-25 18:11:35 +00:00
|
|
|
const name = '0'
|
|
|
|
const value = '0'
|
|
|
|
|
|
|
|
it('should get cookies with promises', (done) => {
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
res.setHeader('Set-Cookie', [`${name}=${value}`])
|
|
|
|
res.end('finished')
|
|
|
|
server.close()
|
|
|
|
})
|
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
|
|
w.webContents.once('did-finish-load', async () => {
|
|
|
|
const list = await w.webContents.session.cookies.get({ url })
|
|
|
|
const cookie = list.find(cookie => cookie.name === name)
|
|
|
|
|
|
|
|
expect(cookie).to.exist.and.to.have.property('value', value)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
const { port } = server.address()
|
|
|
|
w.loadURL(`${url}:${port}`)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should get cookies with callbacks', (done) => {
|
2017-10-27 20:45:58 +00:00
|
|
|
const server = http.createServer((req, res) => {
|
2019-01-25 18:11:35 +00:00
|
|
|
res.setHeader('Set-Cookie', [`${name}=${value}`])
|
2016-03-25 20:03:49 +00:00
|
|
|
res.end('finished')
|
|
|
|
server.close()
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
server.listen(0, '127.0.0.1', () => {
|
2018-12-19 06:44:03 +00:00
|
|
|
w.webContents.once('did-finish-load', () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
w.webContents.session.cookies.get({ url }, (error, list) => {
|
2017-10-27 20:45:58 +00:00
|
|
|
if (error) return done(error)
|
2019-01-25 18:11:35 +00:00
|
|
|
const cookie = list.find(cookie => cookie.name === name)
|
|
|
|
expect(cookie).to.exist.and.to.have.property('value', value)
|
|
|
|
done()
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
2019-01-25 18:11:35 +00:00
|
|
|
const { port } = server.address()
|
2018-12-19 06:44:03 +00:00
|
|
|
w.loadURL(`${url}:${port}`)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2019-01-25 18:11:35 +00:00
|
|
|
it('sets cookies with promises', async () => {
|
|
|
|
let error
|
|
|
|
try {
|
|
|
|
const { cookies } = session.defaultSession
|
|
|
|
const name = '1'
|
|
|
|
const value = '1'
|
|
|
|
|
|
|
|
await cookies.set({ url, name, value })
|
|
|
|
} catch (e) {
|
|
|
|
error = e
|
|
|
|
}
|
|
|
|
expect(error).to.be.undefined(error)
|
2016-06-29 01:44:00 +00:00
|
|
|
})
|
|
|
|
|
2019-01-25 18:11:35 +00:00
|
|
|
it('sets cookies with callbacks', (done) => {
|
|
|
|
const { cookies } = session.defaultSession
|
|
|
|
const name = '1'
|
|
|
|
const value = '1'
|
|
|
|
cookies.set({ url, name, value }, (error, list) => done(error))
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2019-01-25 18:11:35 +00:00
|
|
|
it('yields an error when setting a cookie with missing required fields', async () => {
|
|
|
|
let error
|
|
|
|
try {
|
|
|
|
const { cookies } = session.defaultSession
|
|
|
|
const name = '1'
|
|
|
|
const value = '1'
|
|
|
|
await cookies.set({ url: '', name, value })
|
|
|
|
} catch (e) {
|
|
|
|
error = e
|
|
|
|
}
|
|
|
|
expect(error).is.an('Error')
|
2019-02-27 10:35:52 +00:00
|
|
|
expect(error).to.have.property('message').which.equals('Failed to get cookie domain')
|
2019-01-25 18:11:35 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should overwrite previous cookies', async () => {
|
|
|
|
let error
|
|
|
|
try {
|
|
|
|
const { cookies } = session.defaultSession
|
|
|
|
const name = 'DidOverwrite'
|
|
|
|
for (const value of [ 'No', 'Yes' ]) {
|
|
|
|
await cookies.set({ url, name, value })
|
|
|
|
const list = await cookies.get({ url })
|
|
|
|
|
|
|
|
assert(list.some(cookie => cookie.name === name && cookie.value === value))
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
error = e
|
|
|
|
}
|
|
|
|
expect(error).to.be.undefined(error)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should remove cookies with promises', async () => {
|
|
|
|
let error
|
|
|
|
try {
|
|
|
|
const { cookies } = session.defaultSession
|
|
|
|
const name = '2'
|
|
|
|
const value = '2'
|
|
|
|
|
|
|
|
await cookies.set({ url, name, value })
|
|
|
|
await cookies.remove(url, name)
|
|
|
|
const list = await cookies.get({ url })
|
|
|
|
|
|
|
|
assert(!list.some(cookie => cookie.name === name && cookie.value === value))
|
|
|
|
} catch (e) {
|
|
|
|
error = e
|
|
|
|
}
|
|
|
|
expect(error).to.be.undefined(error)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should remove cookies with callbacks', (done) => {
|
|
|
|
const { cookies } = session.defaultSession
|
|
|
|
const name = '2'
|
|
|
|
const value = '2'
|
|
|
|
|
|
|
|
cookies.set({ url, name, value }, (error) => {
|
2017-10-27 20:45:58 +00:00
|
|
|
if (error) return done(error)
|
2019-01-25 18:11:35 +00:00
|
|
|
cookies.remove(url, name, (error) => {
|
|
|
|
if (error) return done(error)
|
|
|
|
cookies.get({ url }, (error, list) => {
|
2017-10-27 20:45:58 +00:00
|
|
|
if (error) return done(error)
|
2019-01-25 18:11:35 +00:00
|
|
|
assert(!list.some(cookie => cookie.name === name))
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-08-03 10:39:03 +00:00
|
|
|
|
2019-01-25 18:11:35 +00:00
|
|
|
it('should set cookie for standard scheme', async () => {
|
|
|
|
let error
|
|
|
|
try {
|
|
|
|
const { cookies } = session.defaultSession
|
|
|
|
const standardScheme = remote.getGlobal('standardScheme')
|
|
|
|
const domain = 'fake-host'
|
|
|
|
const url = `${standardScheme}://${domain}`
|
|
|
|
const name = 'custom'
|
|
|
|
const value = '1'
|
2016-09-29 00:08:25 +00:00
|
|
|
|
2019-01-25 18:11:35 +00:00
|
|
|
await cookies.set({ url, name, value })
|
|
|
|
const list = await cookies.get({ url })
|
2016-09-29 00:08:25 +00:00
|
|
|
|
2019-01-25 18:11:35 +00:00
|
|
|
expect(list).to.have.lengthOf(1)
|
|
|
|
expect(list[0]).to.have.property('name', name)
|
|
|
|
expect(list[0]).to.have.property('value', value)
|
|
|
|
expect(list[0]).to.have.property('domain', domain)
|
|
|
|
} catch (e) {
|
|
|
|
error = e
|
|
|
|
}
|
2016-09-29 00:39:56 +00:00
|
|
|
|
2019-01-25 18:11:35 +00:00
|
|
|
expect(error).to.be.undefined(error)
|
|
|
|
})
|
2016-09-29 00:39:56 +00:00
|
|
|
|
2019-01-25 18:11:35 +00:00
|
|
|
it('emits a changed event when a cookie is added or removed', async () => {
|
|
|
|
let error
|
|
|
|
const changes = []
|
2016-09-29 00:08:25 +00:00
|
|
|
|
2019-01-25 18:11:35 +00:00
|
|
|
try {
|
|
|
|
const { cookies } = session.fromPartition('cookies-changed')
|
|
|
|
const name = 'foo'
|
|
|
|
const value = 'bar'
|
|
|
|
const eventName = 'changed'
|
|
|
|
const listener = (event, cookie, cause, removed) => { changes.push({ cookie, cause, removed }) }
|
|
|
|
|
|
|
|
cookies.on(eventName, listener)
|
|
|
|
await cookies.set({ url, name, value })
|
|
|
|
await cookies.remove(url, name)
|
|
|
|
cookies.off(eventName, listener)
|
|
|
|
|
|
|
|
expect(changes).to.have.lengthOf(2)
|
|
|
|
expect(changes.every(change => change.cookie.name === name))
|
|
|
|
expect(changes.every(change => change.cookie.value === value))
|
|
|
|
expect(changes.every(change => change.cause === 'explicit'))
|
|
|
|
expect(changes[0].removed).to.be.false()
|
|
|
|
expect(changes[1].removed).to.be.true()
|
|
|
|
} catch (e) {
|
|
|
|
error = e
|
|
|
|
}
|
|
|
|
expect(error).to.be.undefined(error)
|
2016-09-29 00:08:25 +00:00
|
|
|
})
|
2017-04-18 16:08:27 +00:00
|
|
|
|
2019-01-25 18:11:35 +00:00
|
|
|
describe('ses.cookies.flushStore()', async () => {
|
|
|
|
describe('flushes the cookies to disk and invokes the callback when done', async () => {
|
|
|
|
it('with promises', async () => {
|
|
|
|
let error
|
|
|
|
try {
|
|
|
|
const name = 'foo'
|
|
|
|
const value = 'bar'
|
|
|
|
const { cookies } = session.defaultSession
|
|
|
|
|
|
|
|
await cookies.set({ url, name, value })
|
|
|
|
await cookies.flushStore()
|
|
|
|
} catch (e) {
|
|
|
|
error = e
|
|
|
|
}
|
|
|
|
expect(error).to.be.undefined(error)
|
|
|
|
})
|
2019-01-25 22:23:24 +00:00
|
|
|
|
2019-01-25 18:11:35 +00:00
|
|
|
it('with callbacks', (done) => {
|
|
|
|
const name = 'foo'
|
|
|
|
const value = 'bar'
|
|
|
|
const { cookies } = session.defaultSession
|
|
|
|
|
2019-01-25 22:23:24 +00:00
|
|
|
cookies.set({ url, name, value }, (error) => {
|
2019-01-25 18:11:35 +00:00
|
|
|
if (error) return done(error)
|
|
|
|
cookies.flushStore(error => done(error))
|
2017-04-18 16:08:27 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2018-11-29 18:51:13 +00:00
|
|
|
|
2019-01-25 22:23:24 +00:00
|
|
|
describe('ses.cookies.flushStore(callback)', () => {
|
|
|
|
it('flushes the cookies to disk and invokes the callback when done', (done) => {
|
|
|
|
session.defaultSession.cookies.set({
|
|
|
|
url: url,
|
|
|
|
name: 'foo',
|
|
|
|
value: 'bar'
|
|
|
|
}, (error) => {
|
|
|
|
if (error) return done(error)
|
|
|
|
session.defaultSession.cookies.flushStore(() => done())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-11-29 18:51:13 +00:00
|
|
|
it('should survive an app restart for persistent partition', async () => {
|
|
|
|
const appPath = path.join(__dirname, 'fixtures', 'api', 'cookie-app')
|
|
|
|
const electronPath = remote.getGlobal('process').execPath
|
|
|
|
|
|
|
|
const test = (result, phase) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let output = ''
|
|
|
|
|
|
|
|
const appProcess = ChildProcess.spawn(
|
|
|
|
electronPath,
|
|
|
|
[appPath],
|
|
|
|
{ env: { PHASE: phase, ...process.env } }
|
|
|
|
)
|
|
|
|
|
2019-01-25 18:11:35 +00:00
|
|
|
appProcess.stdout.on('data', data => { output += data })
|
2018-11-29 18:51:13 +00:00
|
|
|
appProcess.stdout.on('end', () => {
|
|
|
|
output = output.replace(/(\r\n|\n|\r)/gm, '')
|
|
|
|
assert.strictEqual(output, result)
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
await test('011', 'one')
|
|
|
|
await test('110', 'two')
|
|
|
|
})
|
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('ses.clearStorageData(options)', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
fixtures = path.resolve(__dirname, 'fixtures')
|
2017-10-27 20:45:58 +00:00
|
|
|
it('clears localstorage data', (done) => {
|
2019-03-08 17:02:30 +00:00
|
|
|
ipcMain.on('count', (event, count) => {
|
|
|
|
ipcMain.removeAllListeners('count')
|
|
|
|
assert.strictEqual(count, 0)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.webContents.on('did-finish-load', () => {
|
|
|
|
const options = {
|
|
|
|
origin: 'file://',
|
|
|
|
storages: ['localstorage'],
|
|
|
|
quotas: ['persistent']
|
|
|
|
}
|
|
|
|
w.webContents.session.clearStorageData(options).then(() => {
|
|
|
|
w.webContents.send('getcount')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
w.loadFile(path.join(fixtures, 'api', 'localstorage.html'))
|
|
|
|
})
|
|
|
|
|
|
|
|
// TODO(codebytere): remove when promisification is complete
|
|
|
|
it('clears localstorage data (callback)', (done) => {
|
2017-10-27 20:45:58 +00:00
|
|
|
ipcMain.on('count', (event, count) => {
|
2016-03-25 20:03:49 +00:00
|
|
|
ipcMain.removeAllListeners('count')
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(count, 0)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
w.webContents.on('did-finish-load', () => {
|
|
|
|
const options = {
|
2016-03-25 20:03:49 +00:00
|
|
|
origin: 'file://',
|
2016-01-12 02:40:23 +00:00
|
|
|
storages: ['localstorage'],
|
|
|
|
quotas: ['persistent']
|
2016-03-25 20:03:49 +00:00
|
|
|
}
|
2017-10-27 20:45:58 +00:00
|
|
|
w.webContents.session.clearStorageData(options, () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.webContents.send('getcount')
|
|
|
|
})
|
|
|
|
})
|
2018-12-19 06:44:03 +00:00
|
|
|
w.loadFile(path.join(fixtures, 'api', 'localstorage.html'))
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
2016-02-02 10:24:51 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
describe('will-download event', () => {
|
|
|
|
beforeEach(() => {
|
2016-11-29 19:52:31 +00:00
|
|
|
if (w != null) w.destroy()
|
2016-02-02 10:24:51 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
width: 400,
|
|
|
|
height: 400
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can cancel default download behavior', (done) => {
|
2017-11-23 22:22:31 +00:00
|
|
|
const mockFile = Buffer.alloc(1024)
|
2016-03-25 20:03:49 +00:00
|
|
|
const contentDisposition = 'inline; filename="mockFile.txt"'
|
2017-10-27 20:45:58 +00:00
|
|
|
const downloadServer = http.createServer((req, res) => {
|
2016-02-02 10:24:51 +00:00
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Length': mockFile.length,
|
|
|
|
'Content-Type': 'application/plain',
|
|
|
|
'Content-Disposition': contentDisposition
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
res.end(mockFile)
|
|
|
|
downloadServer.close()
|
|
|
|
})
|
2016-02-02 10:24:51 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
downloadServer.listen(0, '127.0.0.1', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
const port = downloadServer.address().port
|
2017-10-27 20:45:58 +00:00
|
|
|
const url = `http://127.0.0.1:${port}/`
|
2016-02-02 10:24:51 +00:00
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
ipcRenderer.sendSync('set-download-option', false, true)
|
|
|
|
w.loadURL(url)
|
2017-10-27 20:45:58 +00:00
|
|
|
ipcRenderer.once('download-error', (event, downloadUrl, filename, error) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(downloadUrl, url)
|
|
|
|
assert.strictEqual(filename, 'mockFile.txt')
|
|
|
|
assert.strictEqual(error, 'Object has been destroyed')
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-02-02 10:24:51 +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) => {
|
|
|
|
assert.strictEqual(state, 'completed')
|
|
|
|
assert.strictEqual(filename, 'mock.pdf')
|
2018-07-10 11:38:11 +00:00
|
|
|
assert.ok(path.isAbsolute(savePath))
|
|
|
|
assert.ok(isPathEqual(savePath, path.join(__dirname, 'fixtures', 'mock.pdf')))
|
2018-02-15 19:39:52 +00:00
|
|
|
if (isCustom) {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(url, `${protocolName}://item`)
|
2018-02-15 19:39:52 +00:00
|
|
|
} else {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(url, `http://127.0.0.1:${port}/`)
|
2018-02-15 19:39:52 +00:00
|
|
|
}
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(mimeType, 'application/pdf')
|
|
|
|
assert.strictEqual(receivedBytes, mockPDF.length)
|
|
|
|
assert.strictEqual(totalBytes, mockPDF.length)
|
|
|
|
assert.strictEqual(disposition, contentDisposition)
|
2016-03-25 20:03:49 +00:00
|
|
|
assert(fs.existsSync(downloadFilePath))
|
|
|
|
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) => {
|
|
|
|
assert.strictEqual(state, 'cancelled')
|
|
|
|
assert.strictEqual(filename, 'mock.pdf')
|
|
|
|
assert.strictEqual(mimeType, 'application/pdf')
|
|
|
|
assert.strictEqual(receivedBytes, 0)
|
|
|
|
assert.strictEqual(totalBytes, mockPDF.length)
|
|
|
|
assert.strictEqual(disposition, 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) => {
|
|
|
|
assert.strictEqual(state, 'cancelled')
|
|
|
|
assert.strictEqual(filename, 'download.pdf')
|
|
|
|
assert.strictEqual(mimeType, 'application/pdf')
|
|
|
|
assert.strictEqual(receivedBytes, 0)
|
|
|
|
assert.strictEqual(totalBytes, mockPDF.length)
|
|
|
|
assert.strictEqual(disposition, 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) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(state, '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.protocol', () => {
|
2016-06-23 04:11:19 +00:00
|
|
|
const partitionName = 'temp'
|
|
|
|
const protocolName = 'sp'
|
|
|
|
const partitionProtocol = session.fromPartition(partitionName).protocol
|
|
|
|
const protocol = session.defaultSession.protocol
|
2017-10-27 20:45:58 +00:00
|
|
|
const handler = (ignoredError, callback) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
callback({ data: 'test', mimeType: 'text/html' })
|
2016-06-23 04:11:19 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
beforeEach((done) => {
|
2016-06-23 04:11:19 +00:00
|
|
|
if (w != null) w.destroy()
|
2016-06-08 14:31:27 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
partition: partitionName
|
|
|
|
}
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
partitionProtocol.registerStringProtocol(protocolName, handler, (error) => {
|
2016-06-29 16:37:10 +00:00
|
|
|
done(error != null ? error : undefined)
|
2016-06-23 04:11:19 +00:00
|
|
|
})
|
2016-06-08 14:31:27 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
afterEach((done) => {
|
2016-06-15 12:12:06 +00:00
|
|
|
partitionProtocol.unregisterProtocol(protocolName, () => done())
|
|
|
|
})
|
|
|
|
|
2019-01-17 17:05:10 +00:00
|
|
|
it('does not affect defaultSession', async () => {
|
|
|
|
const result1 = await protocol.isProtocolHandled(protocolName)
|
|
|
|
assert.strictEqual(result1, false)
|
|
|
|
|
|
|
|
const result2 = await partitionProtocol.isProtocolHandled(protocolName)
|
|
|
|
assert.strictEqual(result2, true)
|
2016-06-08 14:31:27 +00:00
|
|
|
})
|
2016-06-23 04:14:33 +00:00
|
|
|
|
2018-10-31 12:12:32 +00:00
|
|
|
it('handles requests from partition', (done) => {
|
2017-10-27 20:45:58 +00:00
|
|
|
w.webContents.on('did-finish-load', () => done())
|
2016-06-23 04:14:33 +00:00
|
|
|
w.loadURL(`${protocolName}://fake-host`)
|
|
|
|
})
|
2016-06-08 14:31:27 +00:00
|
|
|
})
|
2016-07-21 04:31:08 +00:00
|
|
|
|
2019-03-08 20:51:12 +00:00
|
|
|
describe('ses.setProxy(options)', () => {
|
2018-10-04 18:08:56 +00:00
|
|
|
let server = null
|
|
|
|
let customSession = null
|
|
|
|
|
2018-10-29 13:23:42 +00:00
|
|
|
beforeEach((done) => {
|
2018-10-04 18:08:56 +00:00
|
|
|
customSession = session.fromPartition('proxyconfig')
|
2018-10-29 13:23:42 +00:00
|
|
|
// FIXME(deepak1556): This is just a hack to force
|
|
|
|
// creation of request context which in turn initializes
|
|
|
|
// the network context, can be removed with network
|
|
|
|
// service enabled.
|
2019-03-08 18:26:17 +00:00
|
|
|
customSession.clearHostResolverCache().then(() => done())
|
2018-10-04 18:08:56 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
if (server) {
|
|
|
|
server.close()
|
|
|
|
}
|
|
|
|
if (customSession) {
|
|
|
|
customSession.destroy()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-03-08 20:51:12 +00:00
|
|
|
it('allows configuring proxy settings', async () => {
|
|
|
|
const config = { proxyRules: 'http=myproxy:80' }
|
|
|
|
await customSession.setProxy(config)
|
|
|
|
const proxy = await customSession.resolveProxy('http://example.com/')
|
|
|
|
assert.strictEqual(proxy, 'PROXY myproxy:80')
|
|
|
|
})
|
|
|
|
|
|
|
|
// TODO(codebytere): remove when Promisification is complete
|
|
|
|
it('allows configuring proxy settings (callback)', (done) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
const config = { proxyRules: 'http=myproxy:80' }
|
2019-01-12 11:17:01 +00:00
|
|
|
customSession.setProxy(config, () => {
|
2019-03-08 20:51:12 +00:00
|
|
|
customSession.resolveProxy('http://example.com/', proxy => {
|
2019-01-12 11:17:01 +00:00
|
|
|
assert.strictEqual(proxy, 'PROXY myproxy:80')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-03-08 20:51:12 +00:00
|
|
|
it('allows removing the implicit bypass rules for localhost', async () => {
|
2019-01-12 11:17:01 +00:00
|
|
|
const config = {
|
|
|
|
proxyRules: 'http=myproxy:80',
|
|
|
|
proxyBypassRules: '<-loopback>'
|
|
|
|
}
|
2019-03-08 20:51:12 +00:00
|
|
|
|
|
|
|
await customSession.setProxy(config)
|
|
|
|
const proxy = await customSession.resolveProxy('http://localhost')
|
|
|
|
assert.strictEqual(proxy, 'PROXY myproxy:80')
|
|
|
|
})
|
|
|
|
|
|
|
|
// TODO(codebytere): remove when Promisification is complete
|
|
|
|
it('allows removing the implicit bypass rules for localhost (callback)', (done) => {
|
|
|
|
const config = {
|
|
|
|
proxyRules: 'http=myproxy:80',
|
|
|
|
proxyBypassRules: '<-loopback>'
|
|
|
|
}
|
|
|
|
customSession.setProxy(config).then(() => {
|
|
|
|
customSession.resolveProxy('http://localhost').then(proxy => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(proxy, 'PROXY myproxy:80')
|
2016-07-21 04:31:08 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-03-08 20:51:12 +00:00
|
|
|
it('allows configuring proxy settings with pacScript', async () => {
|
|
|
|
server = http.createServer((req, res) => {
|
|
|
|
const pac = `
|
|
|
|
function FindProxyForURL(url, host) {
|
|
|
|
return "PROXY myproxy:8132";
|
|
|
|
}
|
|
|
|
`
|
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Type': 'application/x-ns-proxy-autoconfig'
|
|
|
|
})
|
|
|
|
res.end(pac)
|
|
|
|
})
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
server.listen(0, '127.0.0.1', async () => {
|
|
|
|
try {
|
|
|
|
const config = { pacScript: `http://127.0.0.1:${server.address().port}` }
|
|
|
|
await customSession.setProxy(config)
|
|
|
|
const proxy = await customSession.resolveProxy('https://google.com')
|
|
|
|
assert.strictEqual(proxy, 'PROXY myproxy:8132')
|
|
|
|
resolve()
|
|
|
|
} catch (error) {
|
|
|
|
reject(error)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// TODO(codebytere): reconfigure when Promisification is complete
|
|
|
|
it('allows configuring proxy settings with pacScript (callback)', (done) => {
|
2018-10-04 18:08:56 +00:00
|
|
|
server = http.createServer((req, res) => {
|
|
|
|
const pac = `
|
|
|
|
function FindProxyForURL(url, host) {
|
|
|
|
return "PROXY myproxy:8132";
|
|
|
|
}
|
|
|
|
`
|
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Type': 'application/x-ns-proxy-autoconfig'
|
|
|
|
})
|
|
|
|
res.end(pac)
|
|
|
|
})
|
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
|
|
const config = { pacScript: `http://127.0.0.1:${server.address().port}` }
|
|
|
|
customSession.setProxy(config, () => {
|
2019-03-08 20:51:12 +00:00
|
|
|
customSession.resolveProxy('https://google.com', proxy => {
|
2018-10-04 18:08:56 +00:00
|
|
|
assert.strictEqual(proxy, 'PROXY myproxy:8132')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-03-08 20:51:12 +00:00
|
|
|
it('allows bypassing proxy settings', async () => {
|
|
|
|
const config = {
|
|
|
|
proxyRules: 'http=myproxy:80',
|
|
|
|
proxyBypassRules: '<local>'
|
|
|
|
}
|
|
|
|
await customSession.setProxy(config)
|
|
|
|
const proxy = await customSession.resolveProxy('http://example/')
|
|
|
|
assert.strictEqual(proxy, 'DIRECT')
|
|
|
|
})
|
|
|
|
|
|
|
|
// TODO(codebytere): remove when Promisification is complete
|
|
|
|
it('allows bypassing proxy settings (callback)', (done) => {
|
2016-07-21 04:31:08 +00:00
|
|
|
const config = {
|
|
|
|
proxyRules: 'http=myproxy:80',
|
|
|
|
proxyBypassRules: '<local>'
|
|
|
|
}
|
2018-10-04 18:08:56 +00:00
|
|
|
customSession.setProxy(config, () => {
|
2019-03-08 20:51:12 +00:00
|
|
|
customSession.resolveProxy('http://example/', proxy => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(proxy, 'DIRECT')
|
2016-07-21 04:31:08 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-08-23 05:59:21 +00:00
|
|
|
|
2019-03-14 15:11:01 +00:00
|
|
|
describe('ses.getBlobData()', () => {
|
2017-10-27 20:45:58 +00:00
|
|
|
it('returns blob data for uuid', (done) => {
|
2019-01-29 07:11:01 +00:00
|
|
|
const scheme = 'cors-blob'
|
2016-08-23 05:59:21 +00:00
|
|
|
const protocol = session.defaultSession.protocol
|
2017-10-27 20:45:58 +00:00
|
|
|
const url = `${scheme}://host`
|
|
|
|
before(() => {
|
2016-08-23 05:59:21 +00:00
|
|
|
if (w != null) w.destroy()
|
2018-09-13 16:10:51 +00:00
|
|
|
w = new BrowserWindow({ show: false })
|
2016-08-23 05:59:21 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
after((done) => {
|
2016-08-23 05:59:21 +00:00
|
|
|
protocol.unregisterProtocol(scheme, () => {
|
|
|
|
closeWindow(w).then(() => {
|
|
|
|
w = null
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
const postData = JSON.stringify({
|
|
|
|
type: 'blob',
|
|
|
|
value: 'hello'
|
|
|
|
})
|
|
|
|
const content = `<html>
|
|
|
|
<script>
|
|
|
|
let fd = new FormData();
|
|
|
|
fd.append('file', new Blob(['${postData}'], {type:'application/json'}));
|
|
|
|
fetch('${url}', {method:'POST', body: fd });
|
|
|
|
</script>
|
|
|
|
</html>`
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
protocol.registerStringProtocol(scheme, (request, callback) => {
|
2016-08-23 05:59:21 +00:00
|
|
|
if (request.method === 'GET') {
|
2018-09-13 16:10:51 +00:00
|
|
|
callback({ data: content, mimeType: 'text/html' })
|
2016-08-23 05:59:21 +00:00
|
|
|
} else if (request.method === 'POST') {
|
2018-10-02 01:56:31 +00:00
|
|
|
const uuid = request.uploadData[1].blobUUID
|
2016-08-23 05:59:21 +00:00
|
|
|
assert(uuid)
|
2019-03-14 15:11:01 +00:00
|
|
|
session.defaultSession.getBlobData(uuid).then(result => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(result.toString(), postData)
|
2016-08-23 05:59:21 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
}
|
2017-10-27 20:45:58 +00:00
|
|
|
}, (error) => {
|
2016-08-23 05:59:21 +00:00
|
|
|
if (error) return done(error)
|
|
|
|
w.loadURL(url)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-10-24 07:12:49 +00:00
|
|
|
|
2019-01-25 22:23:24 +00:00
|
|
|
describe('ses.setCertificateVerifyProc(callback)', (done) => {
|
2017-10-27 20:45:58 +00:00
|
|
|
let server = null
|
2016-10-24 07:12:49 +00:00
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
beforeEach((done) => {
|
|
|
|
const certPath = path.join(__dirname, 'fixtures', 'certificates')
|
|
|
|
const options = {
|
2016-10-24 07:12:49 +00:00
|
|
|
key: fs.readFileSync(path.join(certPath, 'server.key')),
|
|
|
|
cert: fs.readFileSync(path.join(certPath, 'server.pem')),
|
|
|
|
ca: [
|
|
|
|
fs.readFileSync(path.join(certPath, 'rootCA.pem')),
|
|
|
|
fs.readFileSync(path.join(certPath, 'intermediateCA.pem'))
|
|
|
|
],
|
|
|
|
requestCert: true,
|
|
|
|
rejectUnauthorized: false
|
|
|
|
}
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
server = https.createServer(options, (req, res) => {
|
2016-10-24 07:12:49 +00:00
|
|
|
res.writeHead(200)
|
|
|
|
res.end('<title>hello</title>')
|
|
|
|
})
|
|
|
|
server.listen(0, '127.0.0.1', done)
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
afterEach(() => {
|
2016-10-24 07:12:49 +00:00
|
|
|
session.defaultSession.setCertificateVerifyProc(null)
|
|
|
|
server.close()
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('accepts the request when the callback is called with 0', (done) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
session.defaultSession.setCertificateVerifyProc(({ hostname, certificate, verificationResult, errorCode }, callback) => {
|
2017-02-08 21:33:07 +00:00
|
|
|
assert(['net::ERR_CERT_AUTHORITY_INVALID', 'net::ERR_CERT_COMMON_NAME_INVALID'].includes(verificationResult), verificationResult)
|
2017-10-16 09:17:21 +00:00
|
|
|
assert([-202, -200].includes(errorCode), errorCode)
|
2016-11-09 13:05:46 +00:00
|
|
|
callback(0)
|
2016-10-24 07:12:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
w.webContents.once('did-finish-load', () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(w.webContents.getTitle(), 'hello')
|
2016-10-24 07:12:49 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.loadURL(`https://127.0.0.1:${server.address().port}`)
|
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
it('rejects the request when the callback is called with -2', (done) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
session.defaultSession.setCertificateVerifyProc(({ hostname, certificate, verificationResult }, callback) => {
|
|
|
|
assert.strictEqual(hostname, '127.0.0.1')
|
|
|
|
assert.strictEqual(certificate.issuerName, 'Intermediate CA')
|
|
|
|
assert.strictEqual(certificate.subjectName, 'localhost')
|
|
|
|
assert.strictEqual(certificate.issuer.commonName, 'Intermediate CA')
|
|
|
|
assert.strictEqual(certificate.subject.commonName, 'localhost')
|
|
|
|
assert.strictEqual(certificate.issuerCert.issuer.commonName, 'Root CA')
|
|
|
|
assert.strictEqual(certificate.issuerCert.subject.commonName, 'Intermediate CA')
|
|
|
|
assert.strictEqual(certificate.issuerCert.issuerCert.issuer.commonName, 'Root CA')
|
|
|
|
assert.strictEqual(certificate.issuerCert.issuerCert.subject.commonName, 'Root CA')
|
|
|
|
assert.strictEqual(certificate.issuerCert.issuerCert.issuerCert, undefined)
|
2017-02-08 21:33:07 +00:00
|
|
|
assert(['net::ERR_CERT_AUTHORITY_INVALID', 'net::ERR_CERT_COMMON_NAME_INVALID'].includes(verificationResult), verificationResult)
|
2016-11-09 13:05:46 +00:00
|
|
|
callback(-2)
|
2016-10-24 07:12:49 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 20:45:58 +00:00
|
|
|
const url = `https://127.0.0.1:${server.address().port}`
|
|
|
|
w.webContents.once('did-finish-load', () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(w.webContents.getTitle(), url)
|
2016-10-24 07:12:49 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.loadURL(url)
|
|
|
|
})
|
|
|
|
})
|
2016-11-24 15:16:39 +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) => {
|
|
|
|
assert.strictEqual(state, 'interrupted')
|
|
|
|
assert.deepStrictEqual(urlChain, ['http://127.0.0.1/'])
|
|
|
|
assert.strictEqual(mimeType, 'application/pdf')
|
|
|
|
assert.strictEqual(receivedBytes, 0)
|
|
|
|
assert.strictEqual(totalBytes, 5242880)
|
|
|
|
assert.strictEqual(savePath, 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 {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(state, 'completed')
|
|
|
|
assert.strictEqual(filename, 'logo.png')
|
|
|
|
assert.strictEqual(savePath, downloadFilePath)
|
|
|
|
assert.strictEqual(url, downloadUrl)
|
|
|
|
assert.strictEqual(mimeType, 'image/png')
|
|
|
|
assert.strictEqual(receivedBytes, 14022)
|
|
|
|
assert.strictEqual(totalBytes, 14022)
|
2016-11-24 15:16:39 +00:00
|
|
|
assert(fs.existsSync(downloadFilePath))
|
|
|
|
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
|
|
|
|
2019-03-09 02:41:42 +00:00
|
|
|
describe('ses.clearAuthCache(options)', () => {
|
2017-10-27 20:45:58 +00:00
|
|
|
it('can clear http auth info from cache', (done) => {
|
2016-11-30 20:02:17 +00:00
|
|
|
const ses = session.fromPartition('auth-cache')
|
2017-10-27 20:45:58 +00:00
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
const credentials = auth(req)
|
2016-11-30 20:02:17 +00:00
|
|
|
if (!credentials || credentials.name !== 'test' || credentials.pass !== 'test') {
|
|
|
|
res.statusCode = 401
|
|
|
|
res.setHeader('WWW-Authenticate', 'Basic realm="Restricted"')
|
|
|
|
res.end()
|
|
|
|
} else {
|
2016-12-01 12:40:02 +00:00
|
|
|
res.end('authenticated')
|
2016-11-30 20:02:17 +00:00
|
|
|
}
|
|
|
|
})
|
2019-03-09 02:41:42 +00:00
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
|
|
const port = server.address().port
|
|
|
|
function issueLoginRequest (attempt = 1) {
|
|
|
|
if (attempt > 2) {
|
|
|
|
server.close()
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
const request = net.request({
|
|
|
|
url: `http://127.0.0.1:${port}`,
|
|
|
|
session: ses
|
|
|
|
})
|
|
|
|
request.on('login', (info, callback) => {
|
|
|
|
attempt += 1
|
|
|
|
assert.strictEqual(info.scheme, 'basic')
|
|
|
|
assert.strictEqual(info.realm, 'Restricted')
|
|
|
|
callback('test', 'test')
|
|
|
|
})
|
|
|
|
request.on('response', (response) => {
|
|
|
|
let data = ''
|
|
|
|
response.pause()
|
|
|
|
response.on('data', (chunk) => {
|
|
|
|
data += chunk
|
|
|
|
})
|
|
|
|
response.on('end', () => {
|
|
|
|
assert.strictEqual(data, 'authenticated')
|
|
|
|
ses.clearAuthCache({ type: 'password' }).then(() => {
|
|
|
|
issueLoginRequest(attempt)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
response.on('error', (error) => { done(error) })
|
|
|
|
response.resume()
|
|
|
|
})
|
|
|
|
// Internal api to bypass cache for testing.
|
|
|
|
request.urlRequest._setLoadFlags(1 << 1)
|
|
|
|
request.end()
|
|
|
|
}
|
|
|
|
issueLoginRequest()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// TODO(codebytere): remove when promisification complete
|
|
|
|
it('can clear http auth info from cache (callback)', (done) => {
|
|
|
|
const ses = session.fromPartition('auth-cache')
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
const credentials = auth(req)
|
|
|
|
if (!credentials || credentials.name !== 'test' || credentials.pass !== 'test') {
|
|
|
|
res.statusCode = 401
|
|
|
|
res.setHeader('WWW-Authenticate', 'Basic realm="Restricted"')
|
|
|
|
res.end()
|
|
|
|
} else {
|
|
|
|
res.end('authenticated')
|
|
|
|
}
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
server.listen(0, '127.0.0.1', () => {
|
2016-11-30 20:02:17 +00:00
|
|
|
const port = server.address().port
|
|
|
|
function issueLoginRequest (attempt = 1) {
|
|
|
|
if (attempt > 2) {
|
|
|
|
server.close()
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
const request = net.request({
|
|
|
|
url: `http://127.0.0.1:${port}`,
|
|
|
|
session: ses
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
request.on('login', (info, callback) => {
|
|
|
|
attempt += 1
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(info.scheme, 'basic')
|
|
|
|
assert.strictEqual(info.realm, 'Restricted')
|
2016-11-30 20:02:17 +00:00
|
|
|
callback('test', 'test')
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
request.on('response', (response) => {
|
2016-11-30 20:02:17 +00:00
|
|
|
let data = ''
|
2016-12-01 12:40:02 +00:00
|
|
|
response.pause()
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('data', (chunk) => {
|
2016-11-30 20:02:17 +00:00
|
|
|
data += chunk
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('end', () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(data, 'authenticated')
|
|
|
|
ses.clearAuthCache({ type: 'password' }, () => {
|
2016-11-30 20:02:17 +00:00
|
|
|
issueLoginRequest(attempt)
|
|
|
|
})
|
|
|
|
})
|
2017-10-27 20:45:58 +00:00
|
|
|
response.on('error', (error) => { done(error) })
|
2016-12-01 12:40:02 +00:00
|
|
|
response.resume()
|
2016-11-30 20:02:17 +00:00
|
|
|
})
|
|
|
|
// Internal api to bypass cache for testing.
|
|
|
|
request.urlRequest._setLoadFlags(1 << 1)
|
|
|
|
request.end()
|
|
|
|
}
|
|
|
|
issueLoginRequest()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
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) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(e.channel, 'message')
|
|
|
|
assert.deepStrictEqual(e.args, ['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
|
|
|
})
|