summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/api/abort/keepalive.html
blob: db12df0d289be971cc26cd35dcbffc2f98d489e7 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<!DOCTYPE html>
<meta charset="utf-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script>
// This controller must be on the window so it is visible to the iframe.
window.sharedController = new AbortController();

async function fetchJson(url) {
  const response = await fetch(url);
  assert_true(response.ok, 'response should be ok');
  return response.json();
}

promise_test(async () => {
  const stateKey = token();
  const controller = new AbortController();
  await fetch(`../resources/infinite-slow-response.py?stateKey=${stateKey}`,
              {
                signal: controller.signal,
                keepalive: true
              });
  const before = await fetchJson(`../resources/stash-take.py?key=${stateKey}`);
  assert_equals(before, 'open', 'connection should be open');

  controller.abort();

  // Spin until the abort completes.
  while (true) {
    const after = await fetchJson(`../resources/stash-take.py?key=${stateKey}`);
    if (after) {
      // stateKey='open' was removed from the dictionary by the first fetch of
      // stash-take.py, so we should only ever see the value 'closed' here.
      assert_equals(after, 'closed', 'connection should have closed');
      break;
    }
  }
}, 'aborting a keepalive fetch should work');

promise_test(async t => {
  const key = token();
  const iframeEl = document.querySelector('iframe');

  // Tell the iframe to start the fetch, and wait until it says it has.
  await new Promise(resolve => {
    onmessage = t.step_func(event => {
      assert_equals(event.data, 'started', 'event data should be "started"');
      resolve();
    });
    iframeEl.contentWindow.postMessage(key, '*');
  });

  // Detach the context of the fetch.
  iframeEl.remove();

  sharedController.abort();

  // The abort should not do anything. The connection should stay open. Wait 1
  // second to give time for the fetch to complete.
  await new Promise(resolve => t.step_timeout(resolve, 1000));

  const after = await fetchJson(`../resources/stash-take.py?key=${key}`);
  assert_equals(after, 'on', 'fetch should have completed');
}, 'aborting a detached keepalive fetch should not do anything');
</script>

<iframe srcdoc="
                <!DOCTYPE html>
                <meta charset=utf-8>
                <script>
                onmessage = async event => {
                  const key = event.data;
                  await fetch(
                    `../resources/redirect.py?delay=500&amp;location=` +
                    `../resources/stash-put.py%3fkey=${key}%26value=on`,
                        {
                          signal: parent.sharedController.signal,
                          keepalive: true
                        });
                  parent.postMessage('started', '*');
                };
                </script>
                ">
</iframe>