code wiki / (root) / nx_http_health_test.nx

nx_http_health_test.nx source

↩ module page · 114 lines · 5056 B

1// nx_http_health_test.nx -- smoke for /health emitter. 2// 3// expect_exit: 0 4// 5// license_tier: ORIGINAL 6 7import "nx_syscalls_x86_64.nx" 8import "nx_http_health_emit.nx" // RESTORED emitter. nx_http_health.nx is now a DIFFERENT organ -- a 9 // URL prober with its own main() -- after a 2026-07-21 dedupe treated a 10 // name COLLISION as a COPY and kept the wrong file. 11 12func bytes_eq(a: *u8, b: *u8, n: i64) -> i64 { 13 var i: i64 = 0 14 while i < n { 15 if a[i] != b[i] { return 0 } 16 i = i + 1 17 } 18 return 1 19} 20 21func bytes_contains(haystack: *u8, h_n: i64, needle: *u8, n_n: i64) -> i64 { 22 if n_n > h_n { return 0 } 23 var i: i64 = 0 24 while i <= h_n - n_n { 25 if bytes_eq(((haystack as i64) + i) as *u8, needle, n_n) == 1 { return 1 } 26 i = i + 1 27 } 28 return 0 29} 30 31func main() -> i64 { 32 // ---- Verdict + status enum gates ---- 33 if nxhl_verdict_is_valid(NXHL_OK) != 1 { return 1 } 34 if nxhl_verdict_is_valid(NXHL_VERDICT_N) != 0 { return 2 } 35 if nxhl_status_is_valid(NXHL_STATUS_PASS) != 1 { return 3 } 36 if nxhl_status_is_valid(NXHL_STATUS_FAIL) != 1 { return 4 } 37 if nxhl_status_is_valid(NXHL_STATUS_N) != 0 { return 5 } 38 39 if bytes_eq(nxhl_status_name(NXHL_STATUS_PASS), "pass" as *u8, 4) != 1 { return 6 } 40 if bytes_eq(nxhl_status_name(NXHL_STATUS_WARN), "warn" as *u8, 4) != 1 { return 7 } 41 if bytes_eq(nxhl_status_name(NXHL_STATUS_FAIL), "fail" as *u8, 4) != 1 { return 8 } 42 43 if nxhl_status_http_code(NXHL_STATUS_PASS) != 200 { return 10 } 44 if nxhl_status_http_code(NXHL_STATUS_WARN) != 200 { return 11 } 45 if nxhl_status_http_code(NXHL_STATUS_FAIL) != 503 { return 12 } 46 47 // ---- Emit a healthy response ---- 48 let buf: *u8 = sys_mmap(1024) 49 let nn: *i64 = sys_mmap(8) as *i64 50 let scratch: *u8 = sys_mmap(64) 51 let rc1: i64 = nx_http_emit_health(buf, 1024, nn, 52 NXHL_STATUS_PASS, 12345, 3, scratch) 53 if rc1 != NXHL_OK { return 20 } 54 if nn[0] <= 0 { return 21 } 55 56 // Verify headers contain "HTTP/1.1 200 OK" 57 if bytes_contains(buf, nn[0], "HTTP/1.1 200 OK" as *u8, 15) != 1 { return 22 } 58 // Verify Content-Type 59 if bytes_contains(buf, nn[0], 60 "application/health+json" as *u8, 23) != 1 { return 23 } 61 // Verify body contains the status field 62 if bytes_contains(buf, nn[0], "\"status\":\"pass\"" as *u8, 15) != 1 { return 24 } 63 // Verify uptime 64 if bytes_contains(buf, nn[0], "\"uptime_ms\":12345" as *u8, 17) != 1 { return 25 } 65 // Verify checks count 66 if bytes_contains(buf, nn[0], "\"checks\":3" as *u8, 10) != 1 { return 26 } 67 68 // ---- Emit a failing response (503) ---- 69 let buf2: *u8 = sys_mmap(1024) 70 let nn2: *i64 = sys_mmap(8) as *i64 71 let scratch2: *u8 = sys_mmap(64) 72 let rc2: i64 = nx_http_emit_health(buf2, 1024, nn2, 73 NXHL_STATUS_FAIL, 60000, 1, scratch2) 74 if rc2 != NXHL_OK { return 30 } 75 if bytes_contains(buf2, nn2[0], 76 "HTTP/1.1 503 Service Unavailable" as *u8, 32) != 1 { return 31 } 77 if bytes_contains(buf2, nn2[0], "\"status\":\"fail\"" as *u8, 15) != 1 { return 32 } 78 if bytes_contains(buf2, nn2[0], "\"uptime_ms\":60000" as *u8, 17) != 1 { return 33 } 79 80 // ---- Emit a warn response (still 200) ---- 81 let buf3: *u8 = sys_mmap(1024) 82 let nn3: *i64 = sys_mmap(8) as *i64 83 let scratch3: *u8 = sys_mmap(64) 84 let rc3: i64 = nx_http_emit_health(buf3, 1024, nn3, 85 NXHL_STATUS_WARN, 999, 5, scratch3) 86 if rc3 != NXHL_OK { return 40 } 87 if bytes_contains(buf3, nn3[0], 88 "HTTP/1.1 200 OK" as *u8, 15) != 1 { return 41 } 89 if bytes_contains(buf3, nn3[0], "\"status\":\"warn\"" as *u8, 15) != 1 { return 42 } 90 91 // ---- /ready alias ---- 92 let buf4: *u8 = sys_mmap(1024) 93 let nn4: *i64 = sys_mmap(8) as *i64 94 let scratch4: *u8 = sys_mmap(64) 95 let rc4: i64 = nx_http_emit_ready(buf4, 1024, nn4, 96 NXHL_STATUS_PASS, 1, 1, scratch4) 97 if rc4 != NXHL_OK { return 50 } 98 if bytes_contains(buf4, nn4[0], "\"status\":\"pass\"" as *u8, 15) != 1 { return 51 } 99 100 // ---- BAD_ARG paths ---- 101 if nx_http_emit_health(0 as *u8, 1024, nn, NXHL_STATUS_PASS, 0, 0, scratch) != NXHL_BAD_ARG { return 60 } 102 if nx_http_emit_health(buf, 1024, 0 as *i64, NXHL_STATUS_PASS, 0, 0, scratch) != NXHL_BAD_ARG { return 61 } 103 if nx_http_emit_health(buf, 1024, nn, NXHL_STATUS_PASS, 0, 0, 0 as *u8) != NXHL_BAD_ARG { return 62 } 104 if nx_http_emit_health(buf, 0, nn, NXHL_STATUS_PASS, 0, 0, scratch) != NXHL_BAD_ARG { return 63 } 105 if nx_http_emit_health(buf, 1024, nn, 99, 0, 0, scratch) != NXHL_BAD_ARG { return 64 } 106 107 // ---- OOM_BUFFER ---- 108 let tiny: *u8 = sys_mmap(64) 109 let t_n: *i64 = sys_mmap(8) as *i64 110 let t_s: *u8 = sys_mmap(64) 111 if nx_http_emit_health(tiny, 64, t_n, NXHL_STATUS_PASS, 0, 0, t_s) != NXHL_OOM_BUFFER { return 70 } 112 113 return 0 114}