tor-browser

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

request.any.js (2354B)


      1 // META: timeout=long
      2 // META: global=window,worker
      3 
      4 const BODY_FUNCTION_AND_DATA = {
      5  arrayBuffer: null,
      6  blob: null,
      7  formData: new FormData(),
      8  json: new Blob(["{}"]),
      9  text: null,
     10 };
     11 
     12 for (const [bodyFunction, body] of Object.entries(BODY_FUNCTION_AND_DATA)) {
     13  promise_test(async () => {
     14    const controller = new AbortController();
     15    const signal = controller.signal;
     16    const request = new Request("../resources/data.json", {
     17      method: "post",
     18      signal,
     19      body,
     20    });
     21 
     22    controller.abort();
     23    await request[bodyFunction]();
     24    assert_true(
     25      true,
     26      `An aborted request should still be able to run ${bodyFunction}()`
     27    );
     28  }, `Calling ${bodyFunction}() on an aborted request`);
     29 
     30  promise_test(async () => {
     31    const controller = new AbortController();
     32    const signal = controller.signal;
     33    const request = new Request("../resources/data.json", {
     34      method: "post",
     35      signal,
     36      body,
     37    });
     38 
     39    const p = request[bodyFunction]();
     40    controller.abort();
     41    await p;
     42    assert_true(
     43      true,
     44      `An aborted request should still be able to run ${bodyFunction}()`
     45    );
     46  }, `Aborting a request after calling ${bodyFunction}()`);
     47 
     48  if (!body) {
     49    promise_test(async () => {
     50      const controller = new AbortController();
     51      const signal = controller.signal;
     52      const request = new Request("../resources/data.json", {
     53        method: "post",
     54        signal,
     55        body,
     56      });
     57 
     58      // consuming happens synchronously, so don't wait
     59      fetch(request).catch(() => {});
     60 
     61      controller.abort();
     62      await request[bodyFunction]();
     63      assert_true(
     64        true,
     65        `An aborted consumed request should still be able to run ${bodyFunction}() when empty`
     66      );
     67    }, `Calling ${bodyFunction}() on an aborted consumed empty request`);
     68  }
     69 
     70  promise_test(async t => {
     71    const controller = new AbortController();
     72    const signal = controller.signal;
     73    const request = new Request("../resources/data.json", {
     74      method: "post",
     75      signal,
     76      body: body || new Blob(["foo"]),
     77    });
     78 
     79    // consuming happens synchronously, so don't wait
     80    fetch(request).catch(() => {});
     81 
     82    controller.abort();
     83    await promise_rejects_js(t, TypeError, request[bodyFunction]());
     84  }, `Calling ${bodyFunction}() on an aborted consumed nonempty request`);
     85 }