Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add test for cookie store persistance
- Loading branch information
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "electron-cookie-app", | ||
"main": "main.js" | ||
} |