Rebase python tests.
[WebKit-https.git] / LayoutTests / http / tests / cookies / capped-lifetime-for-cookie-set-in-js.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4     <script src="/js-test-resources/js-test.js"></script>
5     <script src="resources/cookie-utilities.js"></script>
6 </head>
7 <body>
8 <script>
9     description("Check that cookies created by JavaScript with max-age or expiry longer than a week get capped to a week.");
10     jsTestIsAsync = true;
11
12     let passedTests = 0;
13     function checkThatCookieDoesNotExpireAfter(cookieData, maxAgeInSeconds) {
14         let now = new Date();
15         let maxExpiryDateInMilliseconds = now.getTime() + (maxAgeInSeconds * 1000);
16
17         if (maxExpiryDateInMilliseconds > cookieData["expires"])
18             ++passedTests;
19         else
20             testFailed("Cookie named " + cookieData["name"] + " expires in more than " + maxAgeInSeconds + " seconds.");
21     }
22
23     const twoDaysInSeconds = 2 * 24 * 60 * 60;
24     const shortLivedCookieMaxAge = { name : "shortLivedCookieMaxAge", lifetime : "Max-Age=" + twoDaysInSeconds + ";" };
25     document.cookie = shortLivedCookieMaxAge.name + "=foobar; " + shortLivedCookieMaxAge.lifetime + " path=/";
26
27     const twoDaysAsExpiresDate = createExpiresDateFromMaxAge(twoDaysInSeconds);
28     const shortLivedCookieExpires = { name : "shortLivedCookieExpires", lifetime : "Expires=" + twoDaysAsExpiresDate + ";" };
29     document.cookie = shortLivedCookieExpires.name + "=foobar; " + shortLivedCookieExpires.lifetime + " path=/";
30
31     const oneWeekInSeconds = 7 * 24 * 60 * 60;
32     const twoWeeksInSeconds = 2 * oneWeekInSeconds;
33     const longLivedCookieMaxAge = { name : "longLivedCookieMaxAge", lifetime : "Max-Age=" + twoWeeksInSeconds + ";" };
34     document.cookie = longLivedCookieMaxAge.name + "=foobar; " + longLivedCookieMaxAge.lifetime + " path=/";
35
36     const twoWeeksAsExpiresDate = createExpiresDateFromMaxAge(twoWeeksInSeconds);
37     const longLivedCookieExpires = { name : "longLivedCookieExpires", lifetime : "Expires=" + twoWeeksAsExpiresDate + ";" };
38     document.cookie = longLivedCookieExpires.name + "=foobar; " + longLivedCookieExpires.lifetime + " path=/";
39
40     const overTwoDaysInSeconds = twoDaysInSeconds + 30;
41     const overOneWeekInSeconds = oneWeekInSeconds + 30;
42     if (internals) {
43         let cookies = internals.getCookies();
44         if (!cookies.length)
45             testFailed("No cookies found.");
46         for (let cookie of cookies) {
47             switch (cookie.name) {
48                 case shortLivedCookieMaxAge.name:
49                     checkThatCookieDoesNotExpireAfter(cookie, overTwoDaysInSeconds);
50                     break;
51                 case shortLivedCookieExpires.name:
52                     checkThatCookieDoesNotExpireAfter(cookie, overTwoDaysInSeconds);
53                     break;
54                 case longLivedCookieMaxAge.name:
55                     checkThatCookieDoesNotExpireAfter(cookie, overOneWeekInSeconds);
56                     break;
57                 case longLivedCookieExpires.name:
58                     checkThatCookieDoesNotExpireAfter(cookie, overOneWeekInSeconds);
59                     break;
60             }
61         }
62         if (passedTests === 4) {
63             testPassed("The two short-lived cookies don't expire after more than " + overTwoDaysInSeconds + " seconds.");
64             testPassed("The two long-lived cookies don't expire after more than " + overOneWeekInSeconds + " seconds.");
65         } else
66             testFailed("At least one cookie's expiry attribute was beyond the test thresholds.");
67     } else
68         testFailed("No internals object.");
69
70     finishJSTest();
71 </script>
72 </body>
73 </html>