Skip to content

armgit / moli

Public Imported from GitHub
0

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

10.9 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>
const WORKER_URL = new URL(
  "/wpt/ported/worker/resources/worker-fetch-body-worker.js",
  location.href,
).href;
const MULTIPART_CONTENT_TYPE_PREFIX = "multipart/form-data; boundary=";

function asciiHex(value) {
  let output = "";
  for (let i = 0; i < value.length; i++) {
    output += value.charCodeAt(i).toString(16).padStart(2, "0");
  }
  return output;
}

function utf8Hex(value) {
  let output = "";
  for (const byte of new TextEncoder().encode(value)) {
    output += byte.toString(16).padStart(2, "0");
  }
  return output;
}

function hexAscii(hex) {
  let output = "";
  for (let i = 0; i < hex.length; i += 2) {
    output += String.fromCharCode(parseInt(hex.slice(i, i + 2), 16));
  }
  return output;
}

function countOccurrences(value, needle) {
  let count = 0;
  let index = 0;
  while (true) {
    index = value.indexOf(needle, index);
    if (index === -1) {
      return count;
    }
    count += 1;
    index += needle.length;
  }
}

function multipartBoundary(message) {
  assert_true(
    message.content_type.startsWith(MULTIPART_CONTENT_TYPE_PREFIX + "----MoliFormDataBoundary"),
    "worker FormData should install a multipart Content-Type with a generated boundary",
  );
  return message.content_type.slice(MULTIPART_CONTENT_TYPE_PREFIX.length);
}

function assertBoundaryOnlyDelimitsMultipartBody(body, boundary, partCount) {
  assert_equals(
    countOccurrences(body, boundary),
    partCount + 1,
    "the selected multipart boundary should only appear in part delimiters",
  );
  assert_equals(
    countOccurrences(body, `--${boundary}`),
    partCount + 1,
    "the selected multipart boundary delimiters should match the part count",
  );
  assert_true(
    body.endsWith(`--${boundary}--\r\n`),
    "the multipart body should end with the selected closing boundary",
  );
}

function next_message(worker, expectedKind) {
  return new Promise(function (resolve, reject) {
    function cleanup() {
      worker.removeEventListener("message", onmessage);
      worker.removeEventListener("error", onerror);
    }

    function onmessage(event) {
      if (!event.data || event.data.kind !== expectedKind) {
        return;
      }
      cleanup();
      resolve(event.data);
    }

    function onerror(event) {
      cleanup();
      reject(event.error || new Error(event.message || "worker error"));
    }

    worker.addEventListener("message", onmessage);
    worker.addEventListener("error", onerror, { once: true });
  });
}

async function run_worker_case(kind) {
  const worker = new Worker(WORKER_URL);
  try {
    worker.postMessage(kind);
    const message = await next_message(worker, kind);
    assert_equals(message.error, undefined, "worker fetch should not reject");
    assert_equals(message.status, 200, "worker fetch response status");
    return message;
  } finally {
    worker.terminate();
  }
}

promise_test(async function () {
  const message = await run_worker_case("string");
  assert_equals(message.body_hex, "68656c6c6f", "worker string body bytes");
  assert_equals(
    message.content_type,
    "text/plain;charset=UTF-8",
    "worker string body should send default text/plain Content-Type",
  );
}, "Dedicated workers send string fetch() bodies with default text/plain Content-Type");

promise_test(async function () {
  const message = await run_worker_case("params");
  assert_equals(
    message.body_hex,
    "616c7068613d6f6e6526626574613d74776f",
    "worker URLSearchParams body bytes",
  );
  assert_equals(
    message.content_type,
    "application/x-www-form-urlencoded;charset=UTF-8",
    "worker URLSearchParams body should send default urlencoded Content-Type",
  );
}, "Dedicated workers send URLSearchParams fetch() bodies with default urlencoded Content-Type");

promise_test(async function () {
  const message = await run_worker_case("blob");
  assert_equals(
    message.body_hex,
    "00ff41800a0d",
    "worker Blob body should preserve non-UTF-8 bytes",
  );
  assert_equals(
    message.content_type,
    "application/octet-stream",
    "worker Blob body should send the Blob type as Content-Type",
  );
}, "Dedicated workers preserve Blob fetch() body bytes and Blob type Content-Type");

promise_test(async function () {
  const message = await run_worker_case("arraybuffer");
  assert_equals(
    message.body_hex,
    "0100ff80",
    "worker ArrayBuffer body should preserve non-UTF-8 bytes",
  );
  assert_equals(
    message.content_type,
    null,
    "worker ArrayBuffer body should not send a default Content-Type",
  );
}, "Dedicated workers preserve ArrayBuffer fetch() body bytes without adding a default Content-Type");

promise_test(async function () {
  const message = await run_worker_case("typedarray");
  assert_equals(
    message.body_hex,
    "00ff4180",
    "worker TypedArray body should preserve non-UTF-8 bytes",
  );
  assert_equals(
    message.content_type,
    null,
    "worker TypedArray body should not send a default Content-Type",
  );
}, "Dedicated workers preserve TypedArray fetch() body bytes without adding a default Content-Type");

promise_test(async function () {
  const message = await run_worker_case("dataview");
  assert_equals(
    message.body_hex,
    "00ff80",
    "worker DataView body should only send the selected byte range",
  );
  assert_equals(
    message.content_type,
    null,
    "worker DataView body should not send a default Content-Type",
  );
}, "Dedicated workers preserve DataView fetch() body ranges without adding a default Content-Type");

