Add and remove cookie partition accordingly in intermediary redirect requests
[WebKit-https.git] / LayoutTests / http / tests / resourceLoadStatistics / remove-partitioning-from-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 onload="runTest()">
8 <script>
9     description("Tests that partitioning 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     if (document.location.hash === "") {
20         setEnableFeature(true);
21         if (testRunner.isStatisticsPrevalentResource("http://localhost"))
22             testFailed("Localhost was classified as prevalent resource before the test starts.");
23         testRunner.dumpChildFramesAsText();
24         document.location.hash = "step1";
25     }
26
27     const partitionHost = "127.0.0.1:8000";
28     const thirdPartyOrigin = "http://localhost:8000";
29     const resourcePath = "/resourceLoadStatistics/resources";
30     const thirdPartyBaseUrl = thirdPartyOrigin + resourcePath;
31     const partitionBaseUrl = "http://" + partitionHost + resourcePath;
32     const firstPartyCookieName = "firstPartyCookie";
33     const subPathToSetFirstPartyCookie = "/set-cookie.php?name=" + firstPartyCookieName + "&value=value";
34     const partitionedCookieName = "partitionedCookie";
35     const returnUrl = "http://" + partitionHost + "/resourceLoadStatistics/remove-partitioning-from-redirect.html";
36     const subPathToGetCookies = "/get-cookies.php?name1=" + firstPartyCookieName + "&name2=" + partitionedCookieName;
37     const redirectChainUrl = thirdPartyBaseUrl + "/redirect.php?redirectTo=" + partitionBaseUrl + subPathToGetCookies;
38
39     function openIframe(url, onLoadHandler) {
40         const element = document.createElement("iframe");
41         element.src = url;
42         if (onLoadHandler) {
43             element.onload = onLoadHandler;
44         }
45         document.body.appendChild(element);
46     }
47
48     function runTest() {
49         switch (document.location.hash) {
50             case "#step1":
51                 // Set localhost as prevalent.
52                 document.location.hash = "step2";
53                 testRunner.setStatisticsPrevalentResource("http://localhost", true);
54                 testRunner.statisticsUpdateCookiePartitioning();
55                 if (!testRunner.isStatisticsPrevalentResource("http://localhost"))
56                     testFailed("Host did not get set as prevalent resource.");
57                 runTest();
58                 break;
59             case "#step2":
60                 // Set first-party cookie for 127.0.0.1.
61                 document.location.href = partitionBaseUrl + subPathToSetFirstPartyCookie + "#" + returnUrl + "#step3";
62                 break;
63             case "#step3":
64                 // Check that the cookie gets sent for 127.0.0.1 under 127.0.0.1.
65                 document.location.hash = "step4";
66                 openIframe(partitionBaseUrl + subPathToGetCookies + "&message=Should receive first-party cookie.", runTest);
67                 break;
68             case "#step4":
69                 document.location.hash = "step5";
70                 // 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.
71                 openIframe(redirectChainUrl + "&message=Redirect case, should receive first-party cookie.", runTest);
72                 break;
73             case "#step5":
74                 setEnableFeature(false);
75                 finishJSTest();
76                 break;
77         }
78     }
79 </script>
80 </body>
81 </html>