Welcome Middleware registry!

This commit is contained in:
Tommi Reiman 2018-07-27 15:14:00 +03:00
parent 055a03c793
commit 94431352b8
2 changed files with 31 additions and 0 deletions

View file

@ -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]

View file

@ -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]])]