forked from wasmerio/winterjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrangler.js
60 lines (53 loc) · 1.62 KB
/
wrangler.js
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
// THIS FAILS: Investigate
function assert(condition, message) {
if (!condition) {
throw new Error(message || "Assertion failed");
}
}
function assertEquals(actual, expected, message) {
assert(
actual === expected,
message || `Expected ${expected} but got ${actual}`
);
}
async function handleRequest(request) {
try {
const string = "Hello, world!";
const base64Encoded = "SGVsbG8sIHdvcmxkIQ==";
// Test btoa
const encoded = btoa(string);
assertEquals(
encoded,
base64Encoded,
"btoa did not encode the string correctly"
);
// Test atob
const decoded = atob(base64Encoded);
assertEquals(decoded, string, "atob did not decode the string correctly");
// Test btoa with binary data
try {
const binaryData = "\x00\x01\x02";
btoa(binaryData);
assert(true, "btoa handled binary data without throwing error");
} catch (e) {
assert(false, "btoa should not throw error with binary data");
}
// Test atob with invalid input
try {
atob("Invalid base64 string");
assert(false, "atob should throw error with invalid base64 input");
} catch (e) {
assert(true, "atob threw error as expected with invalid base64 input");
}
// Create a response with the Blob's text
return new Response("All Tests Passed!", {
headers: { "content-type": "text/plain" },
});
} catch (error) {
// If there's an error, return the error message in the response
return new Response(error.message, { status: 500 });
}
}
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request));
});