forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(formdata): handle file names correctly when setting on formdata (o…
…ven-sh#12379) Co-authored-by: Andrew Johnston <andrew@bun.sh>
- Loading branch information
1 parent
6f52b64
commit 050a4b5
Showing
2 changed files
with
67 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { expect, test } from "bun:test"; | ||
|
||
// https://github.com/oven-sh/bun/issues/12325 | ||
|
||
test("formdata set with File works as expected", async () => { | ||
const expected = ["617580375", "text-notes1.txt"]; | ||
|
||
using server = Bun.serve({ | ||
port: 0, | ||
fetch: async req => { | ||
const data = await req.formData(); | ||
const chat_id = data.get("chat_id"); | ||
const document = data.get("document"); | ||
expect(chat_id).toEqual(expected[0]); | ||
expect(document.name).toEqual(expected[1]); | ||
return new Response(""); | ||
}, | ||
}); | ||
|
||
async function sendDocument(body) { | ||
const response = await fetch(server.url, { | ||
method: "POST", | ||
body: body, | ||
}); | ||
const text = await response.text(); | ||
return text; | ||
} | ||
|
||
const formDataSet = new FormData(); | ||
formDataSet.set("chat_id", expected[0]); | ||
formDataSet.set("document", new File(["some text notes 1"], expected[1])); | ||
await sendDocument(formDataSet); | ||
}); | ||
|
||
test("formdata apppend with File works as expected", async () => { | ||
const expected = ["617580376", "text-notes2.txt"]; | ||
|
||
using server = Bun.serve({ | ||
port: 0, | ||
fetch: async req => { | ||
const data = await req.formData(); | ||
const chat_id = data.get("chat_id"); | ||
const document = data.get("document"); | ||
expect(chat_id).toEqual(expected[0]); | ||
expect(document.name).toEqual(expected[1]); | ||
return new Response(""); | ||
}, | ||
}); | ||
|
||
async function sendDocument(body) { | ||
const response = await fetch(server.url, { | ||
method: "POST", | ||
body: body, | ||
}); | ||
const text = await response.text(); | ||
return text; | ||
} | ||
|
||
const formDataSet = new FormData(); | ||
formDataSet.append("chat_id", expected[0]); | ||
formDataSet.append("document", new File(["some text notes 2"], expected[1])); | ||
await sendDocument(formDataSet); | ||
}); |