aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/web-util
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2023-10-02 19:26:40 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2023-10-02 19:28:44 +0200
commit712654d4c023a2ab13190c6905d313e0ba897965 (patch)
treeb8505b420d6621022fa6a46271340071d8881322 /tests/unit/web-util
parentMade displayln into a library export. (diff)
downloadcalp-712654d4c023a2ab13190c6905d313e0ba897965.tar.gz
calp-712654d4c023a2ab13190c6905d313e0ba897965.tar.xz
Rewrite test running system.
Diffstat (limited to 'tests/unit/web-util')
-rw-r--r--tests/unit/web-util/server.scm31
-rw-r--r--tests/unit/web-util/web-query.scm37
2 files changed, 68 insertions, 0 deletions
diff --git a/tests/unit/web-util/server.scm b/tests/unit/web-util/server.scm
new file mode 100644
index 00000000..c81abba3
--- /dev/null
+++ b/tests/unit/web-util/server.scm
@@ -0,0 +1,31 @@
+;;; Commentary:
+;; Tests parse-endpoint-string, used for defining server routes.
+;;; Code:
+
+(define-module (test server)
+ :use-module (srfi srfi-64)
+ :use-module (srfi srfi-71)
+ :use-module (srfi srfi-88)
+ :use-module ((web http make-routes)
+ :select (parse-endpoint-string)))
+
+(test-assert "Check that parsing doesn't crash"
+ (parse-endpoint-string "/static/:dir/:file"))
+
+;; Checks that parsing produces correct results
+(test-group
+ "Simple parameters"
+ (let ((path args (parse-endpoint-string "/static/:dir/:file")))
+ (test-equal "Path" "/static/([^/.]+)/([^/.]+)" path)
+ (test-equal "Parameters" '(dir file) args)))
+
+;; Checks that parsing with custom regex works
+;; along with literal periods.
+(test-group
+ "Custom regex for parameters"
+ (let ((path args (parse-endpoint-string "/static/:filename{.*}.:ext")))
+ (test-equal "Path" "/static/(.*)\\.([^/.]+)" path)
+ (test-equal "Parameters" '(filename ext) args)))
+
+
+'((web http make-routes))
diff --git a/tests/unit/web-util/web-query.scm b/tests/unit/web-util/web-query.scm
new file mode 100644
index 00000000..ec20b0c1
--- /dev/null
+++ b/tests/unit/web-util/web-query.scm
@@ -0,0 +1,37 @@
+(define-module (test web-query)
+ :use-module (srfi srfi-64)
+ :use-module (srfi srfi-88)
+ :use-module ((web query) :select (parse-query)))
+
+(test-equal "Empty query gives empty assoc list"
+ '() (parse-query ""))
+(test-equal "Simple key-value query"
+ '(key: "value") (parse-query "key=value"))
+
+;; Slightly cumbersome check, since keys aren't ordered
+(test-group
+ "Simple key-value query, with multiple keys"
+ (let ((kv-list (parse-query "k1=value&k2=1")))
+ (test-equal "value" (and=> (memv k1: kv-list) cadr))
+ (test-equal "1" (and=> (memv k2: kv-list) cadr))))
+
+(test-equal "Values are HTTP-decoded"
+ '(key: " ") (parse-query "key=%20"))
+(test-equal "Keys are HTTP-decoded"
+ '(A: "test") (parse-query "%41=test"))
+
+(test-equal "Query with only key, value becomes key"
+ '(key: "key") (parse-query "key"))
+
+(test-group
+ "Some with only key"
+ (let ((kv-list (parse-query "k1&k2=10")))
+ (test-equal "k1" (and=> (memv k1: kv-list) cadr))
+ (test-equal "10" (and=> (memv k2: kv-list) cadr))))
+
+;; I don't know if HTTP allows this, but my code works like this
+(test-equal "Value with equal in it"
+ '(key: "=") (parse-query "key=="))
+
+
+'((web query))