This commit is contained in:
Tommi Reiman 2017-10-02 08:05:42 +03:00
parent 5c96fa045c
commit a2b04f0677
6 changed files with 71 additions and 70 deletions

View file

@ -35,9 +35,9 @@
(def compojure-api-routes (def compojure-api-routes
(routes (routes
(GET "/auth/login" [] :name :auth/login (constantly "")) (GET "/auth/login" [] {:name :auth/login} (constantly ""))
(GET "/auth/recovery/token/:token" [] :name :auth/recovery (constantly "")) (GET "/auth/recovery/token/:token" [] {:name :auth/recovery} (constantly ""))
(GET "/workspace/:project/:page" [] :name :workspace/page (constantly "")))) (GET "/workspace/:project/:page" [] {:name :workspace/page} (constantly ""))))
(def compojure-api-request (def compojure-api-request
{:compojure.api.request/lookup (routes/route-lookup-table (routes/get-routes (api compojure-api-routes)))}) {:compojure.api.request/lookup (routes/route-lookup-table (routes/get-routes (api compojure-api-routes)))})

View file

@ -133,7 +133,7 @@
:conflicts throw-on-conflicts!}) :conflicts throw-on-conflicts!})
(defn linear-router (defn linear-router
"Creates a LinearRouter from resolved routes and optional "Creates a linear-router from resolved routes and optional
expanded options. See [[router]] for available options" expanded options. See [[router]] for available options"
([routes] ([routes]
(linear-router routes {})) (linear-router routes {}))
@ -174,7 +174,7 @@
(match params))))))) (match params)))))))
(defn lookup-router (defn lookup-router
"Creates a LookupRouter from resolved routes and optional "Creates a lookup-router from resolved routes and optional
expanded options. See [[router]] for available options" expanded options. See [[router]] for available options"
([routes] ([routes]
(lookup-router routes {})) (lookup-router routes {}))
@ -182,7 +182,7 @@
(when-let [wilds (seq (filter impl/wild-route? routes))] (when-let [wilds (seq (filter impl/wild-route? routes))]
(throw (throw
(ex-info (ex-info
(str "can't create LookupRouter with wildcard routes: " wilds) (str "can't create :lookup-router with wildcard routes: " wilds)
{:wilds wilds {:wilds wilds
:routes routes}))) :routes routes})))
(let [compiled (compile-routes routes opts) (let [compiled (compile-routes routes opts)

View file

@ -1,6 +1,6 @@
(ns reitit.core-test (ns reitit.core-test
(:require [clojure.test :refer [deftest testing is are]] (:require [clojure.test :refer [deftest testing is are]]
[reitit.core :as reitit #?@(:cljs [:refer [Match Router]])]) [reitit.core :as r #?@(:cljs [:refer [Match Router]])])
#?(:clj #?(:clj
(:import (reitit.core Match Router) (:import (reitit.core Match Router)
(clojure.lang ExceptionInfo)))) (clojure.lang ExceptionInfo))))
@ -8,66 +8,67 @@
(deftest reitit-test (deftest reitit-test
(testing "linear-router" (testing "linear-router"
(let [router (reitit/router ["/api" ["/ipa" ["/:size" ::beer]]])] (let [router (r/router ["/api" ["/ipa" ["/:size" ::beer]]])]
(is (= :linear-router (reitit/router-name router))) (is (= :linear-router (r/router-name router)))
(is (= [["/api/ipa/:size" {:name ::beer} nil]] (is (= [["/api/ipa/:size" {:name ::beer} nil]]
(reitit/routes router))) (r/routes router)))
(is (= true (map? (reitit/options router)))) (is (= true (map? (r/options router))))
(is (= (reitit/map->Match (is (= (r/map->Match
{:template "/api/ipa/:size" {:template "/api/ipa/:size"
:meta {:name ::beer} :meta {:name ::beer}
:path "/api/ipa/large" :path "/api/ipa/large"
:params {:size "large"}}) :params {:size "large"}})
(reitit/match-by-path router "/api/ipa/large"))) (r/match-by-path router "/api/ipa/large")))
(is (= (reitit/map->Match (is (= (r/map->Match
{:template "/api/ipa/:size" {:template "/api/ipa/:size"
:meta {:name ::beer} :meta {:name ::beer}
:path "/api/ipa/large" :path "/api/ipa/large"
:params {:size "large"}}) :params {:size "large"}})
(reitit/match-by-name router ::beer {:size "large"}))) (r/match-by-name router ::beer {:size "large"})))
(is (= nil (reitit/match-by-name router "ILLEGAL"))) (is (= nil (r/match-by-name router "ILLEGAL")))
(is (= [::beer] (reitit/route-names router))) (is (= [::beer] (r/route-names router)))
(testing "name-based routing with missing parameters" (testing "name-based routing with missing parameters"
(is (= (reitit/map->PartialMatch (is (= (r/map->PartialMatch
{:template "/api/ipa/:size" {:template "/api/ipa/:size"
:meta {:name ::beer} :meta {:name ::beer}
:required #{:size} :required #{:size}
:params nil}) :params nil})
(reitit/match-by-name router ::beer))) (r/match-by-name router ::beer)))
(is (= true (reitit/partial-match? (reitit/match-by-name router ::beer)))) (is (= true (r/partial-match? (r/match-by-name router ::beer))))
(is (thrown-with-msg? (is (thrown-with-msg?
ExceptionInfo ExceptionInfo
#"^missing path-params for route /api/ipa/:size -> \#\{:size\}$" #"^missing path-params for route /api/ipa/:size -> \#\{:size\}$"
(reitit/match-by-name! router ::beer)))))) (r/match-by-name! router ::beer))))))
(testing "lookup-router" (testing "lookup-router"
(let [router (reitit/router ["/api" ["/ipa" ["/large" ::beer]]])] (let [router (r/router ["/api" ["/ipa" ["/large" ::beer]]] {:router r/lookup-router})]
(is (= :lookup-router (reitit/router-name router))) (is (= :lookup-router (r/router-name router)))
(is (= [["/api/ipa/large" {:name ::beer} nil]] (is (= [["/api/ipa/large" {:name ::beer} nil]]
(reitit/routes router))) (r/routes router)))
(is (= true (map? (reitit/options router)))) (is (= true (map? (r/options router))))
(is (= (reitit/map->Match (is (= (r/map->Match
{:template "/api/ipa/large" {:template "/api/ipa/large"
:meta {:name ::beer} :meta {:name ::beer}
:path "/api/ipa/large" :path "/api/ipa/large"
:params {}}) :params {}})
(reitit/match-by-path router "/api/ipa/large"))) (r/match-by-path router "/api/ipa/large")))
(is (= (reitit/map->Match (is (= (r/map->Match
{:template "/api/ipa/large" {:template "/api/ipa/large"
:meta {:name ::beer} :meta {:name ::beer}
:path "/api/ipa/large" :path "/api/ipa/large"
:params {:size "large"}}) :params {:size "large"}})
(reitit/match-by-name router ::beer {:size "large"}))) (r/match-by-name router ::beer {:size "large"})))
(is (= nil (reitit/match-by-name router "ILLEGAL"))) (is (= nil (r/match-by-name router "ILLEGAL")))
(is (= [::beer] (reitit/route-names router))) (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
#"can't create LookupRouter with wildcard routes" #"can't create :lookup-router with wildcard routes"
(reitit/lookup-router (r/lookup-router
(reitit/resolve-routes (r/resolve-routes
["/api/:version/ping"] {})))))))
["/api/:version/ping"] {}))))))) ["/api/:version/ping"] {})))))))
(testing "route coercion & compilation" (testing "route coercion & compilation"
@ -80,7 +81,7 @@
compile (fn [[path meta] _] compile (fn [[path meta] _]
(swap! compile-times inc) (swap! compile-times inc)
(constantly path)) (constantly path))
router (reitit/router router (r/router
["/api" {:roles #{:admin}} ["/api" {:roles #{:admin}}
["/ping" ::ping] ["/ping" ::ping]
["/pong" ::pong] ["/pong" ::pong]
@ -97,27 +98,27 @@
["/api/pong" {:name ::pong ["/api/pong" {:name ::pong
:path "/api/pong", :path "/api/pong",
:roles #{:admin}}]] :roles #{:admin}}]]
(map butlast (reitit/routes router))))) (map butlast (r/routes router)))))
(testing "route match contains compiled handler" (testing "route match contains compiled handler"
(is (= 2 @compile-times)) (is (= 2 @compile-times))
(let [{:keys [result]} (reitit/match-by-path router "/api/pong")] (let [{:keys [result]} (r/match-by-path router "/api/pong")]
(is result) (is result)
(is (= "/api/pong" (result))) (is (= "/api/pong" (result)))
(is (= 2 @compile-times)))))) (is (= 2 @compile-times))))))
(testing "default compile" (testing "default compile"
(let [router (reitit/router ["/ping" (constantly "ok")])] (let [router (r/router ["/ping" (constantly "ok")])]
(let [{:keys [result]} (reitit/match-by-path router "/ping")] (let [{:keys [result]} (r/match-by-path router "/ping")]
(is result) (is result)
(is (= "ok" (result))))))) (is (= "ok" (result)))))))
(testing "custom router" (testing "custom router"
(let [router (reitit/router ["/ping"] {:router (fn [_ _] (let [router (r/router ["/ping"] {:router (fn [_ _]
(reify Router (reify Router
(reitit/router-name [_] (r/router-name [_]
::custom)))})] ::custom)))})]
(is (= ::custom (reitit/router-name router))))) (is (= ::custom (r/router-name router)))))
(testing "bide sample" (testing "bide sample"
(let [routes [["/auth/login" :auth/login] (let [routes [["/auth/login" :auth/login]
@ -126,7 +127,7 @@
expected [["/auth/login" {:name :auth/login}] expected [["/auth/login" {:name :auth/login}]
["/auth/recovery/token/:token" {:name :auth/recovery}] ["/auth/recovery/token/:token" {:name :auth/recovery}]
["/workspace/:project-uuid/:page-uuid" {:name :workspace/page}]]] ["/workspace/:project-uuid/:page-uuid" {:name :workspace/page}]]]
(is (= expected (reitit/resolve-routes routes {}))))) (is (= expected (r/resolve-routes routes {})))))
(testing "ring sample" (testing "ring sample"
(let [pong (constantly "ok") (let [pong (constantly "ok")
@ -143,19 +144,19 @@
["/api/pong" {:mw [:api], :handler pong}] ["/api/pong" {:mw [:api], :handler pong}]
["/api/admin/user" {:mw [:api :admin], :roles #{:user}}] ["/api/admin/user" {:mw [:api :admin], :roles #{:user}}]
["/api/admin/db" {:mw [:api :admin :db], :roles #{:admin}}]] ["/api/admin/db" {:mw [:api :admin :db], :roles #{:admin}}]]
router (reitit/router routes)] router (r/router routes)]
(is (= expected (reitit/resolve-routes routes {}))) (is (= expected (r/resolve-routes routes {})))
(is (= (reitit/map->Match (is (= (r/map->Match
{:template "/api/user/:id/:sub-id" {:template "/api/user/:id/:sub-id"
:meta {:mw [:api], :parameters {:id "String", :sub-id "String"}} :meta {:mw [:api], :parameters {:id "String", :sub-id "String"}}
:path "/api/user/1/2" :path "/api/user/1/2"
:params {:id "1", :sub-id "2"}}) :params {:id "1", :sub-id "2"}})
(reitit/match-by-path router "/api/user/1/2")))))) (r/match-by-path router "/api/user/1/2"))))))
(deftest conflicting-routes-test (deftest conflicting-routes-test
(are [conflicting? data] (are [conflicting? data]
(let [routes (reitit/resolve-routes data {}) (let [routes (r/resolve-routes data {})
conflicts (-> routes (reitit/resolve-routes {}) (reitit/conflicting-routes))] conflicts (-> routes (r/resolve-routes {}) (r/conflicting-routes))]
(if conflicting? (seq conflicts) (nil? conflicts))) (if conflicting? (seq conflicts) (nil? conflicts)))
true [["/a"] true [["/a"]
@ -190,15 +191,15 @@
["/:b" {}] #{["/c" {}] ["/*d" {}]}, ["/:b" {}] #{["/c" {}] ["/*d" {}]},
["/c" {}] #{["/*d" {}]}} ["/c" {}] #{["/*d" {}]}}
(-> [["/a"] ["/:b"] ["/c"] ["/*d"]] (-> [["/a"] ["/:b"] ["/c"] ["/*d"]]
(reitit/resolve-routes {}) (r/resolve-routes {})
(reitit/conflicting-routes))))) (r/conflicting-routes)))))
(testing "router with conflicting routes" (testing "router with conflicting routes"
(testing "throws by default" (testing "throws by default"
(is (thrown-with-msg? (is (thrown-with-msg?
ExceptionInfo ExceptionInfo
#"Router contains conflicting routes" #"Router contains conflicting routes"
(reitit/router (r/router
[["/a"] ["/a"]])))) [["/a"] ["/a"]]))))
(testing "can be configured to ignore" (testing "can be configured to ignore"
(is (not (nil? (reitit/router [["/a"] ["/a"]] {:conflicts (constantly nil)}))))))) (is (not (nil? (r/router [["/a"] ["/a"]] {:conflicts (constantly nil)})))))))

View file

@ -2,7 +2,7 @@
(:require [clojure.test :refer [deftest testing is are]] (:require [clojure.test :refer [deftest testing is are]]
[reitit.middleware :as middleware] [reitit.middleware :as middleware]
[clojure.set :as set] [clojure.set :as set]
[reitit.core :as reitit]) [reitit.core :as r])
#?(:clj #?(:clj
(:import (clojure.lang ExceptionInfo)))) (:import (clojure.lang ExceptionInfo))))
@ -157,7 +157,7 @@
(is (= [::mw1 ::mw3 :ok ::mw3 ::mw1] (app "/api"))) (is (= [::mw1 ::mw3 :ok ::mw3 ::mw1] (app "/api")))
(testing "routes contain list of actually applied mw" (testing "routes contain list of actually applied mw"
(is (= [::mw1 ::mw3] (->> (reitit/routes router) (is (= [::mw1 ::mw3] (->> (r/routes router)
first first
last last
:middleware :middleware
@ -165,7 +165,7 @@
(testing "match contains list of actually applied mw" (testing "match contains list of actually applied mw"
(is (= [::mw1 ::mw3] (->> "/api" (is (= [::mw1 ::mw3] (->> "/api"
(reitit/match-by-path router) (r/match-by-path router)
:result :result
:middleware :middleware
(map :name)))))))))) (map :name))))))))))

