https://github.com/web-platform-tests/wpt
Raw File
Tip revision: c248d538c0ba192b0a7b55eef4aaed6e1b83a5a0 authored by Robert Longson on 14 April 2018, 16:20:39 UTC
support ping, rel, referrerPolicy, relList, hreflang, type and text on SVG a elements
Tip revision: c248d53
fetch-event-referrer-policy.https.html
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<body>
<script>
var worker = 'resources/fetch-event-test-worker.js';

function run_referrer_policy_tests(frame, referrer, href, origin) {
    return frame.contentWindow.fetch('resources/simple.html?referrerFull',
                                     {method: "GET", referrer: referrer})
      .then(function(response) { return response.text(); })
      .then(function(response_text) {
          assert_equals(
            response_text,
            'Referrer: ' + href + '\n' +
            'ReferrerPolicy: no-referrer-when-downgrade',
            'Service Worker should respond to fetch with the referrer URL when a member of RequestInit is present');
          var http_url = get_host_info()['HTTP_ORIGIN'] + base_path() +
                         '/resources/simple.html?referrerFull';
          return frame.contentWindow.fetch(http_url,
                                           {method: "GET", referrer: referrer});
        })
      .then(function(response) { return response.text(); })
      .then(function(response_text) {
          assert_equals(
            response_text,
            'Referrer: \n' +
            'ReferrerPolicy: no-referrer-when-downgrade',
            'Service Worker should respond to fetch with no referrer when a member of RequestInit is present with an HTTP request');
          return frame.contentWindow.fetch('resources/simple.html?referrerFull',
                                           {referrerPolicy: "", referrer: referrer});
        })
      .then(function(response) { return response.text(); })
      .then(function(response_text) {
          assert_equals(
            response_text,
            'Referrer: ' + href + '\n' +
            'ReferrerPolicy: no-referrer-when-downgrade',
            'Service Worker should respond to fetch with the referrer with ""');
          var http_url = get_host_info()['HTTP_ORIGIN'] + base_path() +
                         '/resources/simple.html?referrerFull';
          return frame.contentWindow.fetch(http_url,
                                           {referrerPolicy: "", referrer: referrer});
        })
      .then(function(response) { return response.text(); })
      .then(function(response_text) {
          assert_equals(
            response_text,
            'Referrer: \n' +
            'ReferrerPolicy: no-referrer-when-downgrade',
            'Service Worker should respond to fetch with no referrer with ""');
          return frame.contentWindow.fetch('resources/simple.html?referrerFull',
                                           {referrerPolicy: "origin", referrer: referrer});
        })
      .then(function(response) { return response.text(); })
      .then(function(response_text) {
          assert_equals(
            response_text,
            'Referrer: ' + origin + '/' + '\n' +
            'ReferrerPolicy: origin',
            'Service Worker should respond to fetch with the referrer origin with "origin" and a same origin request');
          var http_url = get_host_info()['HTTP_ORIGIN'] + base_path() +
                         '/resources/simple.html?referrerFull';
          return frame.contentWindow.fetch(http_url,
                                           {referrerPolicy: "origin", referrer: referrer});
        })
      .then(function(response) { return response.text(); })
      .then(function(response_text) {
          assert_equals(
            response_text,
            'Referrer: ' + origin + '/' + '\n' +
            'ReferrerPolicy: origin',
            'Service Worker should respond to fetch with the referrer origin with "origin" and a cross origin request');
          return frame.contentWindow.fetch('resources/simple.html?referrerFull',
                                           {referrerPolicy: "origin-when-cross-origin", referrer: referrer});
        })
      .then(function(response) { return response.text(); })
      .then(function(response_text) {
          assert_equals(
            response_text,
            'Referrer: ' + href + '\n' +
            'ReferrerPolicy: origin-when-cross-origin',
            'Service Worker should respond to fetch with the referrer URL with "origin-when-cross-origin" and a same origin request');
          var http_url = get_host_info()['HTTP_ORIGIN'] + base_path() +
                         '/resources/simple.html?referrerFull';
          return frame.contentWindow.fetch(http_url,
                                           {referrerPolicy: "origin-when-cross-origin", referrer: referrer});
        })
      .then(function(response) { return response.text(); })
      .then(function(response_text) {
          assert_equals(
            response_text,
            'Referrer: ' + origin + '/' + '\n' +
            'ReferrerPolicy: origin-when-cross-origin',
            'Service Worker should respond to fetch with the referrer origin with "origin-when-cross-origin" and a cross origin request');
          return frame.contentWindow.fetch('resources/simple.html?referrerFull',
                                           {referrerPolicy: "no-referrer-when-downgrade", referrer: referrer});
        })
      .then(function(response) { return response.text(); })
      .then(function(response_text) {
          assert_equals(
            response_text,
            'Referrer: ' + href + '\n' +
            'ReferrerPolicy: no-referrer-when-downgrade',
            'Service Worker should respond to fetch with no referrer with "no-referrer-when-downgrade" and a same origin request');
          var http_url = get_host_info()['HTTP_ORIGIN'] + base_path() +
                         '/resources/simple.html?referrerFull';
          return frame.contentWindow.fetch(http_url,
                                           {referrerPolicy: "no-referrer-when-downgrade", referrer: referrer});
        })
      .then(function(response) { return response.text(); })
      .then(function(response_text) {
          assert_equals(
            response_text,
            'Referrer: \n' +
            'ReferrerPolicy: no-referrer-when-downgrade',
            'Service Worker should respond to fetch with no referrer with "no-referrer-when-downgrade" and an HTTP request');
          var http_url = get_host_info()['HTTP_ORIGIN'] + base_path() +
                         '/resources/simple.html?referrerFull';
          return frame.contentWindow.fetch(http_url, {referrerPolicy: "unsafe-url", referrer: referrer});
        })
      .then(function(response) { return response.text(); })
      .then(function(response_text) {
          assert_equals(
            response_text,
            'Referrer: ' + href + '\n' +
            'ReferrerPolicy: unsafe-url',
            'Service Worker should respond to fetch with no referrer with "unsafe-url"');
          return frame.contentWindow.fetch('resources/simple.html?referrerFull',
                                           {referrerPolicy: "no-referrer", referrer: referrer});
        })
      .then(function(response) { return response.text(); })
      .then(function(response_text) {
          assert_equals(
            response_text,
            'Referrer: \n' +
            'ReferrerPolicy: no-referrer',
            'Service Worker should respond to fetch with no referrer URL with "no-referrer"');
          return frame.contentWindow.fetch('resources/simple.html?referrerFull',
                                           {referrerPolicy: "same-origin", referrer: referrer});
        })
      .then(function(response) { return response.text(); })
      .then(function(response_text) {
          assert_equals(
            response_text,
            'Referrer: ' + href + '\n' +
            'ReferrerPolicy: same-origin',
            'Service Worker should respond to fetch with referrer URL with "same-origin" and a same origin request');
          var http_url = get_host_info()['HTTPS_REMOTE_ORIGIN'] + base_path() +
                         '/resources/simple.html?referrerFull';
          return frame.contentWindow.fetch(http_url,
                                           {referrerPolicy: "same-origin", referrer: referrer});
        })
      .then(function(response) { return response.text(); })
      .then(function(response_text) {
          assert_equals(
            response_text,
            'Referrer: \n' +
            'ReferrerPolicy: same-origin',
            'Service Worker should respond to fetch with no referrer with "same-origin" and cross origin request');
          var http_url = get_host_info()['HTTPS_REMOTE_ORIGIN'] + base_path() +
                         '/resources/simple.html?referrerFull';
          return frame.contentWindow.fetch(http_url,
                                           {referrerPolicy: "strict-origin", referrer: referrer});
        })
      .then(function(response) { return response.text(); })
      .then(function(response_text) {
          assert_equals(
            response_text,
            'Referrer: ' + origin + '/' + '\n' +
            'ReferrerPolicy: strict-origin',
            'Service Worker should respond to fetch with the referrer origin  with "strict-origin" and a HTTPS cross origin request');
          return frame.contentWindow.fetch('resources/simple.html?referrerFull',
                                           {referrerPolicy: "strict-origin", referrer: referrer});
        })
      .then(function(response) { return response.text(); })
      .then(function(response_text) {
          assert_equals(
            response_text,
            'Referrer: ' + origin + '/' + '\n' +
            'ReferrerPolicy: strict-origin',
            'Service Worker should respond to fetch with the referrer origin with "strict-origin" and a same origin request');
          var http_url = get_host_info()['HTTP_ORIGIN'] + base_path() +
                         '/resources/simple.html?referrerFull';
          return frame.contentWindow.fetch(http_url,
                                           {referrerPolicy: "strict-origin", referrer: referrer});
        })
      .then(function(response) { return response.text(); })
      .then(function(response_text) {
          assert_equals(
            response_text,
            'Referrer: \n' +
            'ReferrerPolicy: strict-origin',
            'Service Worker should respond to fetch with no referrer with "strict-origin" and a HTTP request');
          return frame.contentWindow.fetch('resources/simple.html?referrerFull',
                                           {referrerPolicy: "strict-origin-when-cross-origin", referrer: referrer});
        })
      .then(function(response) { return response.text(); })
      .then(function(response_text) {
          assert_equals(
            response_text,
            'Referrer: ' + href + '\n' +
            'ReferrerPolicy: strict-origin-when-cross-origin',
            'Service Worker should respond to fetch with the referrer URL with "strict-origin-when-cross-origin" and a same origin request');
          var http_url = get_host_info()['HTTPS_REMOTE_ORIGIN'] + base_path() +
                         '/resources/simple.html?referrerFull';
          return frame.contentWindow.fetch(http_url,
                                           {referrerPolicy: "strict-origin-when-cross-origin", referrer: referrer});
        })
      .then(function(response) { return response.text(); })
      .then(function(response_text) {
          assert_equals(
            response_text,
            'Referrer: ' + origin + '/' + '\n' +
            'ReferrerPolicy: strict-origin-when-cross-origin',
            'Service Worker should respond to fetch with the referrer origin with "strict-origin-when-cross-origin" and a HTTPS cross origin request');
          var http_url = get_host_info()['HTTP_ORIGIN'] + base_path() +
                         '/resources/simple.html?referrerFull';
          return frame.contentWindow.fetch(http_url,
                                           {referrerPolicy: "strict-origin-when-cross-origin", referrer: referrer});
        })
      .then(function(response) { return response.text(); })
      .then(function(response_text) {
          assert_equals(
            response_text,
            'Referrer: \n' +
            'ReferrerPolicy: strict-origin-when-cross-origin',
            'Service Worker should respond to fetch with no referrer with "strict-origin-when-cross-origin" and a HTTP request');
        });
}

