Skip to content

Commit 0d7dd6d

Browse files
add tests for multipart/form-data (#115)
* chore: add tests for `multipart/form-data` * chore: apply automated updates * chore: cleanup --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 106bab9 commit 0d7dd6d

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

server/public/data.pdf

3.91 KB
Binary file not shown.

server/routes/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { baseURL } = useRuntimeConfig().app;
44

55
const withBase = (p) => baseURL + p.replace(/^\//, "");
66

7-
const tests = ["api", "form-data"];
7+
const tests = ["api", "form-data", "multipart-form-data"];
88

99
const manualTests = ["env", "node-compat", "headers"];
1010

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// https://github.com/nitrojs/nitro/issues/1721
2+
export default defineTestHandler(
3+
"multipart-form-data",
4+
async (event) => {
5+
const formData = await readMultipartFormData(event);
6+
const name = formData
7+
.find((partData) => partData.name === "name")
8+
?.data.toString();
9+
const rawFile = formData.find((partData) => partData.name === "file");
10+
11+
return {
12+
data: {
13+
name,
14+
fileName: rawFile.filename,
15+
fileType: rawFile.type,
16+
fileSize: rawFile.data.byteLength,
17+
},
18+
};
19+
},
20+
async (assert) => {
21+
const formData = new FormData();
22+
formData.append("name", "John Doe");
23+
24+
const rawFile = await fetch("/data.pdf").then((res) => res.arrayBuffer());
25+
26+
const file = new Blob([rawFile], { type: "application/pdf" });
27+
formData.append("file", file, "data.pdf");
28+
29+
const res = await fetch("", { method: "POST", body: formData }).then(
30+
(res) => res.json(),
31+
);
32+
assert(res.data.name === "John Doe", `Unexpected response: ${res.data}`);
33+
assert(
34+
res.data.fileSize === rawFile.byteLength,
35+
`Unexpected response: ${res.data}`,
36+
);
37+
},
38+
);

0 commit comments

Comments
 (0)