AtlatestRepositorysigil-discourse

sigil-discourse / tree / testtest-query.sgl

1(import (sigil test)
2 (discourse client))
3
4;; ============================================================
5;; url-encode-string
6;; ============================================================
7
8(test-group "url-encode-string"
9 (test "passes through unreserved characters"
10 (assert-equal "hello-world_test.file~v2"
11 (url-encode-string "hello-world_test.file~v2")))
13 (test "encodes spaces as %20"
14 (assert-equal "hello%20world"
15 (url-encode-string "hello world")))
17 (test "encodes @ sign"
18 (assert-equal "user%40example.com"
19 (url-encode-string "[email protected]")))
21 (test "encodes special characters"
22 (assert-equal "a%3Db%26c%3Dd"
23 (url-encode-string "a=b&c=d")))
25 (test "handles empty string"
26 (assert-equal "" (url-encode-string "")))
28 (test "encodes slash"
29 (assert-equal "path%2Fto%2Fthing"
30 (url-encode-string "path/to/thing")))
32 (test "preserves digits and letters"
33 (assert-equal "ABCxyz012"
34 (url-encode-string "ABCxyz012"))))
36;; ============================================================
37;; build-query-string
38;; ============================================================
40(test-group "build-query-string"
41 (test "builds single parameter"
42 (assert-equal "?email=user%40example.com"
43 (build-query-string '((email . "[email protected]")))))
45 (test "builds multiple parameters"
46 (assert-equal "?q=hello%20world&page=2"
47 (build-query-string '((q . "hello world") (page . 2)))))
49 (test "omits #f values"
50 (assert-equal "?q=test"
51 (build-query-string '((q . "test") (email . #f)))))
53 (test "returns empty string when all values are #f"
54 (assert-equal ""
55 (build-query-string '((email . #f) (page . #f)))))
57 (test "returns empty string for empty list"
58 (assert-equal ""
59 (build-query-string '()))))
61;; ============================================================
62;; discourse-url/query
63;; ============================================================
65(test-group "discourse-url/query"
66 (test "builds URL with query parameters"
67 (assert-equal "https://forum.example.com/admin/users/list/active.json?email=user%40example.com"
68 (discourse-url/query "https://forum.example.com"
69 '((email . "[email protected]"))
70 "admin" "users" "list" "active.json")))
72 (test "builds URL without query when all params are #f"
73 (assert-equal "https://forum.example.com/admin/users/list/active.json"
74 (discourse-url/query "https://forum.example.com"
75 '((email . #f))
76 "admin" "users" "list" "active.json")))
78 (test "builds URL with no path segments"
79 (assert-equal "https://forum.example.com?q=test"
80 (discourse-url/query "https://forum.example.com"
81 '((q . "test"))))))