2024-01-01 19:58:21 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-07-08 13:43:36 +00:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
|
|
|
assertStringIncludes,
|
2021-10-11 21:43:52 +00:00
|
|
|
assertThrows,
|
2021-07-08 13:43:36 +00:00
|
|
|
} from "./test_util.ts";
|
2020-09-21 20:07:41 +00:00
|
|
|
|
2021-11-23 16:45:18 +00:00
|
|
|
Deno.test(async function responseText() {
|
2020-09-21 20:07:41 +00:00
|
|
|
const response = new Response("hello world");
|
|
|
|
const textPromise = response.text();
|
|
|
|
assert(textPromise instanceof Promise);
|
|
|
|
const text = await textPromise;
|
|
|
|
assert(typeof text === "string");
|
|
|
|
assertEquals(text, "hello world");
|
|
|
|
});
|
|
|
|
|
2021-11-23 16:45:18 +00:00
|
|
|
Deno.test(async function responseArrayBuffer() {
|
2020-09-21 20:07:41 +00:00
|
|
|
const response = new Response(new Uint8Array([1, 2, 3]));
|
|
|
|
const arrayBufferPromise = response.arrayBuffer();
|
|
|
|
assert(arrayBufferPromise instanceof Promise);
|
|
|
|
const arrayBuffer = await arrayBufferPromise;
|
|
|
|
assert(arrayBuffer instanceof ArrayBuffer);
|
|
|
|
assertEquals(new Uint8Array(arrayBuffer), new Uint8Array([1, 2, 3]));
|
|
|
|
});
|
|
|
|
|
2021-11-23 16:45:18 +00:00
|
|
|
Deno.test(async function responseJson() {
|
2020-09-21 20:07:41 +00:00
|
|
|
const response = new Response('{"hello": "world"}');
|
|
|
|
const jsonPromise = response.json();
|
|
|
|
assert(jsonPromise instanceof Promise);
|
|
|
|
const json = await jsonPromise;
|
|
|
|
assert(json instanceof Object);
|
|
|
|
assertEquals(json, { hello: "world" });
|
|
|
|
});
|
|
|
|
|
2021-11-23 16:45:18 +00:00
|
|
|
Deno.test(async function responseBlob() {
|
2020-09-21 20:07:41 +00:00
|
|
|
const response = new Response(new Uint8Array([1, 2, 3]));
|
|
|
|
const blobPromise = response.blob();
|
|
|
|
assert(blobPromise instanceof Promise);
|
|
|
|
const blob = await blobPromise;
|
|
|
|
assert(blob instanceof Blob);
|
2021-07-05 13:34:37 +00:00
|
|
|
assertEquals(blob.size, 3);
|
|
|
|
assertEquals(await blob.arrayBuffer(), new Uint8Array([1, 2, 3]).buffer);
|
2020-09-21 20:07:41 +00:00
|
|
|
});
|
|
|
|
|
2021-11-23 16:45:18 +00:00
|
|
|
Deno.test(async function responseFormData() {
|
2020-09-21 20:07:41 +00:00
|
|
|
const input = new FormData();
|
|
|
|
input.append("hello", "world");
|
2021-07-19 22:11:50 +00:00
|
|
|
const response = new Response(input);
|
|
|
|
const contentType = response.headers.get("content-type")!;
|
|
|
|
assert(contentType.startsWith("multipart/form-data"));
|
2020-09-21 20:07:41 +00:00
|
|
|
const formDataPromise = response.formData();
|
|
|
|
assert(formDataPromise instanceof Promise);
|
|
|
|
const formData = await formDataPromise;
|
|
|
|
assert(formData instanceof FormData);
|
2021-07-19 22:11:50 +00:00
|
|
|
assertEquals([...formData], [...input]);
|
2020-09-21 20:07:41 +00:00
|
|
|
});
|
2021-04-21 21:03:04 +00:00
|
|
|
|
2021-11-23 16:45:18 +00:00
|
|
|
Deno.test(function responseInvalidInit() {
|
2021-10-11 21:43:52 +00:00
|
|
|
// deno-lint-ignore ban-ts-comment
|
|
|
|
// @ts-expect-error
|
|
|
|
assertThrows(() => new Response("", 0));
|
|
|
|
assertThrows(() => new Response("", { status: 0 }));
|
|
|
|
// deno-lint-ignore ban-ts-comment
|
|
|
|
// @ts-expect-error
|
|
|
|
assertThrows(() => new Response("", { status: null }));
|
|
|
|
});
|
|
|
|
|
2021-11-23 16:45:18 +00:00
|
|
|
Deno.test(function responseNullInit() {
|
2021-10-11 21:43:52 +00:00
|
|
|
// deno-lint-ignore ban-ts-comment
|
|
|
|
// @ts-expect-error
|
|
|
|
const response = new Response("", null);
|
|
|
|
assertEquals(response.status, 200);
|
|
|
|
});
|
|
|
|
|
2021-11-23 16:45:18 +00:00
|
|
|
Deno.test(function customInspectFunction() {
|
2021-04-21 21:03:04 +00:00
|
|
|
const response = new Response();
|
|
|
|
assertEquals(
|
|
|
|
Deno.inspect(response),
|
|
|
|
`Response {
|
|
|
|
body: null,
|
|
|
|
bodyUsed: false,
|
|
|
|
headers: Headers {},
|
|
|
|
ok: true,
|
|
|
|
redirected: false,
|
|
|
|
status: 200,
|
|
|
|
statusText: "",
|
|
|
|
url: ""
|
|
|
|
}`,
|
|
|
|
);
|
2021-07-08 13:43:36 +00:00
|
|
|
assertStringIncludes(Deno.inspect(Response.prototype), "Response");
|
2021-04-21 21:03:04 +00:00
|
|
|
});
|
2022-09-29 15:38:04 +00:00
|
|
|
|
|
|
|
Deno.test(async function responseBodyUsed() {
|
|
|
|
const response = new Response("body");
|
|
|
|
assert(!response.bodyUsed);
|
|
|
|
await response.text();
|
|
|
|
assert(response.bodyUsed);
|
|
|
|
// .body getter is needed so we can test the faulty code path
|
|
|
|
response.body;
|
|
|
|
assert(response.bodyUsed);
|
|
|
|
});
|