Bug 1481369 [wpt PR 12330] - Fix worklet referrer tests to match spec, a=testonly
authorDominic Farolino <domfarolino@gmail.com>
Mon, 13 Aug 2018 18:20:58 +0000
changeset 486604 23d8c44ece32cb90b1ffdf2900a132ebbd10cc6a
parent 486603 607ed7312870154790798781353f291b78e3071a
child 486605 e2def376d23939122b38fb8d6adaf3d9c66111c5
push id9719
push userffxbld-merge
push dateFri, 24 Aug 2018 17:49:46 +0000
treeherdermozilla-beta@719ec98fba77 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstestonly
bugs1481369, 12330
milestone63.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1481369 [wpt PR 12330] - Fix worklet referrer tests to match spec, a=testonly Automatic update from web-platform-testsFix worklet referrer tests to match spec -- wpt-commits: b315ee9209a6039e96412520163764b3ae360605 wpt-pr: 12330
testing/web-platform/meta/MANIFEST.json
testing/web-platform/tests/worklets/resources/referrer-checker.py
testing/web-platform/tests/worklets/resources/referrer-tests.js
testing/web-platform/tests/worklets/resources/referrer-window.html
--- a/testing/web-platform/meta/MANIFEST.json
+++ b/testing/web-platform/meta/MANIFEST.json
@@ -653035,25 +653035,25 @@
    "1539ad004ed41f521f42b7e564fdf1cbfbdc2de3",
    "support"
   ],
   "worklets/resources/import-tests.js": [
    "5ab841cd4a6c3f42e75d8837bc333f316f66e148",
    "support"
   ],
   "worklets/resources/referrer-checker.py": [
-   "3d9afb4ab89a98412b618f64e2b702e9543d80f3",
+   "8f3798397431fc1c800bf4cf3ec8a0fb6d07eedb",
    "support"
   ],
   "worklets/resources/referrer-tests.js": [
-   "e46f93dbc3f5424a41276569c2e1aa2ef9bcf2b0",
+   "01b8e2ab57acb4c41d898f1541e2011c4946bd0a",
    "support"
   ],
   "worklets/resources/referrer-window.html": [
-   "331db07935a8d5a0f08b6dbf6e9df0958d7a8916",
+   "4817f0452a2999ae2774b2849130cb11e670778d",
    "support"
   ],
   "worklets/resources/service-worker-interception-tests.js": [
    "16d298737772507ee0616ab138adfc227aac3619",
    "support"
   ],
   "worklets/resources/service-worker.js": [
    "67e6754cf039a37965a9ae9e00dcc9d635b1ae27",
--- a/testing/web-platform/tests/worklets/resources/referrer-checker.py
+++ b/testing/web-platform/tests/worklets/resources/referrer-checker.py
@@ -1,15 +1,24 @@
 # Returns a valid response when request's |referrer| matches
 # |expected_referrer|.
 def main(request, response):
-    referrer = request.headers.get("referer", "")
+    # We want |referrer| to be the referrer header with no query params,
+    # because |expected_referrer| will not contain any query params, and
+    # thus cannot be compared with the actual referrer header if it were to
+    # contain query params. This works fine if the actual referrer has no
+    # query params too.
+    referrer = request.headers.get("referer", "").split("?")[0]
+    referrer_policy = request.GET.first("referrer_policy")
     expected_referrer = request.GET.first("expected_referrer", "")
-
     response_headers = [("Content-Type", "text/javascript"),
                         ("Access-Control-Allow-Origin", "*")]
 
-    # The expected referrer doesn't contain query params for simplification, so
-    # we check the referrer by startswith() here.
-    if (expected_referrer != "" and
-        referrer.startswith(expected_referrer + "?")):
-        return (200, response_headers, "")
+    if referrer_policy == "no-referrer" or referrer_policy == "origin":
+        if referrer == expected_referrer:
+            return (200, response_headers, "")
+        return (404, response_headers)
+
+    if referrer_policy == "same-origin":
+        if referrer == expected_referrer:
+            return (200, response_headers, "")
+        return (404, response_headers)
     return (404, response_headers)
--- a/testing/web-platform/tests/worklets/resources/referrer-tests.js
+++ b/testing/web-platform/tests/worklets/resources/referrer-tests.js
@@ -24,150 +24,148 @@ function runReferrerTest(settings) {
       `?pipe=header(Referrer-Policy, ${settings.referrerPolicy})`;
   return openWindow(kWindowURL).then(win => {
     const promise = new Promise(resolve => window.onmessage = resolve);
     win.postMessage(settings, '*');
     return promise;
   }).then(msg_event => assert_equals(msg_event.data, 'RESOLVED'));
 }
 
-// Runs a series of tests related to the referrer policy on a worklet. Referrer
-// on worklet module loading should always be handled with the default referrer
-// policy.
+// Runs a series of tests related to the referrer policy on a worklet.
 //
 // Usage:
 // runReferrerTests("paint");
 function runReferrerTests(workletType) {
   const worklet = get_worklet(workletType);
 
   // Tests for top-level script fetch -----------------------------------------
 
   promise_test(() => {
     return runReferrerTest({ workletType: workletType,
                              fetchType: 'top-level',
                              referrerPolicy: 'no-referrer',
                              scriptOrigins: { topLevel: 'same' } });
   }, 'Importing a same-origin script from a page that has "no-referrer" ' +
-     'referrer policy.');
+     'referrer policy should not send referrer.');
 
   promise_test(() => {
     return runReferrerTest({ workletType: workletType,
                              fetchType: 'top-level',
                              referrerPolicy: 'no-referrer',
                              scriptOrigins: { topLevel: 'remote' } });
   }, 'Importing a remote-origin script from a page that has "no-referrer" ' +
-     'referrer policy.');
+     'referrer policy should not send referrer.');
 
   promise_test(() => {
     return runReferrerTest({ workletType: workletType,
                              fetchType: 'top-level',
                              referrerPolicy: 'origin',
                              scriptOrigins: { topLevel: 'same' } });
   }, 'Importing a same-origin script from a page that has "origin" ' +
-     'referrer policy.');
+     'referrer policy should send only an origin as referrer.');
 
   promise_test(() => {
     return runReferrerTest({ workletType: workletType,
                              fetchType: 'top-level',
                              referrerPolicy: 'origin',
                              scriptOrigins: { topLevel: 'remote' } });
   }, 'Importing a remote-origin script from a page that has "origin" ' +
-     'referrer policy.');
+     'referrer policy should send only an origin as referrer.');
 
   promise_test(() => {
     return runReferrerTest({ workletType: workletType,
                              fetchType: 'top-level',
                              referrerPolicy: 'same-origin',
                              scriptOrigins: { topLevel: 'same' } });
   }, 'Importing a same-origin script from a page that has "same-origin" ' +
-     'referrer policy.');
+     'referrer policy should send referrer.');
 
   promise_test(() => {
     return runReferrerTest({ workletType: workletType,
                              fetchType: 'top-level',
                              referrerPolicy: 'same-origin',
                              scriptOrigins: { topLevel: 'remote' } });
   }, 'Importing a remote-origin script from a page that has "same-origin" ' +
-     'referrer policy.');
+     'referrer policy should not send referrer.');
 
   // Tests for descendant script fetch -----------------------------------------
 
   promise_test(() => {
     return runReferrerTest({ workletType: workletType,
                              fetchType: 'descendant',
                              referrerPolicy: 'no-referrer',
                              scriptOrigins: { topLevel: 'same',
                                               descendant: 'same' } });
   }, 'Importing a same-origin script from a same-origin worklet script that ' +
-     'has "no-referrer" referrer policy.');
+     'has "no-referrer" referrer policy should not send referrer.');
 
   promise_test(() => {
     return runReferrerTest({ workletType: workletType,
                              fetchType: 'descendant',
                              referrerPolicy: 'no-referrer',
                              scriptOrigins: { topLevel: 'same',
                                               descendant: 'remote' } });
   }, 'Importing a remote-origin script from a same-origin worklet script ' +
-     'that has "no-referrer" referrer policy.');
+     'that has "no-referrer" referrer policy should not send referrer.');
 
   promise_test(() => {
     return runReferrerTest({ workletType: workletType,
                              fetchType: 'descendant',
                              referrerPolicy: 'no-referrer',
                              scriptOrigins: { topLevel: 'remote',
                                               descendant: 'remote' } });
   }, 'Importing a remote-origin script from a remote-origin worklet script ' +
-     'that has "no-referrer" referrer policy.');
+     'that has "no-referrer" referrer policy should not send referrer.');
 
   promise_test(() => {
     return runReferrerTest({ workletType: workletType,
                              fetchType: 'descendant',
                              referrerPolicy: 'origin',
                              scriptOrigins: { topLevel: 'same',
                                               descendant: 'same' } });
   }, 'Importing a same-origin script from a same-origin worklet script that ' +
-     'has "origin" referrer policy.');
+     'has "origin" referrer policy should send referrer.');
 
   promise_test(() => {
     return runReferrerTest({ workletType: workletType,
                              fetchType: 'descendant',
                              referrerPolicy: 'origin',
                              scriptOrigins: { topLevel: 'same',
                                               descendant: 'remote' } });
   }, 'Importing a remote-origin script from a same-origin worklet script ' +
-     'that has "origin" referrer policy.');
+     'that has "origin" referrer policy should send referrer.');
 
   promise_test(() => {
     return runReferrerTest({ workletType: workletType,
                              fetchType: 'descendant',
                              referrerPolicy: 'origin',
                              scriptOrigins: { topLevel: 'remote',
                                               descendant: 'remote' } });
   }, 'Importing a remote-origin script from a remote-origin worklet script ' +
-     'that has "origin" referrer policy.');
+     'that has "origin" referrer policy should send referrer.');
 
   promise_test(() => {
     return runReferrerTest({ workletType: workletType,
                              fetchType: 'descendant',
                              referrerPolicy: 'same-origin',
                              scriptOrigins: { topLevel: 'same',
                                               descendant: 'same' } });
   }, 'Importing a same-origin script from a same-origin worklet script that ' +
-     'has "same-origin" referrer policy.');
+     'has "same-origin" referrer policy should send referrer.');
 
   promise_test(() => {
     return runReferrerTest({ workletType: workletType,
                              fetchType: 'descendant',
                              referrerPolicy: 'same-origin',
                              scriptOrigins: { topLevel: 'same',
                                               descendant: 'remote' } });
   }, 'Importing a remote-origin script from a same-origin worklet script ' +
-     'that has "same-origin" referrer policy.');
+     'that has "same-origin" referrer policy should not send referrer.');
 
   promise_test(() => {
     return runReferrerTest({ workletType: workletType,
                              fetchType: 'descendant',
                              referrerPolicy: 'same-origin',
                              scriptOrigins: { topLevel: 'remote',
                                               descendant: 'remote' } });
   }, 'Importing a remote-origin script from a remote-origin worklet script ' +
-     'that has "same-origin" referrer policy.');
+     'that has "same-origin" referrer policy should not send referrer.');
 }
