Unreviewed, rolling out r227942.
[WebKit-https.git] / LayoutTests / http / tests / resourceLoadStatistics / remove-blocking-in-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 blocking is removed 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/remove-blocking-in-redirect.html";
28     const subPathToGetCookies = "/get-cookies.php?name1=" + firstPartyCookieName + "&name2=" + partitionedCookieName;
29     const redirectChainUrl = thirdPartyOrigin + resourcePath + "/redirect.php?redirectTo=http://" + partitionHost + resourcePath + 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 cookies for 127.0.0.1 and localhost.
44                 document.cookie = firstPartyCookieName + "=127.0.0.1;path='/'";
45                 document.location.href = thirdPartyBaseUrl + subPathToSetFirstPartyCookie + "#" + returnUrl + "#step2";
46                 break;
47             case "#step2":
48                 // Check that the cookie gets sent for localhost under 127.0.0.1 since localhost is not prevalent.
49                 document.location.hash = "step3";
50                 openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should receive first-party cookie.", runTest);
51                 break;
52             case "#step3":
53                 document.location.hash = "step4";
54                 // Load an iframe in a redirect chain that starts with localhost and ends with 127.0.0.1. Expect a cookie for 127.0.0.1.
55                 openIframe(redirectChainUrl + "&message=Redirect case 1, should receive first-party cookie for 127.0.0.1.", runTest);
56                 break;
57             case "#step4":
58                 // Set localhost as prevalent and with non-recent user interaction to put it in the blocking category.
59                 document.location.hash = "step5";
60                 testRunner.setStatisticsPrevalentResource("http://localhost", true);
61                 if (!testRunner.isStatisticsPrevalentResource("http://localhost"))
62                     testFailed("Host did not get set as prevalent resource.");
63                 testRunner.statisticsUpdateCookiePartitioning(function() {
64                     runTest();
65                 });
66                 break;
67             case "#step5":
68                 // Set partitioned cookie for localhost under 127.0.0.1.
69                 document.location.hash = "step6";
70                 openIframe(thirdPartyBaseUrl + subPathToSetPartitionedCookie + "&message=Set partitioned, third-party cookie.", runTest);
71                 break;
72             case "#step6":
73                 // Check that no cookie gets sent for localhost under 127.0.0.1 since localhost's cookies are blocked.
74                 document.location.hash = "step7";
75                 openIframe(thirdPartyBaseUrl + subPathToGetCookies +  "&message=Should receive no cookie.", runTest);
76                 break;
77             case "#step7":
78                 // Load an iframe in a redirect chain that starts with localhost and ends with 127.0.0.1. Expect a cookie for 127.0.0.1.
79                 document.location.hash = "step8";
80                 openIframe(redirectChainUrl + "&message=Redirect case 2, should receive first-party cookie for 127.0.0.1.", runTest);
81                 break;
82             case "#step8":
83                 setEnableFeature(false);
84                 finishJSTest();
85                 break;
86         }
87     }
88
89     if (document.location.hash === "") {
90         setEnableFeature(true);
91         if (testRunner.isStatisticsPrevalentResource("http://localhost"))
92             testFailed("Localhost was classified as prevalent resource before the test starts.");
93         testRunner.dumpChildFramesAsText();
94         document.location.hash = "step1";
95         // Make sure the network process is up-to-date.
96         testRunner.statisticsSetShouldPartitionCookiesForHost("localhost", false, runTest);
97     } else {
98         runTest();
99     }
100 </script>
101 </body>
102 </html>