2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-11-23 11:45:18 -05:00
|
|
|
import { assert, assertEquals } 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-11-23 11:45:18 -05:00
|
|
|
Deno.test(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
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
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
|
|
|
|
2023-05-16 11:49:35 -04:00
|
|
|
// FormData: non-ASCII names and filenames
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function bodyMultipartFormDataNonAsciiNames() {
|
|
|
|
const boundary = "----01230123";
|
|
|
|
const payload = [
|
|
|
|
`--${boundary}`,
|
|
|
|
`Content-Disposition: form-data; name="文字"`,
|
|
|
|
"",
|
|
|
|
"文字",
|
|
|
|
`--${boundary}`,
|
|
|
|
`Content-Disposition: form-data; name="file"; filename="文字"`,
|
|
|
|
"Content-Type: application/octet-stream",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
`--${boundary}--`,
|
|
|
|
].join("\r\n");
|
|
|
|
|
|
|
|
const body = buildBody(
|
|
|
|
new TextEncoder().encode(payload),
|
|
|
|
new Headers({
|
|
|
|
"Content-Type": `multipart/form-data; boundary=${boundary}`,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
const formData = await body.formData();
|
|
|
|
assert(formData.has("文字"));
|
|
|
|
assertEquals(formData.get("文字"), "文字");
|
|
|
|
assert(formData.has("file"));
|
|
|
|
assert(formData.get("file") instanceof File);
|
|
|
|
assertEquals((formData.get("file") as File).name, "文字");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
// FormData: non-ASCII names and filenames roundtrip
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function bodyMultipartFormDataNonAsciiRoundtrip() {
|
|
|
|
const inFormData = new FormData();
|
|
|
|
inFormData.append("文字", "文字");
|
|
|
|
inFormData.append("file", new File([], "文字"));
|
|
|
|
|
|
|
|
const body = buildBody(inFormData);
|
|
|
|
|
|
|
|
const formData = await body.formData();
|
|
|
|
assert(formData.has("文字"));
|
|
|
|
assertEquals(formData.get("文字"), "文字");
|
|
|
|
assert(formData.has("file"));
|
|
|
|
assert(formData.get("file") instanceof File);
|
|
|
|
assertEquals((formData.get("file") as File).name, "文字");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
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-11-23 11:45:18 -05:00
|
|
|
Deno.test({ 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-11-23 11:45:18 -05:00
|
|
|
Deno.test(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);
|
|
|
|
});
|
2023-10-05 13:28:44 -04:00
|
|
|
|
|
|
|
// https://github.com/denoland/deno/issues/20793
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: true } },
|
|
|
|
async function bodyMultipartFormDataMultipleHeaders() {
|
|
|
|
const boundary = "----formdata-polyfill-0.970665446687947";
|
|
|
|
const payload = [
|
|
|
|
"------formdata-polyfill-0.970665446687947",
|
|
|
|
'Content-Disposition: form-data; name="x"; filename="blob"',
|
|
|
|
"Content-Length: 1",
|
|
|
|
"Content-Type: application/octet-stream",
|
|
|
|
"last-modified: Wed, 04 Oct 2023 20:28:45 GMT",
|
|
|
|
"",
|
|
|
|
"y",
|
|
|
|
"------formdata-polyfill-0.970665446687947--",
|
|
|
|
].join("\r\n");
|
|
|
|
|
|
|
|
const body = buildBody(
|
|
|
|
new TextEncoder().encode(payload),
|
|
|
|
new Headers({
|
|
|
|
"Content-Type": `multipart/form-data; boundary=${boundary}`,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
const formData = await body.formData();
|
|
|
|
const file = formData.get("x");
|
|
|
|
assert(file instanceof File);
|
|
|
|
const text = await file.text();
|
|
|
|
assertEquals(text, "y");
|
|
|
|
assertEquals(file.size, 1);
|
|
|
|
},
|
|
|
|
);
|