summaryrefslogtreecommitdiff
path: root/js/body_test.ts
diff options
context:
space:
mode:
authorKurt Mackey <mrkurt@gmail.com>2019-05-01 22:56:42 -0500
committerRyan Dahl <ry@tinyclouds.org>2019-05-01 23:56:42 -0400
commitc05cbc8eac91a9e1ab9b87c688ac4392eff01445 (patch)
treecc6e1e2b789ba6626c33523fa488681c86c1a987 /js/body_test.ts
parent1dd30f658fc031dd1d3cdb5b4c435ff0e48740c9 (diff)
Add Request global constructor (#2253)
Diffstat (limited to 'js/body_test.ts')
-rw-r--r--js/body_test.ts60
1 files changed, 60 insertions, 0 deletions
diff --git a/js/body_test.ts b/js/body_test.ts
new file mode 100644
index 000000000..ac63ae78a
--- /dev/null
+++ b/js/body_test.ts
@@ -0,0 +1,60 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+import { test, testPerm, assertEquals, assert } from "./test_util.ts";
+
+// just a hack to get a body object
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+function buildBody(body: any): domTypes.Body {
+ const stub = new Request("", {
+ body: body
+ });
+ return stub as domTypes.Body;
+}
+
+const intArrays = [
+ Int8Array,
+ Int16Array,
+ Int32Array,
+ Uint8Array,
+ Uint16Array,
+ Uint32Array,
+ Uint8ClampedArray,
+ Float32Array,
+ Float64Array
+];
+test(async function arrayBufferFromByteArrays(): Promise<void> {
+ const buffer = new TextEncoder().encode("ahoyhoy8").buffer;
+
+ for (const type of intArrays) {
+ const body = buildBody(new type(buffer));
+ const text = new TextDecoder("utf-8").decode(await body.arrayBuffer());
+ assertEquals(text, "ahoyhoy8");
+ }
+});
+
+//FormData
+testPerm({ net: true }, async function bodyMultipartFormData(): Promise<void> {
+ const response = await fetch(
+ "http://localhost:4545/tests/subdir/multipart_form_data.txt"
+ );
+ const text = await response.text();
+
+ const body = buildBody(text);
+ const formData = await body.formData();
+ assert(formData.has("field_1"));
+ assertEquals(formData.get("field_1").toString(), "value_1 \r\n");
+ assert(formData.has("field_2"));
+});
+
+testPerm({ net: true }, async function bodyURLEncodedFormData(): Promise<void> {
+ const response = await fetch(
+ "http://localhost:4545/tests/subdir/form_urlencoded.txt"
+ );
+ const text = await response.text();
+
+ const body = buildBody(text);
+ const formData = await body.formData();
+ assert(formData.has("field_1"));
+ assertEquals(formData.get("field_1").toString(), "Hi");
+ assert(formData.has("field_2"));
+ assertEquals(formData.get("field_2").toString(), "<Deno>");
+});