Unreviewed, rolling out r227942.
[WebKit-https.git] / LayoutTests / http / tests / resourceLoadStatistics / add-partitioning-to-redirect.html
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4     <meta charset="UTF-8">
5     <script src="/js-test-resources/js-test.js"></script>
6 </head>
7 <body>
8 <script>
9     description("Tests that partitioning is added mid-flight in redirects.");
10     jsTestIsAsync = true;
11
12     function setEnableFeature(enable) {
13         if (!enable)
14             testRunner.statisticsResetToConsistentState();
15         internals.setResourceLoadStatisticsEnabled(enable);
16         testRunner.setCookieStoragePartitioningEnabled(enable);
17     }
18
19     const partitionHost = "127.0.0.1:8000";
20     const thirdPartyOrigin = "http://localhost:8000";
21     const resourcePath = "/resourceLoadStatistics/resources";
22     const thirdPartyBaseUrl = thirdPartyOrigin + resourcePath;
23     const firstPartyCookieName = "firstPartyCookie";
24     const subPathToSetFirstPartyCookie = "/set-cookie.php?name=" + firstPartyCookieName + "&value=value";
25     const partitionedCookieName = "partitionedCookie";
26     const subPathToSetPartitionedCookie = "/set-cookie.php?name=" + partitionedCookieName + "&value=value";
27     const returnUrl = "http://" + partitionHost + "/resourceLoadStatistics/add-partitioning-to-redirect.html";
28     const subPathToGetCookies = "/get-cookies.php?name1=" + firstPartyCookieName + "&name2=" + partitionedCookieName;
29     const redirectChainUrl = "http://" + partitionHost + resourcePath + "/redirect.php?redirectTo=" + thirdPartyBaseUrl + subPathToGetCookies;
30
31     function openIframe(url, onLoadHandler) {
32         const element = document.createElement("iframe");
33         element.src = url;
34         if (onLoadHandler) {
35             element.onload = onLoadHandler;
36         }
37         document.body.appendChild(element);
38     }
39
40     function runTest() {
41         switch (document.location.hash) {
42             case "#step1":
43                 // Set first-party cookie for localhost.
44                 document.location.href = thirdPartyBaseUrl + subPathToSetFirstPartyCookie + "#" + returnUrl + "#step2";
45                 break;
46             case "#step2":
47                 // Check that the cookie gets sent for localhost under 127.0.0.1 since localhost is not prevalent.
48                 document.location.hash = "step3";
49                 openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should receive first-party cookie.", runTest);
50                 break;
51             case "#step3":
52                 document.location.hash = "step4";
53                 // Load an iframe in a redirect chain that starts with 127.0.0.1 and ends with localhost. Expect a cookie for localhost.
54                 openIframe(redirectChainUrl + "&message=Redirect case, should receive first-party cookie.", runTest);
55                 break;
56             case "#step4":
57                 // Set localhost as prevalent and with non-recent user interaction to put it in the partitioning category.
58                 document.location.hash = "step5";
59                 testRunner.setStatisticsPrevalentResource("http://localhost", true);
60                 if (!testRunner.isStatisticsPrevalentResource("http://localhost"))
61                     testFailed("Host did not get set as prevalent resource.");
62                 testRunner.setStatisticsHasHadNonRecentUserInteraction("http://localhost");
63                 testRunner.statisticsUpdateCookiePartitioning(function() {
64                     runTest();
65                 });
66                 break;
67             case "#step5":
68                 // Check that no cookie gets sent for localhost under 127.0.0.1 since localhost is partitioned.
69                 document.location.hash = "step6";
70                 openIframe(thirdPartyBaseUrl + subPathToGetCookies +  "&message=Should receive no cookies.", runTest);
71                 break;
72             case "#step6":
73                 // Load an iframe in a redirect chain that starts with 127.0.0.1 and ends with localhost. Expect no cookie for localhost.
74                 document.location.hash = "step7";
75                 openIframe(redirectChainUrl + "&message=Redirect case, should receive no cookie.", runTest);
76                 break;
77             case "#step7":
78                 // Set partitioned cookie for localhost under 127.0.0.1.
79                 document.location.hash = "step8";
80                 openIframe(thirdPartyBaseUrl + subPathToSetPartitionedCookie + "&message=Set partitioned, third-party cookie.", runTest);
81                 break;
82             case "#step8":
83                 // Check that partitioned cookie gets sent for localhost under 127.0.0.1 since localhost is partitioned.
84                 document.location.hash = "step9";
85                 openIframe(thirdPartyBaseUrl + subPathToGetCookies +  "&message=Should receive partitioned cookie.", runTest);
86                 break;
87             case "#step9":
88                 // Load an iframe in a redirect chain that starts with 127.0.0.1 and ends with localhost. Expect the partitioned cookie for localhost.
89                 document.location.hash = "step10";
90                 openIframe(redirectChainUrl + "&message=Redirect case, should receive partitioned cookie.", runTest);
91                 break;
92             case "#step10":
93                 setEnableFeature(false);
94                 finishJSTest();
95                 break;
96         }
97     }
98
99     if (document.location.hash === "") {
100         setEnableFeature(true);
101         if (testRunner.isStatisticsPrevalentResource("http://localhost"))
102             testFailed("Localhost was classified as prevalent resource before the test started.");
103         testRunner.dumpChildFramesAsText();
104         document.location.hash = "step1";
105         // Make sure the network process is up-to-date.
106         testRunner.statisticsSetShouldPartitionCookiesForHost("localhost", false, runTest);
107     } else {
108         runTest();
109     }
110 </script>
111 </body>
112 </html>