Unreviewed, rolling out r227942.
[WebKit-https.git] / LayoutTests / http / tests / resourceLoadStatistics / add-blocking-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 blocking 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-blocking-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 1, should receive first-party cookie.", runTest);
55                 break;
56             case "#step4":
57                 // Set localhost as prevalent and with no user interaction to put it in the blocking 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.statisticsUpdateCookiePartitioning(function() {
63                     runTest();
64                 });
65                 break;
66             case "#step5":
67                 // Check that no cookie gets sent for localhost under 127.0.0.1 since localhost's cookies are blocked.
68                 document.location.hash = "step6";
69                 openIframe(thirdPartyBaseUrl + subPathToGetCookies +  "&message=Should receive no cookies.", runTest);
70                 break;
71             case "#step6":
72                 // Load an iframe in a redirect chain that starts with 127.0.0.1 and ends with localhost. Expect no cookie for localhost.
73                 document.location.hash = "step7";
74                 openIframe(redirectChainUrl + "&message=Redirect case 2, should receive no cookie.", runTest);
75                 break;
76             case "#step7":
77                 // Try to set cookie for localhost under 127.0.0.1.
78                 document.location.hash = "step8";
79                 openIframe(thirdPartyBaseUrl + subPathToSetPartitionedCookie + "&message=Try to set third-party cookie in blocked mode.", runTest);
80                 break;
81             case "#step8":
82                 // Check that no cookie gets sent for localhost under 127.0.0.1 since localhost's cookies are blocked.
83                 document.location.hash = "step9";
84                 openIframe(thirdPartyBaseUrl + subPathToGetCookies +  "&message=After attempted cookie creation, should receive no cookie.", runTest);
85                 break;
86             case "#step9":
87                 // Load an iframe in a redirect chain that starts with 127.0.0.1 and ends with localhost. Expect no cookie for localhost.
88                 document.location.hash = "step10";
89                 openIframe(redirectChainUrl + "&message=Redirect case 3, after attempted cookie creation, should receive no cookie.", runTest);
90                 break;
91             case "#step10":
92                 setEnableFeature(false);
93                 finishJSTest();
94                 break;
95         }
96     }
97
98     if (document.location.hash === "") {
99         setEnableFeature(true);
100         if (testRunner.isStatisticsPrevalentResource("http://localhost"))
101             testFailed("Localhost was classified as prevalent resource before the test starts.");
102         testRunner.dumpChildFramesAsText();
103         document.location.hash = "step1";
104         // Make sure the network process is up-to-date.
105         testRunner.statisticsSetShouldPartitionCookiesForHost("localhost", false, runTest);
106     } else {
107         runTest();
108     }
109 </script>
110 </body>
111 </html>