code wiki / (root) / nx_lib_http_test.nx

nx_lib_http_test.nx source

↩ module page · 60 lines · 2309 B

1// nx_lib_http_test.nx -- gate for the sovereign library HTTP router. 2// Proves GET routing + HTTP response building straight from nx_lib_store 3// (no socket needed, no python/sqlite). Exit = failed assertion #. 4import "nx_syscalls.nx" 5import "nx_lib_http.nx" 6 7func lht_has(hay: *u8, hayn: i64, needle: *u8) -> i64 { 8 let nn: i64 = ls_strlen(needle) 9 if nn == 0 { return 1 } 10 if hayn < nn { return 0 } 11 let last: i64 = hayn - nn 12 var i: i64 = 0 13 while i <= last { 14 var j: i64 = 0 15 var st: i64 = 0 16 while st == 0 { 17 if j >= nn { st = 2 } 18 if st == 0 { if hay[i+j] != needle[j] { st = 1 } if st == 0 { j = j + 1 } } 19 } 20 if st == 2 { return 1 } 21 i = i + 1 22 } 23 return 0 24} 25 26func main() -> i64 { 27 let rec: *u8 = "HTTPD Work\t10.7/h\t2026\tcc0" as *u8 28 nx_lib_store_put_work("HTTPDT1" as *u8, rec, ls_strlen(rec)) 29 30 let out: *u8 = sys_mmap(1200000) 31 32 // GET /api/work/<hk> -> 200 + JSON with the title, from the sovereign store 33 let p1: *u8 = "/api/work/HTTPDT1" as *u8 34 let n1: i64 = lhd_response(p1, ls_strlen(p1), out) 35 if n1 <= 0 { return 1 } 36 if lht_has(out, n1, "HTTP/1.1 200 OK" as *u8) != 1 { return 2 } 37 if lht_has(out, n1, "Content-Type: application/json" as *u8) != 1 { return 3 } 38 if lht_has(out, n1, "Content-Length: " as *u8) != 1 { return 4 } 39 if lht_has(out, n1, "\"title\":\"HTTPD Work\"" as *u8) != 1 { return 5 } 40 41 // missing work -> 404 42 let p2: *u8 = "/api/work/NOPE_XX" as *u8 43 let n2: i64 = lhd_response(p2, ls_strlen(p2), out) 44 if lht_has(out, n2, "404 Not Found" as *u8) != 1 { return 6 } 45 46 // GET /api/works -> 200 + JSON array containing the hk 47 let p3: *u8 = "/api/works" as *u8 48 let n3: i64 = lhd_response(p3, ls_strlen(p3), out) 49 if lht_has(out, n3, "HTTP/1.1 200 OK" as *u8) != 1 { return 7 } 50 if lht_has(out, n3, "\"HTTPDT1\"" as *u8) != 1 { return 8 } 51 52 // unknown path -> 404 53 let p4: *u8 = "/nope" as *u8 54 let n4: i64 = lhd_response(p4, ls_strlen(p4), out) 55 if lht_has(out, n4, "404 Not Found" as *u8) != 1 { return 9 } 56 57 let msg: *u8 = "nx_lib_http: 9/9 sovereign HTTP-route assertions PASS (GET /api/work,/api/works from nx_lib_store; no python/sqlite)\n" as *u8 58 sys_write(1, msg, ls_strlen(msg)) 59 return 0 60}