2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2021-07-08 09:43:36 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
|
|
|
assertStringIncludes,
|
|
|
|
unitTest,
|
|
|
|
} from "./test_util.ts";
|
2020-09-21 16:07:41 -04:00
|
|
|
|
|
|
|
unitTest(async function responseText() {
|
|
|
|
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");
|
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(async function responseArrayBuffer() {
|
|
|
|
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]));
|
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(async function responseJson() {
|
|
|
|
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" });
|
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(async function responseBlob() {
|
|
|
|
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 09:34:37 -04:00
|
|
|
assertEquals(blob.size, 3);
|
|
|
|
assertEquals(await blob.arrayBuffer(), new Uint8Array([1, 2, 3]).buffer);
|
2020-09-21 16:07:41 -04:00
|
|
|
});
|
|
|
|
|
2021-04-05 09:57:52 -04:00
|
|
|
// TODO(lucacasonato): re-enable test once #10002 is fixed.
|
|
|
|
unitTest({ ignore: true }, async function responseFormData() {
|
2020-09-21 16:07:41 -04:00
|
|
|
const input = new FormData();
|
|
|
|
input.append("hello", "world");
|
|
|
|
const response = new Response(input, {
|
|
|
|
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
|
|
});
|
|
|
|
const formDataPromise = response.formData();
|
|
|
|
assert(formDataPromise instanceof Promise);
|
|
|
|
const formData = await formDataPromise;
|
|
|
|
assert(formData instanceof FormData);
|
|
|
|
assertEquals(formData, input);
|
|
|
|
});
|
2021-04-21 17:03:04 -04:00
|
|
|
|
|
|
|
unitTest(function customInspectFunction(): void {
|
|
|
|
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 09:43:36 -04:00
|
|
|
assertStringIncludes(Deno.inspect(Response.prototype), "Response");
|
2021-04-21 17:03:04 -04:00
|
|
|
});
|