https://github.com/web-platform-tests/wpt
Raw File
Tip revision: ab86a314a1d287663396aed4a4249d1ad00773db authored by Jinho Bang on 06 April 2018, 12:23:32 UTC
WebNFC: Use [SecureContext] instead of manual check
Tip revision: ab86a31
fetch-request-resources.https.html
<!DOCTYPE html>
<title>Service Worker: FetchEvent for resources</title>
<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>
<script>
var url_count = 0;
var expected_results = {};

function add_promise_to_test(url)
{
  var expected = expected_results[url];
  return new Promise((resolve) => {
    expected.resolve = resolve;
  });
}

function image_test(frame, url, cross_origin, expected_mode,
                    expected_credentials) {
  var actual_url = url + (++url_count);
  expected_results[actual_url] = {
      cross_origin: cross_origin,
      mode: expected_mode,
      credentials: expected_credentials,
      redirect: 'follow',
      integrity: '',
      message: 'Image load (url:' +
               actual_url + ' cross_origin:' + cross_origin + ')'
    };
  frame.contentWindow.load_image(actual_url, cross_origin);
  return add_promise_to_test(actual_url);
}

function script_test(frame, url, cross_origin, expected_mode,
                     expected_credentials) {
  var actual_url = url + (++url_count);
  expected_results[actual_url] = {
      cross_origin: cross_origin,
      mode: expected_mode,
      credentials: expected_credentials,
      redirect: 'follow',
      integrity: '',
      message: 'Script load (url:' +
               actual_url + ' cross_origin:' + cross_origin + ')'
    };
  frame.contentWindow.load_script(actual_url, cross_origin);
  return add_promise_to_test(actual_url);
}

function css_test(frame, url, cross_origin, expected_mode,
                  expected_credentials) {
  var actual_url = url + (++url_count);
  expected_results[actual_url] = {
      cross_origin: cross_origin,
      mode: expected_mode,
      credentials: expected_credentials,
      redirect: 'follow',
      integrity: '',
      message: 'CSS load (url:' +
               actual_url + ' cross_origin:' + cross_origin + ')'
    };
  frame.contentWindow.load_css(actual_url, cross_origin);
  return add_promise_to_test(actual_url);
}

function font_face_test(frame, url, expected_mode, expected_credentials) {
  var actual_url = url + (++url_count);
  expected_results[actual_url] = {
      url: actual_url,
      mode: expected_mode,
      credentials: expected_credentials,
      redirect: 'follow',
      integrity: '',
      message: 'FontFace load (url:' + actual_url + ')'
    };
  frame.contentWindow.load_font(actual_url);
  return add_promise_to_test(actual_url);
}

function script_integrity_test(frame, url, integrity, expected_integrity) {
  var actual_url = url + (++url_count);
  expected_results[actual_url] = {
      url: actual_url,
      mode: 'no-cors',
      credentials: 'include',
      redirect: 'follow',
      integrity: expected_integrity,
      message: 'Script load (url:' + actual_url + ')'
    };
  frame.contentWindow.load_script_with_integrity(actual_url, integrity);
  return add_promise_to_test(actual_url);
}

function css_integrity_test(frame, url, integrity, expected_integrity) {
  var actual_url = url + (++url_count);
  expected_results[actual_url] = {
      url: actual_url,
      mode: 'no-cors',
      credentials: 'include',
      redirect: 'follow',
      integrity: expected_integrity,
      message: 'CSS load (url:' + actual_url + ')'
    };
  frame.contentWindow.load_css_with_integrity(actual_url, integrity);
  return add_promise_to_test(actual_url);
}

function fetch_test(frame, url, mode, credentials,
                    expected_mode, expected_credentials) {
  var actual_url = url + (++url_count);
  expected_results[actual_url] = {
      mode: expected_mode,
      credentials: expected_credentials,
      redirect: 'follow',
      integrity: '',
      message: 'fetch (url:' + actual_url + ' mode:' + mode + ' credentials:' +
               credentials + ')'
    };
  frame.contentWindow.fetch(
      new Request(actual_url, {mode: mode, credentials: credentials})).then(() => {
      }, () => { });
  return add_promise_to_test(actual_url);
}