View file

@ -3,7 +3,7 @@
[reitit.middleware :as middleware] [reitit.middleware :as middleware]
[reitit.ring :as ring] [reitit.ring :as ring]
[clojure.set :as set] [clojure.set :as set]
[reitit.core :as reitit]) [reitit.core :as r])
#?(:clj #?(:clj
(:import (clojure.lang ExceptionInfo)))) (:import (clojure.lang ExceptionInfo))))
@ -98,11 +98,11 @@
(testing "only top-level route names are matched" (testing "only top-level route names are matched"
(is (= [::all ::get ::users] (is (= [::all ::get ::users]
(reitit/route-names router)))) (r/route-names router))))
(testing "all named routes can be matched" (testing "all named routes can be matched"
(doseq [name (reitit/route-names router)] (doseq [name (r/route-names router)]
(is (= name (-> (reitit/match-by-name router name) :meta :name)))))))) (is (= name (-> (r/match-by-name router name) :meta :name))))))))
(defn wrap-enforce-roles [handler] (defn wrap-enforce-roles [handler]
(fn [{:keys [::roles] :as request}] (fn [{:keys [::roles] :as request}]

View file

@ -2,12 +2,12 @@
(:require [clojure.test :refer [deftest testing is are]] (:require [clojure.test :refer [deftest testing is are]]
[#?(:clj clojure.spec.test.alpha :cljs cljs.spec.test.alpha) :as stest] [#?(:clj clojure.spec.test.alpha :cljs cljs.spec.test.alpha) :as stest]
[clojure.spec.alpha :as s] [clojure.spec.alpha :as s]
[reitit.core :as reitit] [reitit.core :as r]
[reitit.spec :as spec]) [reitit.spec :as spec])
#?(:clj #?(:clj
(:import (clojure.lang ExceptionInfo)))) (:import (clojure.lang ExceptionInfo))))
(stest/instrument `reitit/router `reitit/routes) (stest/instrument)
(deftest router-spec-test (deftest router-spec-test
@ -15,7 +15,7 @@
(testing "route-data" (testing "route-data"
(are [data] (are [data]
(is (= true (reitit/router? (reitit/router data)))) (is (= true (r/router? (r/router data))))
["/api" {}] ["/api" {}]
@ -31,7 +31,7 @@
(is (thrown-with-msg? (is (thrown-with-msg?
ExceptionInfo ExceptionInfo
#"Call to #'reitit.core/router did not conform to spec" #"Call to #'reitit.core/router did not conform to spec"
(reitit/router (r/router
data))) data)))
;; missing slash ;; missing slash
@ -45,12 +45,12 @@
["/ipa"]]))) ["/ipa"]])))
(testing "routes conform to spec (can't spec protocol functions)" (testing "routes conform to spec (can't spec protocol functions)"
(is (= true (s/valid? ::spec/routes (reitit/routes (reitit/router ["/ping"])))))) (is (= true (s/valid? ::spec/routes (r/routes (r/router ["/ping"]))))))
(testing "options" (testing "options"
(are [opts] (are [opts]
(is (= true (reitit/router? (reitit/router ["/api"] opts)))) (is (= true (r/router? (r/router ["/api"] opts))))
{:path "/"} {:path "/"}
{:meta {}} {:meta {}}
@ -58,13 +58,13 @@
{:coerce (fn [route _] route)} {:coerce (fn [route _] route)}
{:compile (fn [_ _])} {:compile (fn [_ _])}
{:conflicts (fn [_])} {:conflicts (fn [_])}
{:router reitit/linear-router}) {:router r/linear-router})
(are [opts] (are [opts]
(is (thrown-with-msg? (is (thrown-with-msg?
ExceptionInfo ExceptionInfo
#"Call to #'reitit.core/router did not conform to spec" #"Call to #'reitit.core/router did not conform to spec"
(reitit/router (r/router
["/api"] opts))) ["/api"] opts)))
{:path "api"} {:path "api"}