5 <script src="/js-test-resources/js-test.js"></script>
9 description("Tests that partitioning is added mid-flight in redirects.");
12 function setEnableFeature(enable) {
14 testRunner.statisticsResetToConsistentState();
15 internals.setResourceLoadStatisticsEnabled(enable);
16 testRunner.setCookieStoragePartitioningEnabled(enable);
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;
31 function openIframe(url, onLoadHandler) {
32 const element = document.createElement("iframe");
35 element.onload = onLoadHandler;
37 document.body.appendChild(element);
41 switch (document.location.hash) {
43 // Set first-party cookie for localhost.
44 document.location.href = thirdPartyBaseUrl + subPathToSetFirstPartyCookie + "#" + returnUrl + "#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);
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);
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() {
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);
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);
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);
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);
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);
93 setEnableFeature(false);
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);