function audio_test(frame, url, cross_origin,
                    expected_mode, expected_credentials) {
  var actual_url = url + (++url_count);
  expected_results[actual_url] = {
      mode: expected_mode,
      credentials: expected_credentials,
      redirect: 'follow',
      integrity: '',
      message: 'Audio load (url:' + actual_url + ' cross_origin:' +
               cross_origin + ')'
    };
  frame.contentWindow.load_audio(actual_url, cross_origin);
  return add_promise_to_test(actual_url);
}

promise_test(function(t) {
    var SCOPE = 'resources/fetch-request-resources-iframe.https.html';
    var SCRIPT = 'resources/fetch-request-resources-worker.js';
    var host_info = get_host_info();
    var LOCAL_URL =
      host_info['HTTPS_ORIGIN'] + base_path() + 'resources/dummy?test';
    var REMOTE_URL =
      host_info['HTTPS_REMOTE_ORIGIN'] + base_path() + 'resources/dummy?test';
    var worker;
    var frame;
    return service_worker_unregister_and_register(t, SCRIPT, SCOPE)
      .then(function(registration) {
          worker = registration.installing;
          return wait_for_state(t, worker, 'activated');
        })
      .then(function() {
          return new Promise(function(resolve, reject) {
              var channel = new MessageChannel();
              channel.port1.onmessage = t.step_func(function(msg) {
                if (msg.data.ready) {
                  resolve();
                  return;
                }
                var result = msg.data;
                var expected = expected_results[result.url];
                if (!expected) {
                  return;
                }
                test(() => {
                  assert_equals(
                    result.mode, expected.mode,
                    'mode of ' + expected.message +  ' must be ' +
                    expected.mode + '.');
                  assert_equals(
                    result.credentials, expected.credentials,
                    'credentials of ' + expected.message +  ' must be ' +
                    expected.credentials + '.');
                   assert_equals(
                    result.redirect, expected.redirect,
                    'redirect mode of ' + expected.message +  ' must be ' +
                    expected.redirect + '.');
                  assert_equals(
                    result.integrity, expected.integrity,
                    'integrity of ' + expected.message +  ' must be ' +
                    expected.integrity + '.');
                }, expected.message);
                expected.resolve();
                delete expected_results[result.url];
              });
              worker.postMessage(
                {port: channel.port2}, [channel.port2]);
            });
        })
      .then(function() { return with_iframe(SCOPE); })
      .then(async function(f) {
        frame = f;

        await image_test(f, LOCAL_URL, '', 'no-cors', 'include');
        await image_test(f, REMOTE_URL, '', 'no-cors', 'include');
        await css_test(f, LOCAL_URL, '', 'no-cors', 'include');
        await css_test(f, REMOTE_URL, '', 'no-cors', 'include');

        await image_test(f, LOCAL_URL, 'anonymous', 'cors', 'same-origin');
        await image_test(f, LOCAL_URL, 'use-credentials', 'cors', 'include');
        await image_test(f, REMOTE_URL, '', 'no-cors', 'include');
        await image_test(f, REMOTE_URL, 'anonymous', 'cors', 'same-origin');
        await image_test(f, REMOTE_URL, 'use-credentials', 'cors', 'include');

        await script_test(f, LOCAL_URL, '', 'no-cors', 'include');
        await script_test(f, LOCAL_URL, 'anonymous', 'cors', 'same-origin');
        await script_test(f, LOCAL_URL, 'use-credentials', 'cors', 'include');
        await script_test(f, REMOTE_URL, '', 'no-cors', 'include');
        await script_test(f, REMOTE_URL, 'anonymous', 'cors', 'same-origin');
        await script_test(f, REMOTE_URL, 'use-credentials', 'cors', 'include');

        await css_test(f, LOCAL_URL, '', 'no-cors', 'include');
        await css_test(f, LOCAL_URL, 'anonymous', 'cors', 'same-origin');
        await css_test(f, LOCAL_URL, 'use-credentials', 'cors', 'include');
        await css_test(f, REMOTE_URL, '', 'no-cors', 'include');
        await css_test(f, REMOTE_URL, 'anonymous', 'cors', 'same-origin');
        await css_test(f, REMOTE_URL, 'use-credentials', 'cors', 'include');

        await font_face_test(f, LOCAL_URL, 'cors', 'same-origin');
        await font_face_test(f, REMOTE_URL, 'cors', 'same-origin');

        await script_integrity_test(f, LOCAL_URL, '     ', '     ');
        await script_integrity_test(f, LOCAL_URL,
                               'This is not a valid integrity because it has no dashes',
                               'This is not a valid integrity because it has no dashes');
        await script_integrity_test(f, LOCAL_URL, 'sha256-', 'sha256-');
        await script_integrity_test(f, LOCAL_URL, 'sha256-foo?123', 'sha256-foo?123');
        await script_integrity_test(f, LOCAL_URL, 'sha256-foo sha384-abc ', 'sha256-foo sha384-abc ');
        await script_integrity_test(f, LOCAL_URL, 'sha256-foo sha256-abc', 'sha256-foo sha256-abc');

        await css_integrity_test(f, LOCAL_URL, '     ', '     ');
        await css_integrity_test(f, LOCAL_URL,
                            'This is not a valid integrity because it has no dashes',
                            'This is not a valid integrity because it has no dashes');
        await css_integrity_test(f, LOCAL_URL, 'sha256-', 'sha256-');
        await css_integrity_test(f, LOCAL_URL, 'sha256-foo?123', 'sha256-foo?123');
        await css_integrity_test(f, LOCAL_URL, 'sha256-foo sha384-abc ', 'sha256-foo sha384-abc ');
        await css_integrity_test(f, LOCAL_URL, 'sha256-foo sha256-abc', 'sha256-foo sha256-abc');

        await fetch_test(f, LOCAL_URL, 'same-origin', 'omit', 'same-origin', 'omit');
        await fetch_test(f, LOCAL_URL, 'same-origin', 'same-origin',
                    'same-origin', 'same-origin');
        await fetch_test(f, LOCAL_URL, 'same-origin', 'include',
                    'same-origin', 'include');
        await fetch_test(f, LOCAL_URL, 'no-cors', 'omit', 'no-cors', 'omit');
        await fetch_test(f, LOCAL_URL, 'no-cors', 'same-origin',
                    'no-cors', 'same-origin');
        await fetch_test(f, LOCAL_URL, 'no-cors', 'include', 'no-cors', 'include');
        await fetch_test(f, LOCAL_URL, 'cors', 'omit', 'cors', 'omit');
        await fetch_test(f, LOCAL_URL, 'cors', 'same-origin', 'cors', 'same-origin');
        await fetch_test(f, LOCAL_URL, 'cors', 'include', 'cors', 'include');
        await fetch_test(f, REMOTE_URL, 'no-cors', 'omit', 'no-cors', 'omit');
        await fetch_test(f, REMOTE_URL, 'no-cors', 'same-origin',
                    'no-cors', 'same-origin');
        await fetch_test(f, REMOTE_URL, 'no-cors', 'include', 'no-cors', 'include');
        await fetch_test(f, REMOTE_URL, 'cors', 'omit', 'cors', 'omit');
        await fetch_test(f, REMOTE_URL, 'cors', 'same-origin', 'cors', 'same-origin');
        await fetch_test(f, REMOTE_URL, 'cors', 'include', 'cors', 'include');

        await audio_test(f, LOCAL_URL, '', 'no-cors', 'include');
        await audio_test(f, LOCAL_URL, 'anonymous', 'cors', 'same-origin');
        await audio_test(f, LOCAL_URL, 'use-credentials', 'cors', 'include');
        await audio_test(f, REMOTE_URL, '', 'no-cors', 'include');
        await audio_test(f, REMOTE_URL, 'anonymous', 'cors', 'same-origin');
        await audio_test(f, REMOTE_URL, 'use-credentials', 'cors', 'include');

        frame.remove();
        service_worker_unregister(t, SCOPE);
      }).catch(unreached_rejection(t));
  }, 'Verify FetchEvent for resources.');
</script>
back to top