From 91e860f6c66a9c78291359c25d2f4f7f04689a0a Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Sun, 16 Jun 2019 20:11:19 +0300 Subject: [PATCH] Support 3-arity handler for default-options-handler --- modules/reitit-ring/src/reitit/ring.cljc | 14 ++++++++++---- test/cljc/reitit/ring_test.cljc | 14 +++++++++++--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/modules/reitit-ring/src/reitit/ring.cljc b/modules/reitit-ring/src/reitit/ring.cljc index 5f757d55..f0d769f4 100644 --- a/modules/reitit-ring/src/reitit/ring.cljc +++ b/modules/reitit-ring/src/reitit/ring.cljc @@ -54,10 +54,16 @@ (->methods (:handler top) data) childs)))) -(defn default-options-handler [request] - (let [methods (->> request get-match :result (keep (fn [[k v]] (if v k)))) - allow (->> methods (map (comp str/upper-case name)) (str/join ","))] - {:status 200, :body "", :headers {"Allow" allow}})) +(def default-options-handler + (let [handle (fn [request] + (let [methods (->> request get-match :result (keep (fn [[k v]] (if v k)))) + allow (->> methods (map (comp str/upper-case name)) (str/join ","))] + {:status 200, :body "", :headers {"Allow" allow}}))] + (fn + ([request] + (handle request)) + ([request respond _] + (respond (handle request)))))) ;; ;; public api diff --git a/test/cljc/reitit/ring_test.cljc b/test/cljc/reitit/ring_test.cljc index dce1ac83..5dead344 100644 --- a/test/cljc/reitit/ring_test.cljc +++ b/test/cljc/reitit/ring_test.cljc @@ -216,9 +216,17 @@ ["/any" (constantly response)]]))] (testing "endpoint with a non-options handler" - (is (= response (app {:request-method :get, :uri "/get"}))) - (is (= {:status 200, :body "", :headers {"Allow" "GET,POST,OPTIONS"}} - (app {:request-method :options, :uri "/get"})))) + (let [request {:request-method :options, :uri "/get"}] + (is (= response (app {:request-method :get, :uri "/get"}))) + (is (= {:status 200, :body "", :headers {"Allow" "GET,POST,OPTIONS"}} + (app request))) + (testing "3-arity" + (let [result (atom nil) + respond (partial reset! result) + raise ::not-called] + (app request respond raise) + (is (= {:status 200, :body "", :headers {"Allow" "GET,POST,OPTIONS"}} + @result)))))) (testing "endpoint with a options handler" (is (= response (app {:request-method :options, :uri "/options"}))))