test_server_time.html (2255B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>Test for cookie expire attribute with server-time</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <script src="cookiesHelper.js"></script> 7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 8 </head> 9 <body> 10 <script type="application/javascript"> 11 12 async function testServerTime() { 13 await SpecialPowers.pushPrefEnv({ 14 set: [["network.cookie.cookieBehavior", 1]], 15 }); 16 17 const currentTime = Date.now(); 18 const DAY = 24 * 60 * 60 * 1000; 19 20 const tests = [ 21 // Pref on: This cookie is already expired. 22 // Pref off: This cookie is still expired. 23 { cookieName: "test1", 24 serverTime: "", 25 expireTime: currentTime - 24*60*60*1000, 26 expectedCookieWithPrefOn: "", 27 expectedCookieWithPrefOff: "" }, 28 29 // Pref on: This cookie is not expired because the server time is in the past. 30 // Pref off: This cookie is expired. 31 { cookieName: "test2", 32 serverTime: currentTime - 3 * DAY, 33 expireTime: currentTime - DAY, 34 expectedCookieWithPrefOn: "test2=foo", 35 expectedCookieWithPrefOff: "" }, 36 37 // Pref on: This cookie is expired because the server time is in the future. 38 // Pref off: This cookie is not expired. 39 { cookieName: "test3", 40 serverTime: currentTime + 3 * DAY, 41 expireTime: currentTime + DAY, 42 expectedCookieWithPrefOn: "" , 43 expectedCookieWithPrefOff: "test3=foo" }, 44 ] 45 46 for (const test of tests) { 47 await cleanupData(); 48 is(document.cookie, "", "No cookies"); 49 50 await SpecialPowers.pushPrefEnv({ 51 set: [["network.cookie.useServerTime", true]], 52 }); 53 54 await fetch(`server_time.sjs?${test.cookieName}&${test.serverTime}&${test.expireTime}&${Math.random()}`).then(a => a.text()); 55 is(document.cookie, test.expectedCookieWithPrefOn, "Pref on"); 56 57 await SpecialPowers.pushPrefEnv({ 58 set: [["network.cookie.useServerTime", false]], 59 }); 60 61 await fetch(`server_time.sjs?${test.cookieName}&${test.serverTime}&${test.expireTime}&${Math.random()}`).then(a => a.text()); 62 is(document.cookie, test.expectedCookieWithPrefOff, "Pref off"); 63 } 64 65 SimpleTest.finish(); 66 } 67 68 SimpleTest.waitForExplicitFinish(); 69 testServerTime(); 70 71 </script> 72 </body> 73 </html>