1+ import * as Kv from "@spinframework/spin-kv" ;
2+ import { pushStreamChunks , isEqualBytes , readStreamChunks } from "./helpers" ;
3+
4+ const decoder = new TextDecoder ( )
5+ const streamingChunks = [ "chunk1" , "chunk2" , "chunk3" , "chunk4" , "chunk5" ]
6+
7+ function health ( req : Request ) {
8+ return new Response ( "Healthy" , { status : 200 } )
9+ }
10+
11+ function stream ( req : Request ) {
12+ const { readable, writable } = new TransformStream ( ) ;
13+ pushStreamChunks ( writable , streamingChunks )
14+
15+ // Return the stream as a Response
16+ return new Response ( readable , {
17+ headers : { 'Content-Type' : 'text/plain' } ,
18+ } )
19+ }
20+
21+ function statusTest ( req : Request ) {
22+ return new Response ( null , { status : 201 } )
23+ }
24+
25+ function headersTest ( req : Request ) {
26+ return new Response ( null , { headers : { "content-type" : "text/html" } } )
27+ }
28+
29+ async function outboundHttp ( req : Request ) {
30+ let requestUrl = "http://localhost:3000/health"
31+ let response = await fetch ( requestUrl )
32+ if ( response . status == 200 ) {
33+ if ( await response . text ( ) == "Healthy" ) {
34+ return new Response ( "success" , { status : 200 } )
35+ }
36+ }
37+ return new Response ( "failed" , { status : 500 } )
38+ }
39+
40+ function kvTest ( req : Request ) {
41+ let store = Kv . openDefault ( )
42+ store . set ( "test" , "try" )
43+ if ( decoder . decode ( store . get ( "test" ) || new Uint8Array ( ) ) == "try" ) {
44+ return new Response ( "success" , { status : 200 } )
45+ }
46+ return new Response ( "failed" , { status : 500 } )
47+ }
48+
49+ function kvTestUint8Array ( req : Request ) {
50+ let store = Kv . openDefault ( )
51+
52+ let arr = new Uint8Array ( [ 1 , 2 , 3 ] )
53+ store . set ( "arr" , arr )
54+ let ret = store . get ( "arr" )
55+
56+ if ( ret == null || ! isEqualBytes ( ret , arr ) ) {
57+ return new Response ( "failed" , { status : 500 } )
58+ }
59+ return new Response ( "success" , { status : 200 } )
60+ }
61+
62+ async function streamTest ( req : Request ) {
63+ let response = await fetch ( "http://localhost:3000/stream" )
64+
65+ if ( response . body == null ) {
66+ return new Response ( "response has no body" , { status : 500 } )
67+ }
68+
69+ let chunks = await readStreamChunks ( response . body )
70+
71+ if ( chunks . length != streamingChunks . length ) {
72+ return new Response ( "chunks length mismatch" , { status : 500 } )
73+ }
74+
75+ for ( let i = 0 ; i < chunks . length ; i ++ ) {
76+ if ( chunks [ i ] != streamingChunks [ i ] ) {
77+ return new Response ( "chunks mismatch" , { status : 500 } )
78+ }
79+ }
80+
81+ return new Response ( "success" , { status : 200 } )
82+ }
83+
84+ async function testFunctionality ( req : Request ) {
85+ const testCases = [
86+ { name : "statusTest" , validate : ( resp : Response ) => resp . status === 201 } ,
87+ { name : "headersTest" , validate : ( resp : Response ) => resp . status === 200 && resp . headers . get ( "Content-Type" ) === "text/html" } ,
88+ { name : "outboundHttp" , validate : ( resp : Response ) => resp . status === 200 } ,
89+ { name : "kvTest" , validate : ( resp : Response ) => resp . status === 200 } ,
90+ { name : "kvTestUint8Array" , validate : ( resp : Response ) => resp . status === 200 } ,
91+ { name : "streamTest" , validate : ( resp : Response ) => resp . status === 200 } ,
92+ ] ;
93+
94+ const results : { [ key : string ] : boolean } = { } ;
95+
96+ for ( const test of testCases ) {
97+ const resp = await fetch ( `http://localhost:3000/${ test . name } ` ) ;
98+ results [ test . name ] = test . validate ( resp ) ;
99+ }
100+
101+ const allPassed = Object . values ( results ) . every ( Boolean ) ;
102+ let status = allPassed ? 200 : 500 ;
103+ return new Response ( JSON . stringify ( results , null , 2 ) , { status } ) ;
104+ }
105+
106+
107+ export {
108+ health ,
109+ stream ,
110+ testFunctionality ,
111+ headersTest ,
112+ statusTest ,
113+ outboundHttp ,
114+ kvTest ,
115+ kvTestUint8Array ,
116+ streamTest
117+ }
0 commit comments