ring/routes, fix async default handling

This commit is contained in:
Tommi Reiman 2018-04-22 22:50:17 +03:00
parent f66850dacb
commit acbef8527c
2 changed files with 20 additions and 4 deletions

View file

@ -15,6 +15,22 @@
[top (assoc childs k v)]
[(assoc top k v) childs])) [{} {}] data))
(defn routes
"Create a ring handler by combining several handlers into one."
[& handlers]
(let [single-arity (apply some-fn handlers)]
(fn
([request]
(single-arity request))
([request respond raise]
(letfn [(f [handlers]
(if (seq handlers)
(let [handler (first handlers)
respond' #(if % (respond %) (f (rest handlers)))]
(handler request respond' raise))
(respond nil)))]
(f handlers))))))
(defn create-default-handler
"A default ring handler that can handle the following cases,
configured via options:
@ -81,7 +97,7 @@
(impl/fast-assoc :path-params path-params)
(impl/fast-assoc ::r/match match)
(impl/fast-assoc ::r/router router))]
(handler request respond raise))
((routes handler default-handler) request respond raise))
(default-handler request respond raise))))
{::r/router router}))))

View file

@ -17,7 +17,7 @@
(defn handler
([{:keys [::mw]}]
{:status 200 :body (conj mw :ok)})
([request respond raise]
([request respond _]
(respond (handler request))))
(deftest ring-router-test
@ -227,11 +227,11 @@
(app {:request-method :post, :uri "/ping"} respond raise)
(is (= 405 (:status (respond))))
(is (= ::nil (raise)))))
(testing "if handler rejects, nil in still returned."
(testing "if handler rejects"
(let [respond (promise)
raise (promise)]
(app {:request-method :get, :uri "/pong"} respond raise)
(is (= nil (respond)))
(is (= 406 (:status (respond))))
(is (= ::nil (raise))))))))))
(deftest middleware-transform-test