diff options
author | Marcos Casagrande <marcoscvp90@gmail.com> | 2020-05-25 15:26:36 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-25 09:26:36 -0400 |
commit | 1c4a9665e2a2ff85ccb8060f168dafafa4d2194b (patch) | |
tree | 640e4d2d9e743b8dec8430f9ad9e4fd3751bf38e | |
parent | c9f0e34e294241541ba59c3a7eb52f42df7ff993 (diff) |
fix: Allow ArrayBuffer as Fetch request body (#5831)
-rw-r--r-- | cli/js/web/fetch.ts | 2 | ||||
-rw-r--r-- | cli/tests/unit/fetch_test.ts | 13 |
2 files changed, 15 insertions, 0 deletions
diff --git a/cli/js/web/fetch.ts b/cli/js/web/fetch.ts index 3485a770a..9054465bf 100644 --- a/cli/js/web/fetch.ts +++ b/cli/js/web/fetch.ts @@ -505,6 +505,8 @@ export async function fetch( contentType = "text/plain;charset=UTF-8"; } else if (isTypedArray(init.body)) { body = init.body; + } else if (init.body instanceof ArrayBuffer) { + body = new Uint8Array(init.body); } else if (init.body instanceof URLSearchParams) { body = new TextEncoder().encode(init.body.toString()); contentType = "application/x-www-form-urlencoded;charset=UTF-8"; diff --git a/cli/tests/unit/fetch_test.ts b/cli/tests/unit/fetch_test.ts index 458c34ae2..db4c3a407 100644 --- a/cli/tests/unit/fetch_test.ts +++ b/cli/tests/unit/fetch_test.ts @@ -260,6 +260,19 @@ unitTest( unitTest( { perms: { net: true } }, + async function fetchInitArrayBufferBody(): Promise<void> { + const data = "Hello World"; + const response = await fetch("http://localhost:4545/echo_server", { + method: "POST", + body: new TextEncoder().encode(data).buffer, + }); + const text = await response.text(); + assertEquals(text, data); + } +); + +unitTest( + { perms: { net: true } }, async function fetchInitURLSearchParamsBody(): Promise<void> { const data = "param1=value1¶m2=value2"; const params = new URLSearchParams(data); |