code wiki / (root) / nx_urlcanon_test.nx

nx_urlcanon_test.nx source

↩ module page · 35 lines · 1446 B

1// nx_urlcanon_test.nx -- KAT for URL canonicalization. 2// Native sovereign lane; exit 0 = pass, N = assertion N failed. 3 4import "nx_str.nx" 5import "nx_urlcanon.nx" 6 7func _eq(url: *u8, expect: *u8) -> i64 { 8 let out: *u8 = sys_mmap(512) 9 nx_url_canon(url, nx_str_len(url), out, 512) 10 return nx_str_eq(out, expect) 11} 12 13func main() -> i64 { 14 // host lowercased, path case preserved, fragment dropped 15 if _eq("http://Example.COM/Page#sec", "http://example.com/Page") != 1 { return 1 } 16 // default https port dropped 17 if _eq("https://example.com:443/x", "https://example.com/x") != 1 { return 2 } 18 // default http port dropped 19 if _eq("http://example.com:80/x", "http://example.com/x") != 1 { return 3 } 20 // non-default port preserved 21 if _eq("http://example.com:8080/x", "http://example.com:8080/x") != 1 { return 4 } 22 // query preserved, fragment dropped, host lowered 23 if _eq("https://A.B.com/p?q=1#f", "https://a.b.com/p?q=1") != 1 { return 5 } 24 // scheme lowercased 25 if _eq("HTTP://Example.com/", "http://example.com/") != 1 { return 6 } 26 27 // two cosmetic variants canonicalize EQUAL -> crawler dedups them 28 let a: *u8 = sys_mmap(512) 29 let b: *u8 = sys_mmap(512) 30 nx_url_canon("http://Host.COM:80/p#top", nx_str_len("http://Host.COM:80/p#top"), a, 512) 31 nx_url_canon("http://host.com/p", nx_str_len("http://host.com/p"), b, 512) 32 if nx_str_eq(a, b) != 1 { return 7 } 33 34 return 0 35}