mirror of
https://github.com/metosin/reitit.git
synced 2026-01-02 22:48:26 +00:00
Welcome Middleware registry!
This commit is contained in:
parent
055a03c793
commit
94431352b8
2 changed files with 31 additions and 0 deletions
|
|
@ -13,6 +13,17 @@
|
|||
|
||||
(extend-protocol IntoMiddleware
|
||||
|
||||
#?(:clj clojure.lang.Keyword
|
||||
:cljs cljs.core.Keyword)
|
||||
(into-middleware [this data {:keys [::registry] :as opts}]
|
||||
(or (if-let [middleware (if registry (registry this))]
|
||||
(into-middleware middleware data opts))
|
||||
(throw
|
||||
(ex-info
|
||||
(str "Middleware " (pr-str this) " not found in registry.")
|
||||
{:keyword this
|
||||
:registry registry}))))
|
||||
|
||||
#?(:clj clojure.lang.APersistentVector
|
||||
:cljs cljs.core.PersistentVector)
|
||||
(into-middleware [[f & args] data opts]
|
||||
|
|
|
|||
|
|
@ -45,6 +45,26 @@
|
|||
(is (= [:value :ok] (app request)))
|
||||
(is (= 1 @calls)))))
|
||||
|
||||
(testing "as keyword"
|
||||
(reset! calls 0)
|
||||
(let [app (create [:wrap] {::middleware/registry {:wrap #(wrap % :value)}})]
|
||||
(dotimes [_ 10]
|
||||
(is (= [:value :ok] (app request)))
|
||||
(is (= 1 @calls)))))
|
||||
|
||||
(testing "as keyword vector"
|
||||
(reset! calls 0)
|
||||
(let [app (create [[:wrap :value]] {::middleware/registry {:wrap wrap}})]
|
||||
(dotimes [_ 10]
|
||||
(is (= [:value :ok] (app request)))
|
||||
(is (= 1 @calls)))))
|
||||
|
||||
(testing "missing keyword"
|
||||
(is (thrown-with-msg?
|
||||
ExceptionInfo
|
||||
#"Middleware :wrap not found in registry"
|
||||
(create [:wrap]))))
|
||||
|
||||
(testing "as function vector with value(s)"
|
||||
(reset! calls 0)
|
||||
(let [app (create [[wrap :value]])]
|
||||
|
|
|
|||
Loading…
Reference in a new issue