<!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 HTML "a" element navigation</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, {test, userActivated, attributes}) {
    const win = window.open();
    const anchor = win.document.createElement('a');
    anchor.setAttribute('href', url);

    for (const [ name, value ] of Object.entries(attributes)) {
      anchor.setAttribute(name, value);
    }

    win.document.body.appendChild(anchor);

    test.add_cleanup(() => win.close());

    if (userActivated) {
      test_driver.bless('enable user activation', () => anchor.click());
    } else {
      anchor.click();
    }
  }

  {%- for subtest in subtests %}

  promise_test((t) => {
    const key = '{{uuid()}}';

    induceRequest(
      makeRequestURL(key, [% subtest.origins %], {mime: 'text/html'}),
      {
        test: t,
        userActivated: [%subtest.userActivated | default(false) | tojson%],
        attributes: [%subtest.elementAttrs | default({}) | tojson%]
      }
    );

    // `induceRequest` does not necessarily trigger a navigation, so the Python
    // handler must be polled until it has received the initial request.
    return retrieve(key, {poll: true})
      .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("start", " - ")%] - [%subtest.elementAttrs | collection("attributes")%][% " with user activation" if subtest.userActivated%]');

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