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";
|
2021-02-02 06:05:46 -05:00
|
|
|
import { concat } from "../../../test_util/std/bytes/mod.ts";
|
2018-09-14 08:45:50 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function blobString(): void {
|
2018-09-14 08:45:50 -04:00
|
|
|
const b1 = new Blob(["Hello World"]);
|
|
|
|
const str = "Test";
|
|
|
|
const b2 = new Blob([b1, str]);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(b2.size, b1.size + str.length);
|
2018-09-14 08:45:50 -04:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function blobBuffer(): void {
|
2018-09-14 08:45:50 -04:00
|
|
|
const buffer = new ArrayBuffer(12);
|
|
|
|
const u8 = new Uint8Array(buffer);
|
|
|
|
const f1 = new Float32Array(buffer);
|
|
|
|
const b1 = new Blob([buffer, u8]);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(b1.size, 2 * u8.length);
|
2018-09-14 08:45:50 -04:00
|
|
|
const b2 = new Blob([b1, f1]);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(b2.size, 3 * u8.length);
|
2018-09-14 08:45:50 -04:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function blobSlice(): void {
|
2018-09-14 08:45:50 -04:00
|
|
|
const blob = new Blob(["Deno", "Foo"]);
|
|
|
|
const b1 = blob.slice(0, 3, "Text/HTML");
|
|
|
|
assert(b1 instanceof Blob);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(b1.size, 3);
|
|
|
|
assertEquals(b1.type, "text/html");
|
2018-09-14 08:45:50 -04:00
|
|
|
const b2 = blob.slice(-1, 3);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(b2.size, 0);
|
2018-09-14 08:45:50 -04:00
|
|
|
const b3 = blob.slice(100, 3);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(b3.size, 0);
|
2018-09-14 08:45:50 -04:00
|
|
|
const b4 = blob.slice(0, 10);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(b4.size, blob.size);
|
2018-09-14 08:45:50 -04:00
|
|
|
});
|
|
|
|
|
2020-03-31 14:42:18 -04:00
|
|
|
unitTest(function blobInvalidType(): void {
|
|
|
|
const blob = new Blob(["foo"], {
|
|
|
|
type: "\u0521",
|
|
|
|
});
|
|
|
|
|
|
|
|
assertEquals(blob.type, "");
|
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function blobShouldNotThrowError(): void {
|
2019-04-18 21:56:33 -04:00
|
|
|
let hasThrown = false;
|
|
|
|
|
|
|
|
try {
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2020-08-24 19:43:54 -04:00
|
|
|
const options1: any = {
|
2019-04-18 21:56:33 -04:00
|
|
|
ending: "utf8",
|
2020-03-28 13:03:49 -04:00
|
|
|
hasOwnProperty: "hasOwnProperty",
|
2019-04-18 21:56:33 -04:00
|
|
|
};
|
2020-08-24 19:43:54 -04:00
|
|
|
const options2 = Object.create(null);
|
2019-04-18 21:56:33 -04:00
|
|
|
new Blob(["Hello World"], options1);
|
|
|
|
new Blob(["Hello World"], options2);
|
|
|
|
} catch {
|
|
|
|
hasThrown = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(hasThrown, false);
|
|
|
|
});
|
|
|
|
|
2020-09-18 09:20:55 -04:00
|
|
|
/* TODO https://github.com/denoland/deno/issues/7540
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function nativeEndLine(): void {
|
2020-08-24 19:43:54 -04:00
|
|
|
const options = {
|
2020-03-28 13:03:49 -04:00
|
|
|
ending: "native",
|
2020-08-24 19:43:54 -04:00
|
|
|
} as const;
|
2019-09-07 12:27:18 -04:00
|
|
|
const blob = new Blob(["Hello\nWorld"], options);
|
2019-08-01 10:04:39 -04:00
|
|
|
|
2020-04-28 12:35:23 -04:00
|
|
|
assertEquals(blob.size, Deno.build.os === "windows" ? 12 : 11);
|
2019-08-01 10:04:39 -04:00
|
|
|
});
|
2020-09-18 09:20:55 -04:00
|
|
|
*/
|
2019-08-01 10:04:39 -04:00
|
|
|
|
2020-04-03 14:55:23 -04:00
|
|
|
unitTest(async function blobText(): Promise<void> {
|
|
|
|
const blob = new Blob(["Hello World"]);
|
|
|
|
assertEquals(await blob.text(), "Hello World");
|
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(async function blobStream(): Promise<void> {
|
|
|
|
const blob = new Blob(["Hello World"]);
|
|
|
|
const stream = blob.stream();
|
|
|
|
assert(stream instanceof ReadableStream);
|
|
|
|
const reader = stream.getReader();
|
|
|
|
let bytes = new Uint8Array();
|
|
|
|
const read = async (): Promise<void> => {
|
|
|
|
const { done, value } = await reader.read();
|
|
|
|
if (!done && value) {
|
|
|
|
bytes = concat(bytes, value);
|
|
|
|
return read();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
await read();
|
2021-04-05 09:57:52 -04:00
|
|
|
const decoder = new TextDecoder();
|
|
|
|
assertEquals(decoder.decode(bytes), "Hello World");
|
2020-04-03 14:55:23 -04:00
|
|
|
});
|
2020-05-29 02:27:57 -04:00
|
|
|
|
2020-08-17 17:46:08 -04:00
|
|
|
unitTest(async function blobArrayBuffer(): Promise<void> {
|
|
|
|
const uint = new Uint8Array([102, 111, 111]);
|
|
|
|
const blob = new Blob([uint]);
|
|
|
|
assertEquals(await blob.arrayBuffer(), uint.buffer);
|
|
|
|
});
|
|
|
|
|
2020-05-29 02:27:57 -04:00
|
|
|
unitTest(function blobConstructorNameIsBlob(): void {
|
|
|
|
const blob = new Blob();
|
|
|
|
assertEquals(blob.constructor.name, "Blob");
|
|
|
|
});
|
2021-07-08 09:43:36 -04:00
|
|
|
|
|
|
|
unitTest(function blobCustomInspectFunction(): void {
|
|
|
|
const blob = new Blob();
|
|
|
|
assertEquals(
|
|
|
|
Deno.inspect(blob),
|
|
|
|
`Blob { size: 0, type: "" }`,
|
|
|
|
);
|
|
|
|
assertStringIncludes(Deno.inspect(Blob.prototype), "Blob");
|
|
|
|
});
|