-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtest.ts
More file actions
131 lines (106 loc) · 3.98 KB
/
test.ts
File metadata and controls
131 lines (106 loc) · 3.98 KB
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
import * as Kv from "@spinframework/spin-kv";
import { pushStreamChunks, isEqualBytes, readStreamChunks } from "./helpers";
const decoder = new TextDecoder()
const streamingChunks = ["chunk1", "chunk2", "chunk3", "chunk4", "chunk5"]
function health(req: Request) {
return new Response("Healthy", { status: 200 })
}
function stream(req: Request) {
const { readable, writable } = new TransformStream();
pushStreamChunks(writable, streamingChunks)
// Return the stream as a Response
return new Response(readable, {
headers: { 'Content-Type': 'text/plain' },
})
}
function statusTest(req: Request) {
return new Response(null, { status: 201 })
}
function headersTest(req: Request) {
return new Response(null, { headers: { "content-type": "text/html" } })
}
async function outboundHttp(req: Request) {
let requestUrl = "http://localhost:3000/health"
let response = await fetch(requestUrl)
if (response.status == 200) {
if (await response.text() == "Healthy") {
return new Response("success", { status: 200 })
}
}
return new Response("failed", { status: 500 })
}
function kvTest(req: Request) {
let store = Kv.openDefault()
store.set("test", "try")
if (decoder.decode(store.get("test") || new Uint8Array()) == "try") {
return new Response("success", { status: 200 })
}
return new Response("failed", { status: 500 })
}
function kvTestArrayBuffer(req: Request) {
let store = Kv.openDefault()
let arr = new Uint8Array([1, 2, 3])
store.set("arr", arr.buffer)
let ret = store.get("arr")
if (ret == null || !isEqualBytes(new Uint8Array(ret), arr)) {
return new Response("failed", { status: 500 })
}
return new Response("success", { status: 200 })
}
function kvTestUint8Array(req: Request) {
let store = Kv.openDefault()
let arr = new Uint8Array([1, 2, 3])
store.set("arr", arr)
let ret = store.get("arr")
if (ret == null || !isEqualBytes(ret, arr)) {
return new Response("failed", { status: 500 })
}
return new Response("success", { status: 200 })
}
async function streamTest(req: Request) {
let response = await fetch("http://localhost:3000/stream")
if (response.body == null) {
return new Response("response has no body", { status: 500 })
}
let chunks = await readStreamChunks(response.body)
if (chunks.length != streamingChunks.length) {
return new Response("chunks length mismatch", { status: 500 })
}
for (let i = 0; i < chunks.length; i++) {
if (chunks[i] != streamingChunks[i]) {
return new Response("chunks mismatch", { status: 500 })
}
}
return new Response("success", { status: 200 })
}
async function testFunctionality(req: Request) {
const testCases = [
{ name: "statusTest", validate: (resp: Response) => resp.status === 201 },
{ name: "headersTest", validate: (resp: Response) => resp.status === 200 && resp.headers.get("Content-Type") === "text/html" },
{ name: "outboundHttp", validate: (resp: Response) => resp.status === 200 },
{ name: "kvTest", validate: (resp: Response) => resp.status === 200 },
{ name: "kvTestArrayBuffer", validate: (resp: Response) => resp.status === 200 },
{ name: "kvTestUint8Array", validate: (resp: Response) => resp.status === 200 },
{ name: "streamTest", validate: (resp: Response) => resp.status === 200 },
];
const results: { [key: string]: boolean } = {};
for (const test of testCases) {
const resp = await fetch(`http://localhost:3000/${test.name}`);
results[test.name] = test.validate(resp);
}
const allPassed = Object.values(results).every(Boolean);
let status = allPassed ? 200 : 500;
return new Response(JSON.stringify(results, null, 2), { status });
}
export {
health,
stream,
testFunctionality,
headersTest,
statusTest,
outboundHttp,
kvTest,
kvTestArrayBuffer,
kvTestUint8Array,
streamTest
}