summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsko Nõmm <asko@bien.ee>2021-10-02 17:51:55 -0300
committerAsko Nõmm <asko@bien.ee>2021-10-02 17:51:55 -0300
commit05ae5bfe03b41cc46f844219e5f177d68fce3d41 (patch)
tree9a7a5e2e09ec7e9c0afa881a376918c1cf85814b
parent7b927c06fd1bb777057eb8e708354e5633c8a828 (diff)
Add tests
-rw-r--r--project.clj1
-rw-r--r--test/ruuter/core_test.clj61
2 files changed, 61 insertions, 1 deletions
diff --git a/project.clj b/project.clj
index e2afac1..d1bb414 100644
--- a/project.clj
+++ b/project.clj
@@ -5,6 +5,7 @@
:url "https://raw.githubusercontent.com/askonomm/ruuter/master/LICENSE.txt"}
:dependencies [[org.clojure/clojure "1.10.3"]
[http-kit "2.5.3"]]
+ :plugins [[lein-cloverage "1.2.2"]]
:main ruuter.core
:min-lein-version "2.0.0"
:aot [ruuter.core]
diff --git a/test/ruuter/core_test.clj b/test/ruuter/core_test.clj
index f98d41c..c735f1a 100644
--- a/test/ruuter/core_test.clj
+++ b/test/ruuter/core_test.clj
@@ -1,3 +1,62 @@
(ns ruuter.core-test
(:require [clojure.test :refer :all]
- [ruuter.core :refer :all])) \ No newline at end of file
+ [ruuter.core :as ruuter]))
+
+(deftest path->regex-path-test
+ (let [testfn #'ruuter/path->regex-path]
+ (testing "Converting a path to a regex path with no params"
+ (is (= "/hello/world" (testfn "/hello/world"))))
+ (testing "Converting a path to a regex path with params"
+ (is (= "/hello/.*" (testfn "/hello/:who")))
+ (is (= "/.*/.*/.*" (testfn "/:these/:are/:params"))))))
+
+
+(deftest path+uri->path-params-test
+ (let [testfn #'ruuter/path+uri->path-params]
+ (testing "No params returns an empty map"
+ (is (= {} (testfn "/hello/world" "/hello/world"))))
+ (testing "Having a param returns a map accordingly"
+ (is (= {:who "world"} (testfn "/hello/:who" "/hello/world"))))
+ (testing "Multiple params returns a map accordingly"
+ (is (= {:who "world"
+ :why "because"} (testfn "/hello/:who/:why" "/hello/world/because"))))))
+
+
+(deftest match-route-test
+ (let [testfn #'ruuter/match-route]
+ (testing "Find a route that exists"
+ (is (= {:path "/hello"
+ :regex-path "/hello"
+ :method :get
+ :response {:status 200
+ :body "Hello."}}
+ (testfn [{:path "/hello"
+ :method :get
+ :response {:status 200
+ :body "Hello."}}] "/hello" :get))))
+ (testing "No route found"
+ (is (= nil
+ (testfn [] "/hello" :get))))))
+
+
+(deftest route+req->response-test
+ (let [testfn #'ruuter/route+req->response]
+ (testing "Returning a map when the response is a direct map"
+ (= {:status 200
+ :body "Hello."}
+ (testfn {:path "/hello"
+ :response {:status 200
+ :body "Hello."}}
+ {:uri "/hello"})))
+ (testing "Returning a map via a fn when the response is a fn"
+ (= {:status 200
+ :body "Hello, world."}
+ (testfn {:path "/hello/:who"
+ :response (fn [req]
+ {:status 200
+ :body (str "Hello, " (:who (:params req)))})}
+ {:uri "/hello/world"})))
+ (testing "Returning an error map when route is invalid"
+ (= {:status 404
+ :body "Not found."}
+ (testfn nil {:uri "/hello"})))))