Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

2.6 KB
Raw Blame
<!doctype html>
<meta charset="utf-8">
<script src="/wpt/resources/testharness.js"></script>
<script src="/wpt/resources/testharnessreport.js"></script>
<script src="/wpt/resources/moli-wpt-adapter.js"></script>
<script>
function wait(ms) {
  return new Promise(function (resolve) {
    setTimeout(resolve, ms);
  });
}

async function wait_for_abort_observation(token) {
  const statusUrl = "/wpt/runtime/xhr/abort-observing-status/" + encodeURIComponent(token);

  for (let attempt = 0; attempt < 250; attempt += 1) {
    const response = await fetch(statusUrl);
    const payload = await response.json();
    if (payload.complete) {
      return payload;
    }
    await wait(20);
  }

  throw new Error("timed out waiting for abort observation " + token);
}

promise_test(async function () {
  const token = "xhr-abort-" + Date.now() + "-" + Math.random().toString(16).slice(2);
  const xhr = new XMLHttpRequest();
  const events = [];
  let snapshot = null;
  let abortScheduled = false;

  await new Promise(function (resolve) {
    xhr.onreadystatechange = function () {
      if (xhr.readyState === XMLHttpRequest.LOADING && !abortScheduled) {
        abortScheduled = true;
        setTimeout(function () {
          xhr.abort();
        }, 0);
      }
    };
    xhr.onabort = function () {
      events.push("abort");
    };
    xhr.onerror = function () {
      events.push("error");
    };
    xhr.onload = function () {
      events.push("load");
    };
    xhr.onloadend = function () {
      events.push("loadend");
      snapshot = {
        readyState: xhr.readyState,
        status: xhr.status,
        responseText: xhr.responseText,
        responseURL: xhr.responseURL,
      };
      resolve();
    };

    xhr.open("GET", "/wpt/runtime/xhr/abort-observing/" + encodeURIComponent(token));
    xhr.send();

    setTimeout(function () {
      if (!abortScheduled) {
        abortScheduled = true;
        xhr.abort();
      }
    }, 40);
  });

  const observation = await wait_for_abort_observation(token);

  assert_array_equals(events, ["abort", "loadend"], "abort should stay in the abort/loadend terminal path");
  assert_equals(snapshot.readyState, XMLHttpRequest.DONE, "abort loadend should observe DONE");
  assert_equals(snapshot.status, 0, "abort should clear status");
  assert_equals(snapshot.responseText, "", "abort should clear responseText");
  assert_equals(snapshot.responseURL, "", "abort should clear responseURL");
  assert_true(observation.disconnected, "aborting the XHR should close the underlying response stream early");
}, "XMLHttpRequest abort closes the in-flight response transport");
</script>