diff options
author | Chris Couzens <ccouzens@gmail.com> | 2020-06-24 04:56:05 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-23 23:56:05 -0400 |
commit | f6a41469730e8dcd0995d3e5dd370b9410c65ba4 (patch) | |
tree | 4c1f6b42232be2ab8d0569df9a1bbf4d7da5472c /cli | |
parent | 49c54c0805ab26410a62e0251fee3a28b98c0e13 (diff) |
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)
```
Diffstat (limited to 'cli')
-rw-r--r-- | cli/js/web/body.ts | 7 | ||||
-rw-r--r-- | cli/tests/unit/body_test.ts | 7 |
2 files changed, 13 insertions, 1 deletions
diff --git a/cli/js/web/body.ts b/cli/js/web/body.ts index 6e376de37..8f5b48ad8 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 a6df3102e..df824e1ae 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(), "<Deno>"); } ); + +unitTest({ perms: {} }, async function bodyURLSearchParams(): Promise<void> { + const body = buildBody(new URLSearchParams({ hello: "world" })); + + const text = await body.text(); + assertEquals(text, "hello=world"); +}); |