| Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 1 | define(['session'], function (sessionClass) { |
| 2 | |
| Akron | 4fd6e83 | 2025-10-22 09:32:32 +0200 | [diff] [blame] | 3 | // Check once if cookies are writable in this environment (e.g., not on file:// in Chrome) |
| 4 | var _canWriteCookies = (function () { |
| 5 | try { |
| 6 | var testName = 'ck_test_' + Math.random().toString(36).slice(2); |
| 7 | document.cookie = testName + '=1; SameSite=Lax; path=/'; |
| 8 | var ok = document.cookie.indexOf(testName + '=1') !== -1; |
| 9 | if (ok) { |
| 10 | document.cookie = testName + '=; Max-Age=0; path=/'; |
| 11 | } |
| 12 | return ok; |
| 13 | } |
| 14 | catch (e) { |
| 15 | return false; |
| 16 | } |
| 17 | })(); |
| 18 | |
| Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 19 | beforeEach( |
| 20 | function () { |
| 21 | document.cookie.split(';').forEach( |
| 22 | function (i) { |
| 23 | var pair = i.split('='); |
| 24 | var name = pair[0].trim().toLowerCase(); |
| 25 | |
| 26 | } |
| 27 | ); |
| 28 | } |
| 29 | ); |
| 30 | |
| 31 | describe('KorAP.Session', function () { |
| 32 | it('should be initializable', function () { |
| 33 | let s = sessionClass.create(); |
| 34 | |
| 35 | expect(s).toBeTruthy(); |
| 36 | |
| 37 | expect(s.toString().startsWith('korap=')).toBeTruthy(); |
| 38 | |
| 39 | s = sessionClass.create('kalamar'); |
| 40 | |
| 41 | expect(s).toBeTruthy(); |
| 42 | |
| 43 | expect(s.toString().startsWith('kalamar=')).toBeTruthy(); |
| 44 | }); |
| 45 | |
| 46 | it('should settable and gettable', function () { |
| 47 | let s = sessionClass.create('koraptest'); |
| 48 | s.set("test1", "works"); |
| 49 | |
| 50 | expect(s.get("test1")).toEqual("works"); |
| 51 | |
| 52 | s.set("test2", "\"wor}ks\""); |
| 53 | |
| 54 | expect(s.get("test1")).toEqual("works"); |
| 55 | expect(s.get("test2")).toEqual("\"wor}ks\""); |
| 56 | |
| 57 | expect(s.toString().includes("test1")).toBeTruthy(); |
| 58 | expect(s.toString().includes("test2")).toBeTruthy(); |
| 59 | expect(s.toString().includes("works")).toBeTruthy(); |
| 60 | expect(s.toString().includes("%5C%22wor%7Dks%5C%22")).toBeTruthy(); |
| 61 | }); |
| 62 | |
| 63 | it('should write to cookie', function () { |
| Akron | 4fd6e83 | 2025-10-22 09:32:32 +0200 | [diff] [blame] | 64 | if (!_canWriteCookies) { |
| 65 | pending('Cookies are not writable in this environment'); |
| 66 | return; |
| 67 | } |
| Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 68 | let s = sessionClass.create('koraptest'); |
| 69 | s.clear(); |
| 70 | expect(s.toString().includes("koraptest=%7B%7D;")).toBeTruthy(); |
| 71 | expect(document.cookie.includes("koraptest=")).toBeTruthy(); |
| 72 | s.set("test3", "works"); |
| 73 | expect(s.toString().includes("koraptest=%7B%7D;")).toBeFalsy(); |
| 74 | expect(s.toString().includes("koraptest=%7B%22test3%22%3A%22works%22%7D")).toBeTruthy(); |
| 75 | expect(document.cookie.includes("koraptest=%7B%22test3%22%3A%22works%22%7D")).toBeTruthy(); |
| 76 | s.clear(); |
| 77 | expect(document.cookie.includes("koraptest=")).toBeTruthy(); |
| Helge | 9c0a19c | 2024-10-08 13:14:44 +0200 | [diff] [blame] | 78 | expect(s.toString()).toEqual("koraptest=%7B%7D;SameSite=Lax;path=/"); |
| 79 | }); |
| 80 | |
| 81 | /* |
| 82 | * The cookie path should by default be '/'. The path should be settable. |
| 83 | */ |
| 84 | it('cookie path should have default and be settable', function(){ |
| 85 | //cookie should be created with the default path=/ |
| 86 | let c = sessionClass.create("cookwithoutp"); |
| 87 | c.clear(); |
| 88 | expect(c.toString().includes("path=/")).toBeTruthy(); |
| 89 | //the path should be settable |
| 90 | let d = sessionClass.create("cookwithp", "/instance/blub"); |
| 91 | d.clear(); |
| 92 | expect(d.toString().includes("path=/instance/blub")).toBeTruthy(); |
| 93 | }); |
| 94 | |
| Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 95 | }) |
| Helge | 9c0a19c | 2024-10-08 13:14:44 +0200 | [diff] [blame] | 96 | |
| Akron | 7650eaa | 2020-10-19 17:10:30 +0200 | [diff] [blame] | 97 | }); |