From f6a41469730e8dcd0995d3e5dd370b9410c65ba4 Mon Sep 17 00:00:00 2001 From: Chris Couzens Date: Wed, 24 Jun 2020 04:56:05 +0100 Subject: [PATCH] fix(cli/web): Support URLSearchParam as Body (#6416) The following used to fail in Deno despite working in the browser: ```javascript new Request('http://localhost/', {method: 'POST', body: new URLSearchParams({hello: 'world'})}).text().then(console.log) ``` --- cli/js/web/body.ts | 7 ++++++- cli/tests/unit/body_test.ts | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/cli/js/web/body.ts b/cli/js/web/body.ts index 6e376de37f..8f5b48ad8a 100644 --- a/cli/js/web/body.ts +++ b/cli/js/web/body.ts @@ -25,6 +25,8 @@ function validateBodyType(owner: Body, bodySource: BodyInit | null): boolean { return true; } else if (bodySource instanceof FormData) { return true; + } else if (bodySource instanceof URLSearchParams) { + return true; } else if (!bodySource) { return true; // null body is fine } @@ -193,7 +195,10 @@ export class Body implements domTypes.Body { ); } else if (this._bodySource instanceof ReadableStreamImpl) { return bufferFromStream(this._bodySource.getReader()); - } else if (this._bodySource instanceof FormData) { + } else if ( + this._bodySource instanceof FormData || + this._bodySource instanceof URLSearchParams + ) { const enc = new TextEncoder(); return Promise.resolve( enc.encode(this._bodySource.toString()).buffer as ArrayBuffer diff --git a/cli/tests/unit/body_test.ts b/cli/tests/unit/body_test.ts index a6df3102e6..df824e1ae4 100644 --- a/cli/tests/unit/body_test.ts +++ b/cli/tests/unit/body_test.ts @@ -72,3 +72,10 @@ unitTest( assertEquals(formData.get("field_2")!.toString(), ""); } ); + +unitTest({ perms: {} }, async function bodyURLSearchParams(): Promise { + const body = buildBody(new URLSearchParams({ hello: "world" })); + + const text = await body.text(); + assertEquals(text, "hello=world"); +});