From 8d452d74fa6f63eb0bce0567083d375b50329de4 Mon Sep 17 00:00:00 2001 From: "Kevin (Kun) \"Kassimo\" Qian" Date: Thu, 3 Jan 2019 06:41:20 -0500 Subject: Support more fetch init body types (#1449) --- js/fetch_test.ts | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'js/fetch_test.ts') diff --git a/js/fetch_test.ts b/js/fetch_test.ts index 773b2eebd..582f92839 100644 --- a/js/fetch_test.ts +++ b/js/fetch_test.ts @@ -84,6 +84,57 @@ testPerm({ net: true }, async function fetchURLEncodedFormDataSuccess() { assertEqual(formData.get("field_2").toString(), ""); }); +testPerm({ net: true }, async function fetchInitStringBody() { + const data = "Hello World"; + const response = await fetch("http://localhost:4545/echo_server", { + method: "POST", + body: data + }); + const text = await response.text(); + assertEqual(text, data); + assert(response.headers.get("content-type").startsWith("text/plain")); +}); + +testPerm({ net: true }, async function fetchInitTypedArrayBody() { + const data = "Hello World"; + const response = await fetch("http://localhost:4545/echo_server", { + method: "POST", + body: new TextEncoder().encode(data) + }); + const text = await response.text(); + assertEqual(text, data); +}); + +testPerm({ net: true }, async function fetchInitURLSearchParamsBody() { + const data = "param1=value1¶m2=value2"; + const params = new URLSearchParams(data); + const response = await fetch("http://localhost:4545/echo_server", { + method: "POST", + body: params + }); + const text = await response.text(); + assertEqual(text, data); + assert( + response.headers + .get("content-type") + .startsWith("application/x-www-form-urlencoded") + ); +}); + +testPerm({ net: true }, async function fetchInitBlobBody() { + const data = "const a = 1"; + const blob = new Blob([data], { + type: "text/javascript" + }); + const response = await fetch("http://localhost:4545/echo_server", { + method: "POST", + body: blob + }); + const text = await response.text(); + assertEqual(text, data); + assert(response.headers.get("content-type").startsWith("text/javascript")); +}); + // TODO(ry) The following tests work but are flaky. There's a race condition // somewhere. Here is what one of these flaky failures looks like: // -- cgit v1.2.3