diff --git a/README.md b/README.md index 12090b7e..841bc221 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ Creating a router: `:mixed-router` is created (both static & wild routes are found): ```clj -(reitit/router-type router) +(reitit/router-name router) ; :mixed-router ``` diff --git a/src/reitit/core.cljc b/src/reitit/core.cljc index bd1aa9da..beab043e 100644 --- a/src/reitit/core.cljc +++ b/src/reitit/core.cljc @@ -93,7 +93,7 @@ [p m (if compile (compile route opts))]) (defprotocol Router - (router-type [this]) + (router-name [this]) (routes [this]) (options [this]) (route-names [this]) @@ -146,7 +146,7 @@ lookup (impl/fast-map lookup)] (reify Router - (router-type [_] + (router-name [_] :linear-router) (routes [_] routes) @@ -190,7 +190,7 @@ data (impl/fast-map data) lookup (impl/fast-map lookup)] (reify Router - (router-type [_] + (router-name [_] :lookup-router) (routes [_] routes) @@ -220,7 +220,7 @@ lookup-router (lookup-router lookup opts) names (find-names routes opts)] (reify Router - (router-type [_] + (router-name [_] :mixed-router) (routes [_] routes) diff --git a/test/cljc/reitit/core_test.cljc b/test/cljc/reitit/core_test.cljc index 5dfe38f2..15c75b64 100644 --- a/test/cljc/reitit/core_test.cljc +++ b/test/cljc/reitit/core_test.cljc @@ -9,8 +9,8 @@ (testing "linear-router" (let [router (reitit/router ["/api" ["/ipa" ["/:size" ::beer]]])] - (is (= :linear-router (reitit/router-type router))) (is (= [["/api/ipa/:size" {:name ::beer}]] + (is (= :linear-router (reitit/router-name router))) (reitit/routes router))) (is (= true (map? (reitit/options router)))) (is (= (reitit/map->Match @@ -27,6 +27,7 @@ (reitit/match-by-name router ::beer {:size "large"}))) (is (= nil (reitit/match-by-name router "ILLEGAL"))) (is (= [::beer] (reitit/route-names router))) + (testing "name-based routing with missing parameters" (is (= (reitit/map->PartialMatch {:template "/api/ipa/:size" @@ -42,8 +43,8 @@ (testing "lookup-router" (let [router (reitit/router ["/api" ["/ipa" ["/large" ::beer]]])] - (is (= :lookup-router (reitit/router-type router))) (is (= [["/api/ipa/large" {:name ::beer}]] + (is (= :lookup-router (reitit/router-name router))) (reitit/routes router))) (is (= true (map? (reitit/options router)))) (is (= (reitit/map->Match @@ -60,6 +61,7 @@ (reitit/match-by-name router ::beer {:size "large"}))) (is (= nil (reitit/match-by-name router "ILLEGAL"))) (is (= [::beer] (reitit/route-names router))) + (testing "can't be created with wildcard routes" (is (thrown-with-msg? ExceptionInfo @@ -69,6 +71,7 @@ ["/api/:version/ping"] {}))))))) (testing "route coercion & compilation" + (testing "custom compile" (let [compile-times (atom 0) coerce (fn [[path meta] _] @@ -86,6 +89,7 @@ ["/crap"]]] {:coerce coerce :compile compile})] + (testing "routes are coerced" (is (= [["/api/ping" {:name ::ping :path "/api/ping", @@ -100,6 +104,7 @@ (is result) (is (= "/api/pong" (result))) (is (= 2 @compile-times)))))) + (testing "default compile" (let [router (reitit/router ["/ping" (constantly "ok")])] (let [{:keys [result]} (reitit/match-by-path router "/ping")] @@ -109,9 +114,9 @@ (testing "custom router" (let [router (reitit/router ["/ping"] {:router (fn [_ _] (reify Router - (reitit/router-type [_] + (reitit/router-name [_] ::custom)))})] - (is (= ::custom (reitit/router-type router))))) + (is (= ::custom (reitit/router-name router))))) (testing "bide sample" (let [routes [["/auth/login" :auth/login]