Skip to content

Commit

Permalink
Refactor addCookie examples
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed Sep 7, 2023
1 parent 07c3c2a commit 771ce83
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions examples/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,37 @@ export default async function () {

// add some cookies to the browser context
const unixTimeSinceEpoch = Math.round(new Date() / 1000);
const dayAfter = unixTimeSinceEpoch+60*60*24;
const dayBefore = unixTimeSinceEpoch-60*60*24;
context.addCookies([{name: 'testcookie', value: '1', sameSite: 'Strict', domain: '127.0.0.1', path: '/'}]);
context.addCookies([{name: 'testcookie2', value: '2', sameSite: 'Lax', domain: '127.0.0.1', path: '/', expires: dayAfter}]);
// won't set this cookie because it's expired
context.addCookies([{name: 'testcookie3', value: '3', sameSite: 'Lax', domain: '127.0.0.1', path: '/', expires: dayBefore}]);
const day = 60*60*24;
const dayAfter = unixTimeSinceEpoch+day;
const dayBefore = unixTimeSinceEpoch-day;
context.addCookies([
// this cookie expires at the end of the session
{
name: 'testcookie',
value: '1',
sameSite: 'Strict',
domain: '127.0.0.1',
path: '/',
},
// this cookie expires in a day
{
name: 'testcookie2',
value: '2',
sameSite: 'Lax',
domain: '127.0.0.1',
path: '/',
expires: dayAfter,
},
// this cookie expires in the past, so it will be removed.
{
name: 'testcookie3',
value: '3',
sameSite: 'Lax',
domain: '127.0.0.1',
path: '/',
expires: dayBefore
}
]);

check(context.cookies().length, {
'number of cookies should be 2': n => n === 2,
Expand Down

0 comments on commit 771ce83

Please sign in to comment.