--- a/testing/web-platform/tests/worklets/resources/referrer-window.html
+++ b/testing/web-platform/tests/worklets/resources/referrer-window.html
@@ -43,40 +43,57 @@ function createScriptURLForDecendant(scr
 function isDestinationCrossOrigin(fetchType, scriptOrigins) {
   if (fetchType === 'top-level')
     return scriptOrigins.topLevel === 'remote';
   if (fetchType === 'descendant')
     return scriptOrigins.descendant === 'remote';
   assert_unreached('fetchType has an invalid value.');
 }
 
+function createExpectedReferrer(
+    importerURL, fetchType, referrerPolicy, scriptOrigins) {
+  if (referrerPolicy === 'no-referrer')
+    return "";
+  if (referrerPolicy === 'same-origin') {
+    if (isDestinationCrossOrigin(fetchType, scriptOrigins))
+      return "";
+    // Delete query params to make it easier to match with an actual referrer in
+    // the referrer-checker.py.
+    const expectedReferrer = new URL(importerURL);
+    for (var key of expectedReferrer.searchParams.keys())
+      expectedReferrer.searchParams.delete(key);
+    return expectedReferrer;
+  }
+  if (referrerPolicy === 'origin')
+    return (new URL(importerURL)).origin + '/';
+  assert_unreached('referrerPolicy has an invalid value.');
+}
+
 window.onmessage = e => {
   const workletType = e.data.workletType;
   const fetchType = e.data.fetchType;
+  const referrerPolicy = e.data.referrerPolicy;
   const scriptOrigins = e.data.scriptOrigins;
 
   let scriptURL;
   let expectedReferrer;
   if (fetchType === 'top-level') {
     scriptURL = createScriptURLForTopLevel(scriptOrigins.topLevel);
-    // The referrer of the top-level script should be this file.
-    // Delete query params to make it easier to match with an actual referrer in
-    // the referrer-checker.py.
-    expectedReferrer = new URL(location.href);
-    for (var key of expectedReferrer.searchParams.keys())
-      expectedReferrer.searchParams.delete(key);
+    expectedReferrer = createExpectedReferrer(
+        location.href, fetchType, referrerPolicy, scriptOrigins);
   } else if (fetchType === 'descendant') {
     scriptURL = createScriptURLForDecendant(scriptOrigins);
-    // The referrer of the imported script should be the importer script.
-    expectedReferrer = scriptURL;
+    expectedReferrer = createExpectedReferrer(
+        scriptURL, fetchType, referrerPolicy, scriptOrigins);
   } else {
     assert_unreached('fetchType should be \'top-level\' or \'descendant\'');
   }
 
   const params = new URLSearchParams;
+  params.append('referrer_policy', referrerPolicy);
   params.append('expected_referrer', expectedReferrer);
 
   get_worklet(workletType).addModule(scriptURL + '?' + params)
       .then(() => window.opener.postMessage('RESOLVED', '*'))
       .catch(e => window.opener.postMessage(e.message, '*'));
 };
 
 window.opener.postMessage('LOADED', '*');