From 5c2e499c3a70ec58b7f5adab3a7801d601f9b2e1 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Mon, 21 Sep 2020 22:07:41 +0200 Subject: [PATCH] fix: Response.arrayBuffer() doesn't return promise (#7618) --- cli/tests/unit/response_test.ts | 51 +++++++++++++++++++++++++++++++++ cli/tests/unit/unit_tests.ts | 1 + op_crates/fetch/26_fetch.js | 2 +- 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 cli/tests/unit/response_test.ts diff --git a/cli/tests/unit/response_test.ts b/cli/tests/unit/response_test.ts new file mode 100644 index 0000000000..13a8570d7b --- /dev/null +++ b/cli/tests/unit/response_test.ts @@ -0,0 +1,51 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { unitTest, assert, assertEquals } from "./test_util.ts"; + +unitTest(async function responseText() { + const response = new Response("hello world"); + const textPromise = response.text(); + assert(textPromise instanceof Promise); + const text = await textPromise; + assert(typeof text === "string"); + assertEquals(text, "hello world"); +}); + +unitTest(async function responseArrayBuffer() { + const response = new Response(new Uint8Array([1, 2, 3])); + const arrayBufferPromise = response.arrayBuffer(); + assert(arrayBufferPromise instanceof Promise); + const arrayBuffer = await arrayBufferPromise; + assert(arrayBuffer instanceof ArrayBuffer); + assertEquals(new Uint8Array(arrayBuffer), new Uint8Array([1, 2, 3])); +}); + +unitTest(async function responseJson() { + const response = new Response('{"hello": "world"}'); + const jsonPromise = response.json(); + assert(jsonPromise instanceof Promise); + const json = await jsonPromise; + assert(json instanceof Object); + assertEquals(json, { hello: "world" }); +}); + +unitTest(async function responseBlob() { + const response = new Response(new Uint8Array([1, 2, 3])); + const blobPromise = response.blob(); + assert(blobPromise instanceof Promise); + const blob = await blobPromise; + assert(blob instanceof Blob); + assertEquals(blob, new Blob([new Uint8Array([1, 2, 3])])); +}); + +unitTest(async function responseFormData() { + const input = new FormData(); + input.append("hello", "world"); + const response = new Response(input, { + headers: { "content-type": "application/x-www-form-urlencoded" }, + }); + const formDataPromise = response.formData(); + assert(formDataPromise instanceof Promise); + const formData = await formDataPromise; + assert(formData instanceof FormData); + assertEquals(formData, input); +}); diff --git a/cli/tests/unit/unit_tests.ts b/cli/tests/unit/unit_tests.ts index 03f0c6f1c5..e97c485a85 100644 --- a/cli/tests/unit/unit_tests.ts +++ b/cli/tests/unit/unit_tests.ts @@ -54,6 +54,7 @@ import "./remove_test.ts"; import "./rename_test.ts"; import "./request_test.ts"; import "./resources_test.ts"; +import "./response_test.ts"; import "./signal_test.ts"; import "./stat_test.ts"; import "./stdio_test.ts"; diff --git a/op_crates/fetch/26_fetch.js b/op_crates/fetch/26_fetch.js index 4b31110d64..9916a4f82c 100644 --- a/op_crates/fetch/26_fetch.js +++ b/op_crates/fetch/26_fetch.js @@ -869,7 +869,7 @@ if (this._bodySource instanceof ReadableStream) { return bufferFromStream(this._bodySource.getReader(), this.#size); } - return bodyToArrayBuffer(this._bodySource); + return Promise.resolve(bodyToArrayBuffer(this._bodySource)); } }