tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

form-submission.sub.html (2956B)


      1 <!DOCTYPE html>
      2 <!--
      3 [%provenance%]
      4 -->
      5 <html lang="en">
      6  <meta charset="utf-8">
      7  <meta name="timeout" content="long">
      8  <title>HTTP headers on request for HTML form navigation</title>
      9  <script src="/resources/testharness.js"></script>
     10  <script src="/resources/testharnessreport.js"></script>
     11  {%- if subtests|selectattr('userActivated')|list %}
     12  <script src="/resources/testdriver.js"></script>
     13  <script src="/resources/testdriver-vendor.js"></script>
     14  {%- endif %}
     15  <script src="/fetch/metadata/resources/helper.sub.js"></script>
     16  <body>
     17  <script>
     18  'use strict';
     19 
     20  function induceRequest(method, url, userActivated) {
     21    const windowName = String(Math.random());
     22    const form = document.createElement('form');
     23    const submit = document.createElement('input');
     24    submit.setAttribute('type', 'submit');
     25    form.appendChild(submit);
     26    const win = open('about:blank', windowName);
     27    form.setAttribute('method', method);
     28    form.setAttribute('action', url);
     29    form.setAttribute('target', windowName);
     30    document.body.appendChild(form);
     31 
     32    // Query parameters must be expressed as form values so that they are sent
     33    // with the submission of forms whose method is POST.
     34    Array.from(new URL(url, location.origin).searchParams)
     35      .forEach(([name, value]) => {
     36        const input = document.createElement('input');
     37        input.setAttribute('type', 'hidden');
     38        input.setAttribute('name', name);
     39        input.setAttribute('value', value);
     40        form.appendChild(input);
     41      });
     42 
     43    return new Promise((resolve) => {
     44        addEventListener('message', function(event) {
     45          if (event.source === win) {
     46            resolve();
     47          }
     48        });
     49 
     50        if (userActivated) {
     51          test_driver.click(submit);
     52        } else {
     53          submit.click();
     54        }
     55      })
     56      .then(() => {
     57        form.remove();
     58        win.close();
     59      });
     60  }
     61  const responseParams = {
     62    mime: 'text/html',
     63    body: `<script>opener.postMessage('done', '*')</${''}script>`
     64  };
     65 
     66  {%- for subtest in subtests %}
     67 
     68  promise_test(() => {
     69    const key = '{{uuid()}}';
     70    const url = makeRequestURL(key, [% subtest.origins %], responseParams);
     71    const userActivated = [% 'true' if subtest.userActivated else 'false' %];
     72    return induceRequest('[%subtest.method | default("POST")%]', url, userActivated)
     73      .then(() => retrieve(key))
     74      .then((headers) => {
     75        {%- if subtest.expected == none %}
     76          assert_not_own_property(headers, '[%subtest.headerName%]');
     77        {%- else %}
     78          assert_own_property(headers, '[%subtest.headerName%]');
     79          assert_array_equals(headers['[%subtest.headerName%]'], ['[%subtest.expected%]']);
     80        {%- endif %}
     81        });
     82  }, '[%subtest.headerName%] - [%subtest.description | pad("end", " - ")%][%subtest.method | default("POST")%][%" with user activation" if subtest.userActivated%]');
     83 
     84  {%- endfor %}
     85  </script>
     86  </body>
     87 </html>