<!DOCTYPE html>
<!--
[%provenance%]
-->
<html lang="en">
  <meta charset="utf-8">
  <meta name="timeout" content="long">
  <title>HTTP headers on request for HTML form 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(method, url, userActivated) {
    const windowName = String(Math.random());
    const form = document.createElement('form');
    const submit = document.createElement('input');
    submit.setAttribute('type', 'submit');
    form.appendChild(submit);
    const win = open('about:blank', windowName);
    form.setAttribute('method', method);
    form.setAttribute('action', url);
    form.setAttribute('target', windowName);
    document.body.appendChild(form);

    // Query parameters must be expressed as form values so that they are sent
    // with the submission of forms whose method is POST.
    Array.from(new URL(url, location.origin).searchParams)
      .forEach(([name, value]) => {
        const input = document.createElement('input');
        input.setAttribute('type', 'hidden');
        input.setAttribute('name', name);
        input.setAttribute('value', value);
        form.appendChild(input);
      });

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

        if (userActivated) {
          test_driver.click(submit);
        } else {
          submit.click();
        }
      })
      .then(() => {
        form.remove();
        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 userActivated = [% 'true' if subtest.userActivated else 'false' %];
    return induceRequest('[%subtest.method | default("POST")%]', url, userActivated)
      .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", " - ")%][%subtest.method | default("POST")%][%" with user activation" if subtest.userActivated%]');

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