Rebase python tests.
[WebKit-https.git] / LayoutTests / http / tests / resourceLoadStatistics / 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="/cookies/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     if (internals)
13         internals.setResourceLoadStatisticsEnabled(true);
14
15     let passedTests = 0;
16     function checkThatCookieDoesNotExpireAfter(cookieData, maxAgeInSeconds) {
17         let now = new Date();
18         let maxExpiryDateInMilliseconds = now.getTime() + (maxAgeInSeconds * 1000);
19
20         if (maxExpiryDateInMilliseconds > cookieData["expires"])
21             ++passedTests;
22         else
23             testFailed("Cookie named " + cookieData["name"] + " expires in more than " + maxAgeInSeconds + " seconds.");
24     }
25
26     const twoDaysInSeconds = 2 * 24 * 60 * 60;
27     const shortLivedCookieMaxAge = { name : "shortLivedCookieMaxAge", lifetime : "Max-Age=" + twoDaysInSeconds + ";" };
28     document.cookie = shortLivedCookieMaxAge.name + "=foobar; " + shortLivedCookieMaxAge.lifetime + " path=/";
29
30     const twoDaysAsExpiresDate = createExpiresDateFromMaxAge(twoDaysInSeconds);
31     const shortLivedCookieExpires = { name : "shortLivedCookieExpires", lifetime : "Expires=" + twoDaysAsExpiresDate + ";" };
32     document.cookie = shortLivedCookieExpires.name + "=foobar; " + shortLivedCookieExpires.lifetime + " path=/";
33
34     const oneWeekInSeconds = 7 * 24 * 60 * 60;
35     const twoWeeksInSeconds = 2 * oneWeekInSeconds;
36     const longLivedCookieMaxAge = { name : "longLivedCookieMaxAge", lifetime : "Max-Age=" + twoWeeksInSeconds + ";" };
37     document.cookie = longLivedCookieMaxAge.name + "=foobar; " + longLivedCookieMaxAge.lifetime + " path=/";
38
39     const twoWeeksAsExpiresDate = createExpiresDateFromMaxAge(twoWeeksInSeconds);
40     const longLivedCookieExpires = { name : "longLivedCookieExpires", lifetime : "Expires=" + twoWeeksAsExpiresDate + ";" };
41     document.cookie = longLivedCookieExpires.name + "=foobar; " + longLivedCookieExpires.lifetime + " path=/";
42
43     const overTwoDaysInSeconds = twoDaysInSeconds + 30;
44     const overOneWeekInSeconds = oneWeekInSeconds + 30;
45     if (internals) {
46         let cookies = internals.getCookies();
47         if (!cookies.length)
48             testFailed("No cookies found.");
49         for (let cookie of cookies) {
50             switch (cookie.name) {
51                 case shortLivedCookieMaxAge.name:
52                     checkThatCookieDoesNotExpireAfter(cookie, overTwoDaysInSeconds);
53                     break;
54                 case shortLivedCookieExpires.name:
55                     checkThatCookieDoesNotExpireAfter(cookie, overTwoDaysInSeconds);
56                     break;
57                 case longLivedCookieMaxAge.name:
58                     checkThatCookieDoesNotExpireAfter(cookie, overOneWeekInSeconds);
59                     break;
60                 case longLivedCookieExpires.name:
61                     checkThatCookieDoesNotExpireAfter(cookie, overOneWeekInSeconds);
62                     break;
63             }
64         }
65         if (passedTests === 4) {
66             testPassed("The two short-lived cookies don't expire after more than " + overTwoDaysInSeconds + " seconds.");
67             testPassed("The two long-lived cookies don't expire after more than " + overOneWeekInSeconds + " seconds.");
68         } else
69             testFailed("At least one cookie's expiry attribute was beyond the test thresholds.");
70     } else
71         testFailed("No internals object.");
72
73     if (internals)
74         internals.setResourceLoadStatisticsEnabled(false);
75
76     finishJSTest();
77 </script>
78 </body>
79 </html>