From 1d0f1ed446acd2db7c052503570c9249e7b841e9 Mon Sep 17 00:00:00 2001 From: Casper Beyer Date: Tue, 8 Sep 2020 17:46:15 +0800 Subject: [PATCH] fix: Empty Response body returns 0-byte array (#7387) --- cli/rt/24_body.js | 2 +- cli/tests/unit/fetch_test.ts | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/cli/rt/24_body.js b/cli/rt/24_body.js index a3348907b9..f755e2bad1 100644 --- a/cli/rt/24_body.js +++ b/cli/rt/24_body.js @@ -85,7 +85,7 @@ const enc = new TextEncoder(); return enc.encode(bodySource.toString()).buffer; } else if (!bodySource) { - return null; + return new ArrayBuffer(0); } throw new Error( `Body type not implemented: ${bodySource.constructor.name}`, diff --git a/cli/tests/unit/fetch_test.ts b/cli/tests/unit/fetch_test.ts index 012ce7b345..4bdb54d9eb 100644 --- a/cli/tests/unit/fetch_test.ts +++ b/cli/tests/unit/fetch_test.ts @@ -738,6 +738,16 @@ unitTest(function responseRedirect(): void { assertEquals(redir.type, "default"); }); +unitTest(async function responseWithoutBody(): Promise { + const response = new Response(); + assertEquals(await response.arrayBuffer(), new ArrayBuffer(0)); + assertEquals(await response.blob(), new Blob([])); + assertEquals(await response.text(), ""); + await assertThrowsAsync(async () => { + await response.json(); + }); +}); + unitTest({ perms: { net: true } }, async function fetchBodyReadTwice(): Promise< void > {