Skip to content

Commit

Permalink
test: add test for cookie store persistance
Browse files Browse the repository at this point in the history
  • Loading branch information
brenca committed Nov 27, 2018
1 parent ef14473 commit 76baa64
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
28 changes: 28 additions & 0 deletions spec/api-session-spec.js
Expand Up @@ -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')
Expand Down Expand Up @@ -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)', () => {
Expand Down
70 changes: 70 additions & 0 deletions spec/fixtures/api/cookie-app/main.js
@@ -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()
})
}
})
4 changes: 4 additions & 0 deletions spec/fixtures/api/cookie-app/package.json
@@ -0,0 +1,4 @@
{
"name": "electron-cookie-app",
"main": "main.js"
}

0 comments on commit 76baa64

Please sign in to comment.