Skip to content

Commit

Permalink
Makes cookie path overridable
Browse files Browse the repository at this point in the history
Change-Id: I3244103a72fbb994597af08df233f0237eb28f18
  • Loading branch information
hebasta committed Oct 8, 2024
1 parent 479ddeb commit 9c0a19c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
20 changes: 18 additions & 2 deletions dev/js/spec/sessionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,23 @@ define(['session'], function (sessionClass) {
expect(document.cookie.includes("koraptest=%7B%22test3%22%3A%22works%22%7D")).toBeTruthy();
s.clear();
expect(document.cookie.includes("koraptest=")).toBeTruthy();
expect(s.toString()).toEqual("koraptest=%7B%7D;SameSite=Lax");
});
expect(s.toString()).toEqual("koraptest=%7B%7D;SameSite=Lax;path=/");
});

/*
* The cookie path should by default be '/'. The path should be settable.
*/
it('cookie path should have default and be settable', function(){
//cookie should be created with the default path=/
let c = sessionClass.create("cookwithoutp");
c.clear();
expect(c.toString().includes("path=/")).toBeTruthy();
//the path should be settable
let d = sessionClass.create("cookwithp", "/instance/blub");
d.clear();
expect(d.toString().includes("path=/instance/blub")).toBeTruthy();
});

})

});
8 changes: 5 additions & 3 deletions dev/js/src/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ define({
/**
* Create a new session.
* Expects a name or defaults to 'korap'
* Expects a path or defaults to '/'
*/
create : function (name = 'korap') {
create : function (name = 'korap', path ="/") {
const obj = Object.create(this);
obj._name = name.toLowerCase();
obj._path = path;
obj._hash = {};
obj._parse();
return obj;
Expand All @@ -38,7 +40,7 @@ define({
this._hash[key] = value;
this._store();
},


/**
* Clears the session by removing the cookie
Expand All @@ -63,7 +65,7 @@ define({
var date = new Date();
date.setYear(date.getFullYear() + 1);
*/
return this._name + '=' + encodeURIComponent(JSON.stringify(this._hash)) + ';SameSite=Lax' + ';path=/';
return this._name + '=' + encodeURIComponent(JSON.stringify(this._hash)) + ';SameSite=Lax' + ';path='+this._path;
},


Expand Down

0 comments on commit 9c0a19c

Please sign in to comment.