2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-03-04 11:31:14 -05:00
|
|
|
import { unitTest, assertEquals, assert } from "./test_util.ts";
|
2019-05-01 23:56:42 -04:00
|
|
|
|
|
|
|
// just a hack to get a body object
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2019-09-10 10:57:17 -04:00
|
|
|
function buildBody(body: any): Body {
|
2019-05-01 23:56:42 -04:00
|
|
|
const stub = new Request("", {
|
2020-03-28 13:03:49 -04:00
|
|
|
body: body,
|
2019-05-01 23:56:42 -04:00
|
|
|
});
|
2019-09-10 10:57:17 -04:00
|
|
|
return stub as Body;
|
2019-05-01 23:56:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const intArrays = [
|
|
|
|
Int8Array,
|
|
|
|
Int16Array,
|
|
|
|
Int32Array,
|
|
|
|
Uint8Array,
|
|
|
|
Uint16Array,
|
|
|
|
Uint32Array,
|
|
|
|
Uint8ClampedArray,
|
|
|
|
Float32Array,
|
2020-03-28 13:03:49 -04:00
|
|
|
Float64Array,
|
2019-05-01 23:56:42 -04:00
|
|
|
];
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(async function arrayBufferFromByteArrays(): Promise<void> {
|
2019-05-01 23:56:42 -04:00
|
|
|
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
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function bodyMultipartFormData(): Promise<void> {
|
|
|
|
const response = await fetch(
|
|
|
|
"http://localhost:4545/cli/tests/subdir/multipart_form_data.txt"
|
|
|
|
);
|
|
|
|
const text = await response.text();
|
2019-05-01 23:56:42 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
const body = buildBody(text);
|
2019-05-03 00:52:50 -04:00
|
|
|
|
2020-05-26 10:02:16 -04:00
|
|
|
// @ts-expect-error
|
2020-03-04 11:31:14 -05:00
|
|
|
body.contentType = "multipart/form-data;boundary=boundary";
|
2019-05-03 00:52:50 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
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"));
|
|
|
|
}
|
|
|
|
);
|
2019-05-01 23:56:42 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { net: true } },
|
|
|
|
async function bodyURLEncodedFormData(): Promise<void> {
|
|
|
|
const response = await fetch(
|
|
|
|
"http://localhost:4545/cli/tests/subdir/form_urlencoded.txt"
|
|
|
|
);
|
|
|
|
const text = await response.text();
|
2019-05-01 23:56:42 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
const body = buildBody(text);
|
2019-05-03 00:52:50 -04:00
|
|
|
|
2020-05-26 10:02:16 -04:00
|
|
|
// @ts-expect-error
|
2020-03-04 11:31:14 -05:00
|
|
|
body.contentType = "application/x-www-form-urlencoded";
|
2019-05-03 00:52:50 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
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>");
|
|
|
|
}
|
|
|
|
);
|