Skip to content
File

Blob: src/workerd/api/tests/blob2-test.js

javascript49 lines
1// Copyright (c) 2023 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5import { ok, strictEqual, deepStrictEqual, throws } from 'node:assert';
6 
7export const test = {
8 async test() {
9 const res = new Response('test', {
10 headers: {
11 'content-type': 'text/plain, x/y, text/html',
12 },
13 });
14 
15 const blob = await res.blob();
16 
17 // Without the blob_standard_mime_type compat flag, the blob.type would be
18 // the incorrect 'text/plain, x/y, text/html'
19 
20 strictEqual(await blob.text(), 'test');
21 strictEqual(blob.type, 'text/html');
22 },
23};
24 
25export const bytes = {
26 async test() {
27 const check = new Uint8Array([116, 101, 115, 116]);
28 const blob = new Blob(['test']);
29 const u8 = await blob.bytes();
30 deepStrictEqual(u8, check);
31 ok(u8 instanceof Uint8Array);
32 
33 const res = new Response('test');
34 const u8_2 = await res.bytes();
35 deepStrictEqual(u8_2, check);
36 ok(u8_2 instanceof Uint8Array);
37 },
38};
39 
40export const noResizable = {
41 test() {
42 const ab = new ArrayBuffer(8, { maxByteLength: 16 });
43 throws(() => new Blob([ab]), {
44 name: 'TypeError',
45 message: 'Cannot create a Blob with a resizable ArrayBuffer',
46 });
47 },
48};