2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-27 06:22:32 -04:00
|
|
|
import { assert, assertEquals, unitTest } from "./test_util.ts";
|
2019-05-01 23:56:42 -04:00
|
|
|
|
|
|
|
// just a hack to get a body object
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2020-07-07 22:25:34 -04:00
|
|
|
function buildBody(body: any, headers?: Headers): Body {
|
2020-10-09 01:12:44 -04:00
|
|
|
const stub = new Request("http://foo/", {
|
2020-03-28 13:03:49 -04:00
|
|
|
body: body,
|
2020-07-07 22:25:34 -04:00
|
|
|
headers,
|
2021-04-20 08:47:22 -04:00
|
|
|
method: "POST",
|
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
|
|
|
];
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest(async function arrayBufferFromByteArrays() {
|
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(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function bodyMultipartFormData() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const response = await fetch(
|
2020-07-14 15:24:17 -04:00
|
|
|
"http://localhost:4545/multipart_form_data.txt",
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2020-08-20 11:47:58 -04:00
|
|
|
assert(response.body instanceof ReadableStream);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
const text = await response.text();
|
2019-05-01 23:56:42 -04:00
|
|
|
|
2020-07-07 22:25:34 -04:00
|
|
|
const body = buildBody(text, response.headers);
|
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"));
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-05-01 23:56:42 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { net: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function bodyURLEncodedFormData() {
|
2020-03-04 11:31:14 -05:00
|
|
|
const response = await fetch(
|
2021-08-11 10:20:47 -04:00
|
|
|
"http://localhost:4545/subdir/form_urlencoded.txt",
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2020-08-20 11:47:58 -04:00
|
|
|
assert(response.body instanceof ReadableStream);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
const text = await response.text();
|
2019-05-01 23:56:42 -04:00
|
|
|
|
2020-07-07 22:25:34 -04:00
|
|
|
const body = buildBody(text, response.headers);
|
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>");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2020-06-23 23:56:05 -04:00
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
unitTest({ permissions: {} }, async function bodyURLSearchParams() {
|
2020-06-23 23:56:05 -04:00
|
|
|
const body = buildBody(new URLSearchParams({ hello: "world" }));
|
|
|
|
|
|
|
|
const text = await body.text();
|
|
|
|
assertEquals(text, "hello=world");
|
|
|
|
});
|
2020-06-28 10:31:56 -04:00
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest(async function bodyArrayBufferMultipleParts() {
|
2020-06-28 10:31:56 -04:00
|
|
|
const parts: Uint8Array[] = [];
|
|
|
|
let size = 0;
|
|
|
|
for (let i = 0; i <= 150000; i++) {
|
|
|
|
const part = new Uint8Array([1]);
|
|
|
|
parts.push(part);
|
|
|
|
size += part.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
let offset = 0;
|
|
|
|
const stream = new ReadableStream({
|
2021-08-05 07:08:58 -04:00
|
|
|
pull(controller) {
|
2020-06-28 10:31:56 -04:00
|
|
|
// parts.shift() takes forever: https://github.com/denoland/deno/issues/5259
|
|
|
|
const chunk = parts[offset++];
|
|
|
|
if (!chunk) return controller.close();
|
|
|
|
controller.enqueue(chunk);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const body = buildBody(stream);
|
|
|
|
assertEquals((await body.arrayBuffer()).byteLength, size);
|
|
|
|
});
|