mirror of
https://github.com/metosin/reitit.git
synced 2025-12-18 08:51:12 +00:00
Test that different routers work correctly
This commit is contained in:
parent
1f27021c29
commit
8bde6aefa2
1 changed files with 65 additions and 125 deletions
|
|
@ -7,140 +7,80 @@
|
||||||
|
|
||||||
(deftest reitit-test
|
(deftest reitit-test
|
||||||
|
|
||||||
(testing "prefix-tree-router"
|
(testing "routers handling wildcard paths"
|
||||||
(let [router (r/router ["/api" ["/ipa" ["/:size" ::beer]]])]
|
(are [r name]
|
||||||
(is (= :prefix-tree-router (r/router-name router)))
|
(let [router (r/router ["/api" ["/ipa" ["/:size" ::beer]]] {:router r})]
|
||||||
(is (= [["/api/ipa/:size" {:name ::beer} nil]]
|
(is (= name (r/router-name router)))
|
||||||
(r/routes router)))
|
(is (= [["/api/ipa/:size" {:name ::beer} nil]]
|
||||||
(is (= true (map? (r/options router))))
|
(r/routes router)))
|
||||||
(is (= (r/map->Match
|
(is (= true (map? (r/options router))))
|
||||||
{:template "/api/ipa/:size"
|
(is (= (r/map->Match
|
||||||
:meta {:name ::beer}
|
|
||||||
:path "/api/ipa/large"
|
|
||||||
:params {:size "large"}})
|
|
||||||
(r/match-by-path router "/api/ipa/large")))
|
|
||||||
(is (= (r/map->Match
|
|
||||||
{:template "/api/ipa/:size"
|
|
||||||
:meta {:name ::beer}
|
|
||||||
:path "/api/ipa/large"
|
|
||||||
:params {:size "large"}})
|
|
||||||
(r/match-by-name router ::beer {:size "large"})))
|
|
||||||
(is (= nil (r/match-by-name router "ILLEGAL")))
|
|
||||||
(is (= [::beer] (r/route-names router)))
|
|
||||||
|
|
||||||
(testing "name-based routing with missing parameters"
|
|
||||||
(is (= (r/map->PartialMatch
|
|
||||||
{:template "/api/ipa/:size"
|
{:template "/api/ipa/:size"
|
||||||
:meta {:name ::beer}
|
:meta {:name ::beer}
|
||||||
:required #{:size}
|
:path "/api/ipa/large"
|
||||||
:params nil})
|
:params {:size "large"}})
|
||||||
(r/match-by-name router ::beer)))
|
(r/match-by-path router "/api/ipa/large")))
|
||||||
(is (= true (r/partial-match? (r/match-by-name router ::beer))))
|
(is (= (r/map->Match
|
||||||
(is (thrown-with-msg?
|
|
||||||
ExceptionInfo
|
|
||||||
#"^missing path-params for route /api/ipa/:size -> \#\{:size\}$"
|
|
||||||
(r/match-by-name! router ::beer))))))
|
|
||||||
|
|
||||||
(testing "linear-router"
|
|
||||||
(let [router (r/router ["/api" ["/ipa" ["/:size" ::beer]]] {:router r/linear-router})]
|
|
||||||
(is (= :linear-router (r/router-name router)))
|
|
||||||
(is (= [["/api/ipa/:size" {:name ::beer} nil]]
|
|
||||||
(r/routes router)))
|
|
||||||
(is (= true (map? (r/options router))))
|
|
||||||
(is (= (r/map->Match
|
|
||||||
{:template "/api/ipa/:size"
|
|
||||||
:meta {:name ::beer}
|
|
||||||
:path "/api/ipa/large"
|
|
||||||
:params {:size "large"}})
|
|
||||||
(r/match-by-path router "/api/ipa/large")))
|
|
||||||
(is (= (r/map->Match
|
|
||||||
{:template "/api/ipa/:size"
|
|
||||||
:meta {:name ::beer}
|
|
||||||
:path "/api/ipa/large"
|
|
||||||
:params {:size "large"}})
|
|
||||||
(r/match-by-name router ::beer {:size "large"})))
|
|
||||||
(is (= nil (r/match-by-name router "ILLEGAL")))
|
|
||||||
(is (= [::beer] (r/route-names router)))
|
|
||||||
|
|
||||||
(testing "name-based routing with missing parameters"
|
|
||||||
(is (= (r/map->PartialMatch
|
|
||||||
{:template "/api/ipa/:size"
|
{:template "/api/ipa/:size"
|
||||||
:meta {:name ::beer}
|
:meta {:name ::beer}
|
||||||
:required #{:size}
|
:path "/api/ipa/large"
|
||||||
:params nil})
|
:params {:size "large"}})
|
||||||
(r/match-by-name router ::beer)))
|
(r/match-by-name router ::beer {:size "large"})))
|
||||||
(is (= true (r/partial-match? (r/match-by-name router ::beer))))
|
(is (= nil (r/match-by-name router "ILLEGAL")))
|
||||||
(is (thrown-with-msg?
|
(is (= [::beer] (r/route-names router)))
|
||||||
ExceptionInfo
|
|
||||||
#"^missing path-params for route /api/ipa/:size -> \#\{:size\}$"
|
|
||||||
(r/match-by-name! router ::beer))))))
|
|
||||||
|
|
||||||
(testing "lookup-router"
|
(testing "name-based routing with missing parameters"
|
||||||
(let [router (r/router ["/api" ["/ipa" ["/large" ::beer]]] {:router r/lookup-router})]
|
(is (= (r/map->PartialMatch
|
||||||
(is (= :lookup-router (r/router-name router)))
|
{:template "/api/ipa/:size"
|
||||||
(is (= [["/api/ipa/large" {:name ::beer} nil]]
|
:meta {:name ::beer}
|
||||||
(r/routes router)))
|
:required #{:size}
|
||||||
(is (= true (map? (r/options router))))
|
:params nil})
|
||||||
(is (= (r/map->Match
|
(r/match-by-name router ::beer)))
|
||||||
{:template "/api/ipa/large"
|
(is (= true (r/partial-match? (r/match-by-name router ::beer))))
|
||||||
:meta {:name ::beer}
|
(is (thrown-with-msg?
|
||||||
:path "/api/ipa/large"
|
ExceptionInfo
|
||||||
:params {}})
|
#"^missing path-params for route /api/ipa/:size -> \#\{:size\}$"
|
||||||
(r/match-by-path router "/api/ipa/large")))
|
(r/match-by-name! router ::beer)))))
|
||||||
(is (= (r/map->Match
|
|
||||||
{:template "/api/ipa/large"
|
|
||||||
:meta {:name ::beer}
|
|
||||||
:path "/api/ipa/large"
|
|
||||||
:params {:size "large"}})
|
|
||||||
(r/match-by-name router ::beer {:size "large"})))
|
|
||||||
(is (= nil (r/match-by-name router "ILLEGAL")))
|
|
||||||
(is (= [::beer] (r/route-names router)))
|
|
||||||
|
|
||||||
(testing "can't be created with wildcard routes"
|
r/linear-router :linear-router
|
||||||
(is (thrown-with-msg?
|
r/prefix-tree-router :prefix-tree-router
|
||||||
ExceptionInfo
|
r/mixed-router :mixed-router))
|
||||||
#"can't create :lookup-router with wildcard routes"
|
|
||||||
(r/lookup-router
|
|
||||||
(r/resolve-routes
|
|
||||||
["/api/:version/ping"] {})))))))
|
|
||||||
|
|
||||||
(testing "single-static-path-router"
|
(testing "routers handling static paths"
|
||||||
(let [router (r/router ["/api" ["/ipa" ["/large" ::beer]]])]
|
(are [r name]
|
||||||
(is (= :single-static-path-router (r/router-name router)))
|
(let [router (r/router ["/api" ["/ipa" ["/large" ::beer]]] {:router r})]
|
||||||
(is (= [["/api/ipa/large" {:name ::beer} nil]]
|
(is (= name (r/router-name router)))
|
||||||
(r/routes router)))
|
(is (= [["/api/ipa/large" {:name ::beer} nil]]
|
||||||
(is (= true (map? (r/options router))))
|
(r/routes router)))
|
||||||
(is (= (r/map->Match
|
(is (= true (map? (r/options router))))
|
||||||
{:template "/api/ipa/large"
|
(is (= (r/map->Match
|
||||||
:meta {:name ::beer}
|
{:template "/api/ipa/large"
|
||||||
:path "/api/ipa/large"
|
:meta {:name ::beer}
|
||||||
:params {}})
|
:path "/api/ipa/large"
|
||||||
(r/match-by-path router "/api/ipa/large")))
|
:params {}})
|
||||||
(is (= (r/map->Match
|
(r/match-by-path router "/api/ipa/large")))
|
||||||
{:template "/api/ipa/large"
|
(is (= (r/map->Match
|
||||||
:meta {:name ::beer}
|
{:template "/api/ipa/large"
|
||||||
:path "/api/ipa/large"
|
:meta {:name ::beer}
|
||||||
:params {:size "large"}})
|
:path "/api/ipa/large"
|
||||||
(r/match-by-name router ::beer {:size "large"})))
|
:params {:size "large"}})
|
||||||
(is (= nil (r/match-by-name router "ILLEGAL")))
|
(r/match-by-name router ::beer {:size "large"})))
|
||||||
(is (= [::beer] (r/route-names router)))
|
(is (= nil (r/match-by-name router "ILLEGAL")))
|
||||||
|
(is (= [::beer] (r/route-names router)))
|
||||||
|
|
||||||
(testing "can't be created with wildcard routes"
|
(testing "can't be created with wildcard routes"
|
||||||
(is (thrown-with-msg?
|
(is (thrown-with-msg?
|
||||||
ExceptionInfo
|
ExceptionInfo
|
||||||
#":single-static-path-router requires exactly 1 static route"
|
#"can't create :lookup-router with wildcard routes"
|
||||||
(r/single-static-path-router
|
(r/lookup-router
|
||||||
(r/resolve-routes
|
(r/resolve-routes
|
||||||
["/api/:version/ping"] {})))))
|
["/api/:version/ping"] {}))))))
|
||||||
|
|
||||||
(testing "can't be created with multiple routes"
|
r/lookup-router :lookup-router
|
||||||
(is (thrown-with-msg?
|
r/single-static-path-router :single-static-path-router
|
||||||
ExceptionInfo
|
r/linear-router :linear-router
|
||||||
#":single-static-path-router requires exactly 1 static route"
|
r/prefix-tree-router :prefix-tree-router
|
||||||
(r/single-static-path-router
|
r/mixed-router :mixed-router))
|
||||||
(r/resolve-routes
|
|
||||||
[["/ping"]
|
|
||||||
["/pong"]] {})))))))
|
|
||||||
|
|
||||||
(testing "route coercion & compilation"
|
(testing "route coercion & compilation"
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue