Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
<!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 ECHO_BODY_HEX_URL = "/wpt/runtime/fetch/echo-body-hex";
const MULTIPART_CONTENT_TYPE_PREFIX = "multipart/form-data; boundary=";
async function sendBody(body, init) {
const response = await fetch(ECHO_BODY_HEX_URL, Object.assign({
method: "POST",
body,
}, init || {}));
assert_equals(response.status, 200, "fetch FormData request status");
return response.json();
}
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 bytesHex(bytes) {
let output = "";
for (const byte of bytes) {
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(payload) {
assert_true(
payload.content_type.startsWith(MULTIPART_CONTENT_TYPE_PREFIX + "----MoliFormDataBoundary"),
"FormData should install a multipart Content-Type with a generated boundary",
);
return payload.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",
);
}
promise_test(async function () {
const data = new FormData();
data.append("alpha", "one");
data.append("line\nname", "line\nvalue");
data.append('quote"name', 'quote"value');
const payload = await sendBody(data);
const boundary = multipartBoundary(payload);
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="quote%22name"\r\n\r\n') +
asciiHex('quote"value\r\n') +
asciiHex(`--${boundary}--\r\n`);
assert_equals(
payload.body_hex,
expectedHex,
"fetch(FormData) should serialize string fields in insertion order",
);
}, "fetch() serializes FormData string fields as multipart/form-data");
promise_test(async function () {
const binaryBytes = [0x00, 0xff, 0x41, 0x80, 0x0a, 0x0d];
const data = new FormData();
data.append("before", "text");
data.append("blob", new Blob([new Uint8Array(binaryBytes)], { type: "application/octet-stream" }));
data.append('file"name', new File(["file-body"], 'report"name.txt', { type: "text/custom" }));
const payload = await sendBody(data);
const boundary = multipartBoundary(payload);
const expectedHex =
asciiHex(`--${boundary}\r\n`) +
asciiHex('Content-Disposition: form-data; name="before"\r\n\r\n') +
asciiHex("text\r\n") +
asciiHex(`--${boundary}\r\n`) +
asciiHex('Content-Disposition: form-data; name="blob"; filename="blob"\r\n') +
asciiHex("Content-Type: application/octet-stream\r\n\r\n") +
bytesHex(binaryBytes) +
asciiHex("\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`);
assert_equals(
payload.body_hex,
expectedHex,
"fetch(FormData) should preserve Blob/File metadata and non-UTF-8 bytes",
);
}, "fetch() serializes FormData Blob and File parts");
promise_test(async function () {
const data = new FormData();
data.append("empty-blob", new Blob(["blob-body"]));
data.append("empty-file", new File(["file-body"], "empty.bin"));
const payload = await sendBody(data);
const boundary = multipartBoundary(payload);
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(
payload.body_hex,
expectedHex,
"fetch(FormData) should default empty Blob/File MIME types to application/octet-stream",
);
}, "fetch() serializes empty-MIME FormData Blob and File parts with the default Content-Type");
promise_test(async function () {
const probes = [
"----MoliFormDataBoundary0000000000000000",
"----MoliFormDataBoundary0123456789abcdef",
"----MoliFormDataBoundaryffffffffffffffff",
];
const data = new FormData();
data.append(`field ${probes[0]}`, `text value ${probes[1]}`);
data.append("file", new File([`file bytes ${probes[2]}`], `report ${probes[0]}.txt`, {
type: "application/x-moliformdataboundary",
}));
data.append("tail", probes.join("|"));
const payload = await sendBody(data);
const boundary = multipartBoundary(payload);
const body = hexAscii(payload.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",
);
}
}, "fetch() avoids choosing a FormData boundary that appears in multipart parts");
promise_test(async function () {
const data = new FormData();
data.append("alpha", "one");
const payload = await sendBody(data, {
headers: { "Content-Type": "text/plain;charset=utf-8" },
});
assert_equals(
payload.content_type,
"text/plain;charset=utf-8",
"explicit Content-Type should not be overwritten for FormData",
);
assert_true(
payload.body_hex.startsWith(asciiHex("------MoliFormDataBoundary")),
"FormData body should still use the generated multipart boundary",
);
}, "fetch() preserves an explicit Content-Type for FormData bodies");
promise_test(async function () {
const boundary = "parse-boundary";
const body =
`--${boundary}\r\n` +
'Content-Disposition: form-data; name="field%22name"\r\n' +
"\r\n" +
"before--parse-boundary-like text\r\nafter\r\n" +
`--${boundary}\r\n` +
'Content-Disposition: form-data; name="upload"; filename="report%22name.txt"\r\n' +
"Content-Type: Text/Custom\r\n" +
"\r\n" +
"file-body\r\n" +
`--${boundary}\r\n` +
'Content-Disposition: form-data; name="untyped"; filename="plain.txt"\r\n' +
"\r\n" +
"plain-body\r\n" +
`--${boundary}--\r\n`;
const formData = await new Response(body, {
headers: { "Content-Type": `multipart/form-data; boundary="${boundary}"` },
}).formData();
const file = formData.get("upload");
const untyped = formData.get("untyped");
assert_equals(
formData.get("field%22name"),
"before--parse-boundary-like text\r\nafter",
"Body.formData() should preserve percent sequences in field names and ordinary boundary-like text",
);
assert_true(file instanceof File, "multipart filename parts should become File objects");
assert_equals(file.name, "report%22name.txt", "filename header parameters should preserve percent sequences");
assert_equals(file.type, "text/custom", "part Content-Type should be normalized for File.type");
assert_equals(await file.text(), "file-body", "file body should be preserved");
assert_true(untyped instanceof File, "filename parts without Content-Type should still become File objects");
assert_equals(untyped.name, "plain.txt", "filename should be preserved when Content-Type is absent");
assert_equals(untyped.type, "text/plain", "filename parts without Content-Type should default File.type to text/plain");
assert_equals(await untyped.text(), "plain-body", "untyped file body should be preserved");
}, "Body.formData() parses multipart/form-data fields and file parts");
promise_test(async function () {
const boundary = "preamble-boundary";
const body =
"ignored preamble\r\n" +
`--${boundary}\r\n` +
'Content-Disposition: form-data; name="field"\r\n' +
"\r\n" +
"value\r\n" +
`--${boundary}--\r\n` +
"ignored epilogue";
const formData = await new Response(body, {
headers: { "Content-Type": `multipart/form-data; boundary=${boundary}` },
}).formData();
assert_equals(formData.get("field"), "value", "preamble and epilogue should be ignored");
}, "Body.formData() ignores multipart preamble and epilogue");
promise_test(async function (t) {
const boundary = "parse-boundary";
const body =
`--${boundary}\r\n` +
'Content-Disposition: form-data; name="field"\r\n' +
"\r\n" +
"before\r\n" +
`--${boundary}-not-a-delimiter\r\n` +
"after\r\n" +
`--${boundary}--\r\n`;
const response = new Response(body, {
headers: { "Content-Type": `multipart/form-data; boundary=${boundary}` },
});
await promise_rejects_js(
t,
TypeError,
response.formData(),
"a boundary prefix with an invalid suffix should reject before truncating the field",
);
}, "Body.formData() rejects multipart boundary prefixes with invalid suffixes");
promise_test(async function (t) {
const boundary = "chromium-disposition";
const duplicateBody =
`--${boundary}\r\n` +
"Content-Disposition: form-data; name=first; filename=first.txt; NAME=second; FILENAME=second.txt\r\n" +
"\r\n" +
"body\r\n" +
`--${boundary}--\r\n`;
const formData = await new Response(duplicateBody, {
headers: { "Content-Type": `multipart/form-data; boundary=${boundary}` },
}).formData();
const file = formData.get("second");
assert_true(file instanceof File, "the last duplicate name should select the file entry");
assert_equals(file.name, "second.txt", "the last duplicate filename should win");
for (const disposition of [
"form-data; name=",
'form-data; name="field";',
'form-data; malformed; name="field"',
"form-data; name=two words",
'FORM-DATA; name="field"',
]) {
const body =
`--${boundary}\r\n` +
`Content-Disposition: ${disposition}\r\n` +
"\r\n" +
"body\r\n" +
`--${boundary}--\r\n`;
const response = new Response(body, {
headers: { "Content-Type": `multipart/form-data; boundary=${boundary}` },
});
await promise_rejects_js(
t,
TypeError,
response.formData(),
`Chromium rejects malformed or differently-cased Content-Disposition: ${disposition}`,
);
}
}, "Body.formData() follows Chromium Content-Disposition parameter parsing");
promise_test(async function () {
const ordinaryBoundary = "chromium-framing";
const emptyResponse = new Response(`--${ordinaryBoundary}--\r\n`, {
headers: { "Content-Type": `multipart/form-data; boundary=${ordinaryBoundary}` },
});
const emptyFormData = await emptyResponse.formData();
assert_array_equals(
Array.from(emptyFormData.entries()),
[],
"a closing delimiter without parts should create empty FormData",
);
// Blink's MultipartParser::Finish accepts a partial CRLF after the complete
// closing "--". A lone CR is invalid RFC 2046 framing, but is observable in
// Chromium and therefore intentionally accepted here.
const partialCloseBody =
`--${ordinaryBoundary}\r\n` +
'Content-Disposition: form-data; name="field"\r\n' +
"\r\n" +
"value\r\n" +
`--${ordinaryBoundary}--\r`;
const partialCloseFormData = await new Response(partialCloseBody, {
headers: { "Content-Type": `multipart/form-data; boundary=${ordinaryBoundary}` },
}).formData();
assert_equals(partialCloseFormData.get("field"), "value", "a partial closing CRLF should parse");
// Chromium treats the non-empty boundary as literal parser input instead of
// enforcing RFC 2046's bchars, 70-byte, and no-trailing-space restrictions.
for (const boundary of ["quoted@boundary", "a".repeat(71), "trailing space "]) {
const body =
`--${boundary}\r\n` +
'Content-Disposition: form-data; name="field"\r\n' +
"\r\n" +
"value\r\n" +
`--${boundary}--\r\n`;
const formData = await new Response(body, {
headers: { "Content-Type": `multipart/form-data; boundary="${boundary}"` },
}).formData();
assert_equals(
formData.get("field"),
"value",
`Chromium-compatible non-RFC boundary ${JSON.stringify(boundary)} should parse`,
);
}
}, "Body.formData() follows multipart boundary and closing-delimiter compatibility");
promise_test(async function () {
const boundary = "decode-boundary";
const response = new Response(new Blob([
`--${boundary}\r\n`,
'Content-Disposition: form-data; name="string"\r\n',
"\r\nvalue", new Uint8Array([0xc2, 0xa0]), "1\r\n",
`--${boundary}\r\n`,
'Content-Disposition: form-data; name="default-charset"\r\n',
"Content-Type: text/plain; charset=utf-8\r\n",
"\r\nvalue", new Uint8Array([0xc2, 0xa0]), "2\r\n",
`--${boundary}\r\n`,
'Content-Disposition: form-data; name="non-default-charset"\r\n',
"Content-Type: text/plain; charset=iso-8859-1\r\n",
"\r\nvalue", new Uint8Array([0xc2, 0xa0]), "3\r\n",
`--${boundary}\r\n`,
'Content-Disposition: form-data; name="non-default-type"\r\n',
"Content-Type: application/octet-stream\r\n",
"\r\nvalue", new Uint8Array([0xc2, 0xa0]), "4\r\n",
`--${boundary}\r\n`,
'Content-Disposition: form-data; name="bom"\r\n',
"\r\n\uFEFFquick\uFEFFfox\uFEFF\r\n",
`--${boundary}\r\n`,
'Content-Disposition: form-data; name="file"; filename="file1"\r\n',
"Content-Type: application/octet-stream; x-param=x-value\r\n",
"\r\n", new Uint8Array([5, 0, 255]), "\r\n",
`--${boundary}\r\n`,
'Content-Disposition: form-data; name="file-without-type"; filename="file2"\r\n',
"\r\n", new Uint8Array([6, 0, 127, 255]), "\r\n",
`--${boundary}--\r\n`,
]), {
headers: [["Content-Type", `multipart/form-data; boundary="${boundary}"`]],
});
const formData = await response.formData();
const file = formData.get("file");
const fileWithoutType = formData.get("file-without-type");
assert_equals(formData.get("string"), "value\u00A01", "text part without Content-Type should decode as UTF-8");
assert_equals(formData.get("default-charset"), "value\u00A02", "text/plain charset=utf-8 should decode as UTF-8");
assert_equals(formData.get("non-default-charset"), "value\u00A03", "text/plain non-UTF-8 charset should still decode as UTF-8");
assert_equals(formData.get("non-default-type"), "value\u00A04", "non-file part with binary MIME should still decode as UTF-8 text");
assert_equals(formData.get("bom"), "\uFEFFquick\uFEFFfox\uFEFF", "multipart text values should preserve BOM code points");
assert_true(file instanceof File, "filename part should become a File");
assert_equals(file.name, "file1", "filename should be taken from Content-Disposition");
assert_equals(file.type, "application/octet-stream; x-param=x-value", "File.type should preserve Content-Type parameters");
assert_array_equals(new Uint8Array(await file.arrayBuffer()), [5, 0, 255], "file bytes should be preserved");
assert_true(fileWithoutType instanceof File, "filename part without Content-Type should still become a File");
assert_equals(fileWithoutType.name, "file2", "quoted filename should be preserved");
assert_equals(fileWithoutType.type, "text/plain", "missing file Content-Type should default to text/plain");
assert_array_equals(new Uint8Array(await fileWithoutType.arrayBuffer()), [6, 0, 127, 255], "untyped file bytes should be preserved");
}, "Body.formData() decodes multipart text parts as UTF-8 and preserves file metadata");
promise_test(async function () {
const boundary = "Boundary_with_capital_letters";
const body =
`--${boundary}\r\n` +
"Content-Type: application/json\r\n" +
'Content-Disposition: form-data; name="does_this_work"\r\n' +
"\r\n" +
"YES\r\n" +
`--${boundary}--\r\n`;
const formData = await new Response(new Blob([body]), {
headers: [["Content-Type", `multipart/form-data; boundary=${boundary}`]],
}).formData();
assert_equals(
formData.get("does_this_work"),
"YES",
"capital letters in the boundary parameter should be honored",
);
}, "Body.formData() parses multipart/form-data with capital letters in the boundary");
promise_test(async function (t) {
const boundary = "Boundary_with_capital_letters";
const body =
`--${boundary}\r\n` +
"Content-Type: application/json\r\n" +
'Content-Disposition: form-data; name="does_this_work"\r\n' +
"\r\n" +
"YES\r\n" +
`--${boundary}-Random junk`;
const response = new Response(new Blob([body]), {
headers: [["Content-Type", `multipart/form-data; boundary=${boundary}`]],
});
await promise_rejects_js(
t,
TypeError,
response.formData(),
"malformed multipart closing delimiter should reject",
);
}, "Body.formData() rejects malformed multipart/form-data boundary suffixes");
</script>