Merge pull request #294 from metosin/3-arity-options-handler

Support 3-arity handler for default-options-handler
This commit is contained in:
Tommi Reiman 2019-06-16 20:16:15 +03:00 committed by GitHub
commit 6fa761c558
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 7 deletions

View file

@ -54,10 +54,16 @@
(->methods (:handler top) data)
childs))))
(defn default-options-handler [request]
(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}}))
{:status 200, :body "", :headers {"Allow" allow}}))]
(fn
([request]
(handle request))
([request respond _]
(respond (handle request))))))
;;
;; public api

View file

@ -216,9 +216,17 @@
["/any" (constantly response)]]))]
(testing "endpoint with a non-options handler"
(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-method :options, :uri "/get"}))))
(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"}))))