diff options
author | Marcos Casagrande <marcoscvp90@gmail.com> | 2020-06-08 18:08:26 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-08 18:08:26 +0200 |
commit | d9071339443a1da3fbb4c65fa19b4f65328da2f3 (patch) | |
tree | f31b69e1701ba445870e0505e4d034005b4ea8f0 /cli/tests/unit/fetch_test.ts | |
parent | 4feccdd3b7d7b1c4ef63c4ccc572a52403135df0 (diff) |
fix(cli/web/fetch): multipart/form-data request body support for binary files (#5886)
Diffstat (limited to 'cli/tests/unit/fetch_test.ts')
-rw-r--r-- | cli/tests/unit/fetch_test.ts | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/cli/tests/unit/fetch_test.ts b/cli/tests/unit/fetch_test.ts index 5544eee24..98ff42737 100644 --- a/cli/tests/unit/fetch_test.ts +++ b/cli/tests/unit/fetch_test.ts @@ -268,6 +268,60 @@ unitTest( ); unitTest( + { perms: { net: true } }, + async function fetchInitFormDataMultipleFilesBody(): Promise<void> { + const files = [ + { + // prettier-ignore + content: new Uint8Array([137,80,78,71,13,10,26,10, 137, 1, 25]), + type: "image/png", + name: "image", + fileName: "some-image.png", + }, + { + // prettier-ignore + content: new Uint8Array([108,2,0,0,145,22,162,61,157,227,166,77,138,75,180,56,119,188,177,183]), + name: "file", + fileName: "file.bin", + expectedType: "application/octet-stream", + }, + { + content: new TextEncoder().encode("deno land"), + type: "text/plain", + name: "text", + fileName: "deno.txt", + }, + ]; + const form = new FormData(); + form.append("field", "value"); + for (const file of files) { + form.append( + file.name, + new Blob([file.content], { type: file.type }), + file.fileName + ); + } + const response = await fetch("http://localhost:4545/echo_server", { + method: "POST", + body: form, + }); + const resultForm = await response.formData(); + assertEquals(form.get("field"), resultForm.get("field")); + for (const file of files) { + const inputFile = form.get(file.name) as File; + const resultFile = resultForm.get(file.name) as File; + assertEquals(inputFile.size, resultFile.size); + assertEquals(inputFile.name, resultFile.name); + assertEquals(file.expectedType || file.type, resultFile.type); + assertEquals( + new Uint8Array(await resultFile.arrayBuffer()), + file.content + ); + } + } +); + +unitTest( { perms: { net: true }, }, @@ -427,6 +481,36 @@ unitTest( } ); +unitTest( + { perms: { net: true } }, + async function fetchInitFormDataTextFileBody(): Promise<void> { + const fileContent = "deno land"; + const form = new FormData(); + form.append("field", "value"); + form.append( + "file", + new Blob([new TextEncoder().encode(fileContent)], { + type: "text/plain", + }), + "deno.txt" + ); + const response = await fetch("http://localhost:4545/echo_server", { + method: "POST", + body: form, + }); + const resultForm = await response.formData(); + assertEquals(form.get("field"), resultForm.get("field")); + + const file = form.get("file") as File; + const resultFile = resultForm.get("file") as File; + + assertEquals(file.size, resultFile.size); + assertEquals(file.name, resultFile.name); + assertEquals(file.type, resultFile.type); + assertEquals(await file.text(), await resultFile.text()); + } +); + unitTest({ perms: { net: true } }, async function fetchUserAgent(): Promise< void > { |