summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/api/abort/request.any.js
blob: dcc7803abe5576d048a5a4dd9291768149190fb3 (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
// META: timeout=long
// META: global=window,worker

const BODY_FUNCTION_AND_DATA = {
  arrayBuffer: null,
  blob: null,
  formData: new FormData(),
  json: new Blob(["{}"]),
  text: null,
};

for (const [bodyFunction, body] of Object.entries(BODY_FUNCTION_AND_DATA)) {
  promise_test(async () => {
    const controller = new AbortController();
    const signal = controller.signal;
    const request = new Request("../resources/data.json", {
      method: "post",
      signal,
      body,
    });

    controller.abort();
    await request[bodyFunction]();
    assert_true(
      true,
      `An aborted request should still be able to run ${bodyFunction}()`
    );
  }, `Calling ${bodyFunction}() on an aborted request`);

  promise_test(async () => {
    const controller = new AbortController();
    const signal = controller.signal;
    const request = new Request("../resources/data.json", {
      method: "post",
      signal,
      body,
    });

    const p = request[bodyFunction]();
    controller.abort();
    await p;
    assert_true(
      true,
      `An aborted request should still be able to run ${bodyFunction}()`
    );
  }, `Aborting a request after calling ${bodyFunction}()`);

  if (!body) {
    promise_test(async () => {
      const controller = new AbortController();
      const signal = controller.signal;
      const request = new Request("../resources/data.json", {
        method: "post",
        signal,
        body,
      });

      // consuming happens synchronously, so don't wait
      fetch(request).catch(() => {});

      controller.abort();
      await request[bodyFunction]();
      assert_true(
        true,
        `An aborted consumed request should still be able to run ${bodyFunction}() when empty`
      );
    }, `Calling ${bodyFunction}() on an aborted consumed empty request`);
  }

  promise_test(async t => {
    const controller = new AbortController();
    const signal = controller.signal;
    const request = new Request("../resources/data.json", {
      method: "post",
      signal,
      body: body || new Blob(["foo"]),
    });

    // consuming happens synchronously, so don't wait
    fetch(request).catch(() => {});

    controller.abort();
    await promise_rejects_js(t, TypeError, request[bodyFunction]());
  }, `Calling ${bodyFunction}() on an aborted consumed nonempty request`);
}