async_test(function(t) {
    var scope = 'resources/simple.html?referrerPolicy';
    var frame;
    service_worker_unregister_and_register(t, worker, scope)
      .then(function(reg) {
          return wait_for_state(t, reg.installing, 'activated');
        })
      .then(function() { return with_iframe(scope); })
      .then(function(f) {
          frame = f;
          assert_equals(
            frame.contentDocument.body.textContent,
            'ReferrerPolicy: no-referrer-when-downgrade',
            'Service Worker should respond to fetch with the default referrer policy');
          // First, run the referrer policy tests without passing a referrer in RequestInit.
          return run_referrer_policy_tests(frame, undefined, frame.contentDocument.location.href,
                                           frame.contentDocument.location.origin);
        })
      .then(function() {
          // Now, run the referrer policy tests while passing a referrer in RequestInit.
          var referrer = get_host_info()['HTTPS_ORIGIN'] + base_path() + 'fake-referrer';
          return run_referrer_policy_tests(frame, 'fake-referrer', referrer,
                                           frame.contentDocument.location.origin);
        })
      .then(function() {
          frame.remove();
          return service_worker_unregister_and_done(t, scope);
        })
      .catch(unreached_rejection(t));
  }, 'Service Worker responds to fetch event with the referrer policy');

</script>
</body>
back to top