<!DOCTYPE html>
<!--
[%provenance%]
-->
<html lang="en">
  <meta charset="utf-8">
  {%- if subtests|length > 10 %}
  <meta name="timeout" content="long">
  {%- endif %}
  <title>HTTP headers on request for navigation via the HTML Location API</title>
  <script src="/resources/testharness.js"></script>
  <script src="/resources/testharnessreport.js"></script>
  {%- if subtests|selectattr('userActivated')|list %}
  <script src="/resources/testdriver.js"></script>
  <script src="/resources/testdriver-vendor.js"></script>
  {%- endif %}
  <script src="/fetch/metadata/resources/helper.sub.js"></script>
  <body>
  <script>
  'use strict';

  function induceRequest(url, navigate, userActivated) {
    const win = window.open();

    return new Promise((resolve) => {
        addEventListener('message', function(event) {
          if (event.source === win) {
            resolve();
          }
        });

        if (userActivated) {
          test_driver.bless('enable user activation', () => {
            navigate(win, url);
          });
        } else {
          navigate(win, url);
        }
      })
      .then(() => win.close());
  }

  const responseParams = {
    mime: 'text/html',
    body: `<script>opener.postMessage('done', '*')</${''}script>`
  };
  {%- for subtest in subtests %}

  promise_test(() => {
    const key = '{{uuid()}}';
    const url = makeRequestURL(key, [% subtest.origins %], responseParams);

    const navigate = (win, path) => {
      win.location = path;
    };
    return induceRequest(url, navigate, [% 'true' if subtest.userActivated else 'false' %])
      .then(() => retrieve(key))
      .then((headers) => {
        {%- if subtest.expected == none %}
          assert_not_own_property(headers, '[%subtest.headerName%]');
        {%- else %}
          assert_own_property(headers, '[%subtest.headerName%]');
          assert_array_equals(headers['[%subtest.headerName%]'], ['[%subtest.expected%]']);
        {%- endif %}
        });
  }, '[%subtest.headerName%] - [%subtest.description | pad("end", " - ")%]location[% " with user activation" if subtest.userActivated%]');

  promise_test(() => {
    const key = '{{uuid()}}';
    const url = makeRequestURL(key, [% subtest.origins %], responseParams);

    const navigate = (win, path) => {
      win.location.href = path;
    };
    return induceRequest(url, navigate, [% 'true' if subtest.userActivated else 'false' %])
      .then(() => retrieve(key))
      .then((headers) => {
        {%- if subtest.expected == none %}
          assert_not_own_property(headers, '[%subtest.headerName%]');
        {%- else %}
          assert_own_property(headers, '[%subtest.headerName%]');
          assert_array_equals(headers['[%subtest.headerName%]'], ['[%subtest.expected%]']);
        {%- endif %}
        });
  }, '[%subtest.headerName%] - [%subtest.description | pad("end", " - ")%]location.href[% " with user activation" if subtest.userActivated%]');

  promise_test(() => {
    const key = '{{uuid()}}';
    const url = makeRequestURL(key, [% subtest.origins %], responseParams);

    const navigate = (win, path) => {
      win.location.assign(path);
    };
    return induceRequest(url, navigate, [% 'true' if subtest.userActivated else 'false' %])
      .then(() => retrieve(key))
      .then((headers) => {
        {%- if subtest.expected == none %}
          assert_not_own_property(headers, '[%subtest.headerName%]');
        {%- else %}
          assert_own_property(headers, '[%subtest.headerName%]');
          assert_array_equals(headers['[%subtest.headerName%]'], ['[%subtest.expected%]']);
        {%- endif %}
        });
  }, '[%subtest.headerName%] - [%subtest.description | pad("end", " - ")%]location.assign[% " with user activation" if subtest.userActivated%]');

  promise_test(() => {
    const key = '{{uuid()}}';
    const url = makeRequestURL(key, [% subtest.origins %], responseParams);

    const navigate = (win, path) => {
      win.location.replace(path);
    };
    return induceRequest(url, navigate, [% 'true' if subtest.userActivated else 'false' %])
      .then(() => retrieve(key))
      .then((headers) => {
        {%- if subtest.expected == none %}
          assert_not_own_property(headers, '[%subtest.headerName%]');
        {%- else %}
          assert_own_property(headers, '[%subtest.headerName%]');
          assert_array_equals(headers['[%subtest.headerName%]'], ['[%subtest.expected%]']);
        {%- endif %}
        });
  }, '[%subtest.headerName%] - [%subtest.description | pad("end", " - ")%]location.replace[% " with user activation" if subtest.userActivated%]');

  {%- endfor %}
  </script>
  </body>
</html>
