Skip to content
Permalink
Browse files

test: add test for cookie store persistance

  • Loading branch information...
brenca committed Nov 26, 2018
1 parent ef14473 commit 76baa646e6fb25ae0a8d4ce93263ecf64e21b2ba
Showing with 102 additions and 0 deletions.
  1. +28 −0 spec/api-session-spec.js
  2. +70 −0 spec/fixtures/api/cookie-app/main.js
  3. +4 −0 spec/fixtures/api/cookie-app/package.json
@@ -6,6 +6,7 @@ const path = require('path')
const fs = require('fs')
const send = require('send')
const auth = require('basic-auth')
const ChildProcess = require('child_process')
const { closeWindow } = require('./window-helpers')

const { ipcRenderer, remote } = require('electron')
@@ -213,6 +214,33 @@ describe('session module', () => {
})
})
})

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 } }
)

appProcess.stdout.on('data', (data) => { output += data })
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')
})
})

describe('ses.clearStorageData(options)', () => {
@@ -0,0 +1,70 @@
const { app, session } = require('electron')

app.on('ready', async function () {
const url = 'http://foo.bar'
const persistentSession = session.fromPartition('persist:ence-test')

const set = () => {
return new Promise((resolve, reject) => {
persistentSession.cookies.set({
url,
name: 'test',
value: 'true',
expirationDate: Date.now() + 60000
}, error => {
if (error) {
reject(error)
} else {
resolve()
}
})
})
}

const get = () => {
return new Promise((resolve, reject) => {
persistentSession.cookies.get({ url }, (error, list) => {
if (error) {
reject(error)
} else {
resolve(list)
}
})
})
}

const maybeRemove = (pred) => {
return new Promise((resolve, reject) => {
if (pred()) {
persistentSession.cookies.remove(url, 'test', error => {
if (error) {
reject(error)
} else {
resolve()
}
})
} else {
resolve()
}
})
}

try {
await maybeRemove(() => process.env.PHASE === 'one')
const one = await get()
await set()
const two = await get()
await maybeRemove(() => process.env.PHASE === 'two')
const three = await get()

process.stdout.write(`${one.length}${two.length}${three.length}`)
} catch (e) {
process.stdout.write('ERROR')
} finally {
process.stdout.end()

setImmediate(() => {
app.quit()
})
}
})
@@ -0,0 +1,4 @@
{
"name": "electron-cookie-app",
"main": "main.js"
}

0 comments on commit 76baa64

Please sign in to comment.
You can’t perform that action at this time.