summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/metadata/unload.https.sub.html
blob: bc26048c810725515e538496482bd167b50a4ee2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE html>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/resources/testdriver.js></script>
<script src=/resources/testdriver-vendor.js></script>
<script src=/fetch/metadata/resources/helper.js></script>
<script src=/common/utils.js></script>
<body>
<script>
  // The test
  // 1. Creates a same-origin iframe
  // 2. Adds to the iframe an unload handler that will
  //    trigger a request to <unload_request_url>/.../record-header.py...
  // 3. Navigate the iframe to a cross-origin url (to data: url)
  // 4. Waits until the request goes through
  // 5. Verifies Sec-Fetch-Site request header of the request.
  //
  // This is a regression test for https://crbug.com/986577.
  function create_test(unload_request_origin, expectations) {
    async_test(t => {
      // STEP 1: Create an iframe.
      let nonce = token();
      let key = "unload-test-" + nonce;
      let url = unload_request_origin +
          "/fetch/metadata/resources/record-header.py?file=" + key;
      let i = document.createElement('iframe');
      i.src = 'resources/unload-with-beacon.html';
      i.onload = () => {
          // STEP 2: Ask the iframe to add an unload handler.
          i.contentWindow.postMessage(url, '*');
      };
      window.addEventListener('message', e => {
          // STEP 3: Navigate the iframe away
          i.contentWindow.location = 'data:text/html,DONE';
      });
      document.body.appendChild(i);

      // STEPS 4 and 5: Wait for the beacon to go through and verify
      // the request headers.
      function wait_and_verify() {
          t.step_timeout(() => {
              fetch("resources/record-header.py?retrieve=true&file=" + key)
                  .then(response => response.text())
                  .then(text => t.step(() => {
                      if (text == 'No header has been recorded') {
                        wait_and_verify();
                        return;
                      }
                      assert_header_equals(text, expectations);
                      t.done();
                  }))
          }, 200);
      }
      wait_and_verify();
    }, "Fetch from an unload handler");
  }

  create_test("https://{{host}}:{{ports[https][0]}}", {
    "site": "same-origin",
    "user": "",
    "mode": "no-cors",
    "dest": "empty"
  });
</script>