Skip to content

Commit 50f0625

Browse files
committed
chore(tests): Add formData.js integration tests
1 parent 4051ee1 commit 50f0625

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { serveTest } from "../test-server.js";
2+
import { strictEqual, assert } from "../../assert.js";
3+
4+
5+
6+
export const handler = serveTest(async (t) => {
7+
await t.test("encode-form-data-simple", async () => {
8+
let fd = new FormData();
9+
fd.set("name", "value");
10+
fd.set("key", "pair");
11+
let req = new Request("", {
12+
method: "POST",
13+
body: fd,
14+
});
15+
let bytes = await req.text();
16+
console.log(bytes);
17+
strictEqual(req.headers.get("content-length"), "250")
18+
assert(req.headers.get("content-type")?.startsWith("multipart/form-data;"));
19+
});
20+
await t.test("encode-form-data-with-new-lines", async () => {
21+
let fd = new FormData();
22+
fd.set("name", "line1\nline2\rline3\r\nline4\n\n\n");
23+
let req = new Request("", {
24+
method: "POST",
25+
body: fd,
26+
});
27+
"".charCodeAt
28+
let bytes = await req.text();
29+
console.log(bytes);
30+
strictEqual(req.headers.get("content-length"), "177")
31+
assert(req.headers.get("content-type")?.startsWith("multipart/form-data;"));
32+
});
33+
await t.test("encode-form-data-utf8", async () => {
34+
let fd = new FormData();
35+
fd.set("emoji", "😁🇧🇴🇮🇴");
36+
fd.set("cjk", "素晴らしい");
37+
let req = new Request("", {
38+
method: "POST",
39+
body: fd,
40+
});
41+
let bytes = await req.text();
42+
console.log(bytes);
43+
strictEqual(req.headers.get("content-length"), "277")
44+
assert(req.headers.get("content-type")?.startsWith("multipart/form-data;"));
45+
});
46+
await t.test("encode-form-data-file", async () => {
47+
let fd = new FormData();
48+
const len = 1024;
49+
let arr = new Uint8Array(len);
50+
arr.fill(42);
51+
let b = new Blob([arr]);
52+
let f = new File([b], "file.txt");
53+
fd.set("file1", f);
54+
let req = new Request("", {
55+
method: "POST",
56+
body: fd,
57+
});
58+
// ensure the body is encoded
59+
await req.text();
60+
// console.log(bytes);
61+
strictEqual(req.headers.get("content-length"), "1231")
62+
assert(req.headers.get("content-type")?.startsWith("multipart/form-data;"));
63+
});
64+
});

tests/integration/handlers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export { handler as performance } from './performance/performance.js';
44
export { handler as crypto } from './crypto/crypto.js';
55
export { handler as timers } from './timers/timers.js';
66
export { handler as fetch } from './fetch/fetch.js';
7+
export { handler as formData } from './formData/formData.js';

tests/tests.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,4 @@ test_integration(crypto)
5050
test_integration(fetch)
5151
test_integration(performance)
5252
test_integration(timers)
53+
test_integration(formData)

0 commit comments

Comments
 (0)