Add Resource Load Statistics test case for Ping
authorwilander@apple.com <wilander@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 10 Apr 2019 01:03:28 +0000 (01:03 +0000)
committerwilander@apple.com <wilander@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 10 Apr 2019 01:03:28 +0000 (01:03 +0000)
https://bugs.webkit.org/show_bug.cgi?id=196748

Reviewed by Alex Christensen.

* http/tests/resourceLoadStatistics/ping-to-prevalent-resource-expected.txt: Added.
* http/tests/resourceLoadStatistics/ping-to-prevalent-resource.html: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@244101 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/http/tests/resourceLoadStatistics/ping-to-prevalent-resource-expected.txt [new file with mode: 0644]
LayoutTests/http/tests/resourceLoadStatistics/ping-to-prevalent-resource.html [new file with mode: 0644]

index 7f1475e..2d5c8ae 100644 (file)
@@ -1,3 +1,13 @@
+2019-04-09  John Wilander  <wilander@apple.com>
+
+        Add Resource Load Statistics test case for Ping
+        https://bugs.webkit.org/show_bug.cgi?id=196748
+
+        Reviewed by Alex Christensen.
+
+        * http/tests/resourceLoadStatistics/ping-to-prevalent-resource-expected.txt: Added.
+        * http/tests/resourceLoadStatistics/ping-to-prevalent-resource.html: Added.
+
 2019-04-09  Megan Gardner  <megan_gardner@apple.com>
 
         Turn on editing test that should pass now
diff --git a/LayoutTests/http/tests/resourceLoadStatistics/ping-to-prevalent-resource-expected.txt b/LayoutTests/http/tests/resourceLoadStatistics/ping-to-prevalent-resource-expected.txt
new file mode 100644 (file)
index 0000000..549bfb9
--- /dev/null
@@ -0,0 +1,24 @@
+Tests that cookie blocking is applied to ping loads.
+   
+
+--------
+Frame: '<!--frame1-->'
+--------
+Should receive first-party cookie.
+Received cookie named 'firstPartyCookie'.
+Client-side document.cookie: firstPartyCookie=value
+
+--------
+Frame: '<!--frame2-->'
+--------
+Should receive no cookies.
+Did not receive cookie named 'firstPartyCookie'.
+Client-side document.cookie:
+
+--------
+Frame: '<!--frame3-->'
+--------
+Ping received.
+HTTP_HOST: localhost:8000
+REQUEST_URI: /contentextensions/resources/save-ping.php?test=ping-to-prevalent-resource
+No cookies in ping.
diff --git a/LayoutTests/http/tests/resourceLoadStatistics/ping-to-prevalent-resource.html b/LayoutTests/http/tests/resourceLoadStatistics/ping-to-prevalent-resource.html
new file mode 100644 (file)
index 0000000..3f5fdac
--- /dev/null
@@ -0,0 +1,97 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <script src="/js-test-resources/ui-helper.js"></script>
+    <script src="resources/util.js"></script>
+</head>
+<body>
+<div id="description">Tests that cookie blocking is applied to ping loads.</div>
+<a id="targetLink" href="#" ping="http://localhost:8000/contentextensions/resources/save-ping.php?test=ping-to-prevalent-resource">The link</a>
+<script>
+    if (testRunner)
+        testRunner.waitUntilDone();
+
+    const partitionHost = "127.0.0.1:8000";
+    const thirdPartyOrigin = "http://localhost:8000";
+    const resourcePath = "/resourceLoadStatistics/resources";
+    const thirdPartyBaseUrl = thirdPartyOrigin + resourcePath;
+    const firstPartyCookieName = "firstPartyCookie";
+    const subPathToSetFirstPartyCookie = "/set-cookie.php?name=" + firstPartyCookieName + "&value=value";
+    const returnUrl = "http://" + partitionHost + "/resourceLoadStatistics/ping-to-prevalent-resource.html";
+    const subPathToGetCookies = "/get-cookies.php?name1=" + firstPartyCookieName;
+
+    function activateElement(elementID, callback) {
+        var element = document.getElementById(elementID);
+        var centerX = element.offsetLeft + element.offsetWidth / 2;
+        var centerY = element.offsetTop + element.offsetHeight / 2;
+        UIHelper.activateAt(centerX, centerY).then(
+            function () {
+                callback();
+            },
+            function () {
+                testRunner.notifyDone();
+            }
+        );
+    }
+
+    function openIframe(url, onLoadHandler) {
+        const element = document.createElement("iframe");
+        element.src = url;
+        if (onLoadHandler) {
+            element.onload = onLoadHandler;
+        }
+        document.body.appendChild(element);
+    }
+
+    function runTest() {
+        switch (document.location.hash) {
+            case "#step1":
+                // Set first-party cookie for localhost.
+                document.location.href = thirdPartyBaseUrl + subPathToSetFirstPartyCookie + "#" + returnUrl + "#step2";
+                break;
+            case "#step2":
+                // Check that the cookie gets sent for localhost under 127.0.0.1 since localhost is not prevalent.
+                document.location.hash = "step3";
+                openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should receive first-party cookie.", runTest);
+                break;
+            case "#step3":
+                // Set localhost as prevalent to put it in the blocking category.
+                document.location.hash = "step4";
+                testRunner.setStatisticsPrevalentResource(thirdPartyOrigin, true, function() {
+                    testRunner.statisticsUpdateCookieBlocking(runTest);
+                });
+                break;
+            case "#step4":
+                // Check that no cookie gets sent for localhost under 127.0.0.1 since localhost's cookies are blocked.
+                document.location.hash = "step5";
+                openIframe(thirdPartyBaseUrl + subPathToGetCookies +  "&message=Should receive no cookies.", runTest);
+                break;
+            case "#step5":
+                // Click the link to trigger the ping.
+                activateElement("targetLink", function() {
+                    openIframe(thirdPartyOrigin + "/contentextensions/resources/get-ping-data.php?test=ping-to-prevalent-resource", function() {
+                        document.body.removeChild(document.getElementById("targetLink"));
+                        setEnableFeature(false, function() {
+                            testRunner.notifyDone();
+                        });
+                    });
+                });
+                break;
+        }
+    }
+
+    if (document.location.hash === "") {
+        if (testRunner) {
+            setEnableFeature(true, function () {
+                testRunner.dumpChildFramesAsText();
+                document.location.hash = "step1";
+                runTest();
+            });
+        }
+    } else {
+        runTest();
+    }
+</script>
+</body>
+</html>