1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
import { it, describe, expect } from "bun:test";
import fs from "fs";
describe("fetch", () => {
const urls = ["https://example.com", "http://example.com"];
for (let url of urls) {
it(url, async () => {
const response = await fetch(url);
const text = await response.text();
if (
fs.readFileSync(
import.meta.path.substring(0, import.meta.path.lastIndexOf("/")) +
"/fetch.js.txt",
"utf8"
) !== text
) {
throw new Error("Expected fetch.js.txt to match snapshot");
}
});
}
});
function testBlobInterface(blobbyConstructor, hasBlobFn) {
it("json", async () => {
var response = blobbyConstructor(JSON.stringify({ hello: true }));
expect(JSON.stringify(await response.json())).toBe(
JSON.stringify({ hello: true })
);
});
it("text", async () => {
var response = blobbyConstructor(JSON.stringify({ hello: true }));
expect(await response.text()).toBe(JSON.stringify({ hello: true }));
});
it("arrayBuffer", async () => {
var response = blobbyConstructor(JSON.stringify({ hello: true }));
const bytes = new TextEncoder().encode(JSON.stringify({ hello: true }));
const compare = new Uint8Array(await response.arrayBuffer());
for (let i = 0; i < compare.length; i++) {
expect(compare[i]).toBe(bytes[i]);
}
});
hasBlobFn &&
it("blob", async () => {
var response = blobbyConstructor(JSON.stringify({ hello: true }));
const size = JSON.stringify({ hello: true }).length;
const blobed = await response.blob();
expect(blobed instanceof Blob).toBe(true);
expect(blobed.size).toBe(size);
expect(blobed.type).toBe("");
blobed.type = "application/json";
expect(blobed.type).toBe("application/json");
});
}
describe("Blob", () => {
testBlobInterface((data) => new Blob([data]));
var blobConstructorValues = [
["123", "456"],
["123", 456],
["123", "456", "789"],
["123", 456, 789],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[Uint8Array.from([1, 2, 3, 4, 5, 6, 7, 9])],
[Uint8Array.from([1, 2, 3, 4]), "5678", 9],
[new Blob([Uint8Array.from([1, 2, 3, 4])]), "5678", 9],
];
var expected = [
"123456",
"123456",
"123456789",
"123456789",
"123456789",
"\x01\x02\x03\x04\x05\x06\x07\t",
"\x01\x02\x03\x0456789",
"\x01\x02\x03\x0456789",
];
it(`blobConstructorValues`, async () => {
for (let i = 0; i < blobConstructorValues.length; i++) {
var response = new Blob(blobConstructorValues[i]);
const res = await response.text();
if (res !== expected[i]) {
throw new Error(
`Failed: ${expected[i]
.split("")
.map((a) => a.charCodeAt(0))}, received: ${res
.split("")
.map((a) => a.charCodeAt(0))}`
);
}
expect(res).toBe(expected[i]);
}
});
});
describe("Response", () => {
it("clone", async () => {
var body = new Response("<div>hello</div>", {
headers: {
"content-type": "text/html; charset=utf-8",
},
});
var clone = body.clone();
body.headers.set("content-type", "text/plain");
expect(clone.headers.get("content-type")).toBe("text/html; charset=utf-8");
expect(body.headers.get("content-type")).toBe("text/plain");
expect(await clone.text()).toBe("<div>hello</div>");
});
testBlobInterface((data) => new Response(data), true);
});
describe("Request", () => {
it("clone", async () => {
var body = new Request("https://hello.com", {
headers: {
"content-type": "text/html; charset=utf-8",
},
body: "<div>hello</div>",
});
expect(body.headers.get("content-type")).toBe("text/html; charset=utf-8");
var clone = body.clone();
body.headers.set("content-type", "text/plain");
expect(clone.headers.get("content-type")).toBe("text/html; charset=utf-8");
expect(body.headers.get("content-type")).toBe("text/plain");
expect(await clone.text()).toBe("<div>hello</div>");
});
testBlobInterface(
(data) => new Request("https://hello.com", { body: data }),
true
);
});
describe("Headers", () => {
it("writes", async () => {
var body = new Request("https://hello.com", {
headers: {
"content-type": "text/html; charset=utf-8",
},
body: "<div>hello</div>",
});
expect(body.headers.get("content-type")).toBe("text/html; charset=utf-8");
var clone = body.clone();
body.headers.set("content-type", "text/plain");
expect(clone.headers.get("content-type")).toBe("text/html; charset=utf-8");
expect(body.headers.get("content-type")).toBe("text/plain");
expect(await clone.text()).toBe("<div>hello</div>");
});
});
|