Skip to content

armgit / moli

Public Imported from GitHub
0

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

2.5 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 assert_invalid_character_error(callback, description) {
  let thrown = null;
  try {
    callback();
  } catch (error) {
    thrown = error;
  }
  assert_true(thrown instanceof DOMException, description + " should throw a DOMException");
  assert_equals(thrown.name, "InvalidCharacterError", description + " exception name");
  assert_equals(thrown.code, 5, description + " legacy code");
}

test(function () {
  assert_equals(typeof atob, "function");
  assert_equals(typeof btoa, "function");
  assert_equals(atob.length, 1, "atob length");
  assert_equals(btoa.length, 1, "btoa length");
}, "atob() and btoa() are exposed as one-argument functions");

test(function () {
  assert_equals(btoa("hello"), "aGVsbG8=");
  assert_equals(btoa("\x00\xff"), "AP8=");
  assert_equals(btoa(null), "bnVsbA==", "arguments are stringified before encoding");
  assert_equals(btoa(undefined), "dW5kZWZpbmVk", "explicit undefined is stringified");
  assert_equals(atob("aGVsbG8="), "hello");
  assert_equals(atob(" Y\tQ\n==\r"), "a", "ASCII whitespace is ignored while decoding");
  assert_equals(atob("YQ"), "a", "padding may be omitted");
}, "base64 encode/decode handles Latin-1 bytes, stringification, whitespace, and omitted padding");

test(function () {
  assert_throws_js(TypeError, function () {
    btoa();
  }, "btoa missing argument");
  assert_throws_js(TypeError, function () {
    atob();
  }, "atob missing argument");
  assert_throws_js(TypeError, function () {
    btoa(Symbol("data"));
  }, "btoa Symbol input");
  assert_throws_js(TypeError, function () {
    atob(Symbol("data"));
  }, "atob Symbol input");
  assert_throws_js(Error, function () {
    btoa({ toString() { throw new Error("boom"); } });
  }, "btoa stringification errors propagate");
  assert_throws_js(Error, function () {
    atob({ toString() { throw new Error("boom"); } });
  }, "atob stringification errors propagate");
}, "base64 globals parse required DOMString arguments with WebIDL semantics");

test(function () {
  assert_invalid_character_error(function () {
    btoa("\u2713");
  }, "btoa non-Latin-1 input");
  assert_invalid_character_error(function () {
    atob("Y");
  }, "atob length modulo 4 equals 1");
  assert_invalid_character_error(function () {
    atob("!!!!");
  }, "atob invalid alphabet input");
}, "invalid base64 inputs throw InvalidCharacterError");
</script>