promise_test(async function () {
  const message = await run_worker_case("explicit-string");
  assert_equals(message.body_hex, "68656c6c6f", "worker string body bytes");
  assert_equals(
    message.content_type,
    "text/custom-worker",
    "worker string body should preserve an explicit Content-Type",
  );
}, "Dedicated workers preserve explicit Content-Type over string BodyInit defaults");

promise_test(async function () {
  const message = await run_worker_case("explicit-params");
  assert_equals(
    message.body_hex,
    "616c7068613d6f6e65",
    "worker URLSearchParams body bytes",
  );
  assert_equals(
    message.content_type,
    "application/custom-worker-form",
    "worker URLSearchParams body should preserve an explicit Content-Type",
  );
}, "Dedicated workers preserve explicit Content-Type over URLSearchParams BodyInit defaults");

promise_test(async function () {
  const message = await run_worker_case("explicit-blob");
  assert_equals(message.body_hex, "00ff41", "worker Blob body bytes");
  assert_equals(
    message.content_type,
    "application/custom-worker-blob",
    "worker Blob body should preserve an explicit Content-Type",
  );
}, "Dedicated workers preserve explicit Content-Type over Blob BodyInit defaults");

promise_test(async function () {
  const message = await run_worker_case("explicit-arraybuffer");
  assert_equals(message.body_hex, "0100ff80", "worker ArrayBuffer body bytes");
  assert_equals(
    message.content_type,
    "application/custom-worker-bytes",
    "worker ArrayBuffer body should preserve an explicit Content-Type",
  );
}, "Dedicated workers preserve explicit Content-Type over ArrayBuffer BodyInit");

promise_test(async function () {
  const message = await run_worker_case("formdata-empty-mime");
  const boundary = multipartBoundary(message);
  const expectedHex =
    asciiHex(`--${boundary}\r\n`) +
    asciiHex('Content-Disposition: form-data; name="empty-blob"; filename="blob"\r\n') +
    asciiHex("Content-Type: application/octet-stream\r\n\r\n") +
    asciiHex("blob-body\r\n") +
    asciiHex(`--${boundary}\r\n`) +
    asciiHex('Content-Disposition: form-data; name="empty-file"; filename="empty.bin"\r\n') +
    asciiHex("Content-Type: application/octet-stream\r\n\r\n") +
    asciiHex("file-body\r\n") +
    asciiHex(`--${boundary}--\r\n`);

  assert_equals(
    message.body_hex,
    expectedHex,
    "worker fetch(FormData) should default empty Blob/File MIME types to application/octet-stream",
  );
}, "Dedicated workers serialize empty-MIME FormData Blob and File parts with the default Content-Type");

promise_test(async function () {
  const message = await run_worker_case("formdata-escaped-names");
  const boundary = multipartBoundary(message);
  const expectedHex =
    asciiHex(`--${boundary}\r\n`) +
    asciiHex('Content-Disposition: form-data; name="alpha"\r\n\r\n') +
    asciiHex("one\r\n") +
    asciiHex(`--${boundary}\r\n`) +
    asciiHex('Content-Disposition: form-data; name="line%0D%0Aname"\r\n\r\n') +
    asciiHex("line\r\nvalue\r\n") +
    asciiHex(`--${boundary}\r\n`) +
    asciiHex('Content-Disposition: form-data; name="carriage%0D%0Aname"\r\n\r\n') +
    asciiHex("carriage\r\nvalue\r\n") +
    asciiHex(`--${boundary}\r\n`) +
    asciiHex('Content-Disposition: form-data; name="quote%22name"\r\n\r\n') +
    asciiHex('quote"value\r\n') +
    asciiHex(`--${boundary}\r\n`) +
    utf8Hex('Content-Disposition: form-data; name="cafe-é"\r\n\r\n') +
    utf8Hex("值-😀\r\n") +
    asciiHex(`--${boundary}\r\n`) +
    asciiHex('Content-Disposition: form-data; name="file%22name"; filename="report%22name.txt"\r\n') +
    asciiHex("Content-Type: text/custom\r\n\r\n") +
    asciiHex("file-body\r\n") +
    asciiHex(`--${boundary}\r\n`) +
    utf8Hex('Content-Disposition: form-data; name="unicode-file"; filename="resume-é-😀.txt"\r\n') +
    asciiHex("Content-Type: text/unicode\r\n\r\n") +
    asciiHex("unicode-file-body\r\n") +
    asciiHex(`--${boundary}--\r\n`);

  assert_equals(
    message.body_hex,
    expectedHex,
    "worker fetch(FormData) should escape multipart field names and filenames while preserving UTF-8 bytes",
  );
}, "Dedicated workers escape FormData multipart field names and filenames");

promise_test(async function () {
  const probes = [
    "----MoliFormDataBoundary0000000000000000",
    "----MoliFormDataBoundary0123456789abcdef",
    "----MoliFormDataBoundaryffffffffffffffff",
  ];
  const message = await run_worker_case("formdata-boundary-collision");
  const boundary = multipartBoundary(message);
  const body = hexAscii(message.body_hex);

  assertBoundaryOnlyDelimitsMultipartBody(body, boundary, 3);
  for (const probe of probes) {
    assert_true(
      body.includes(probe),
      "boundary-like field data should be preserved in the multipart body",
    );
  }
}, "Dedicated workers avoid choosing a FormData boundary that appears in multipart parts");

promise_test(async function () {
  const message = await run_worker_case("explicit-formdata");
  assert_equals(
    message.content_type,
    "text/custom-worker-formdata",
    "worker FormData body should preserve an explicit Content-Type",
  );
  assert_true(
    message.body_hex.startsWith(asciiHex("------MoliFormDataBoundary")),
    "worker FormData body should still use the generated multipart boundary",
  );
}, "Dedicated workers preserve explicit Content-Type over FormData BodyInit defaults");
</script>