mirror of
https://github.com/metosin/reitit.git
synced 2025-12-24 02:48:25 +00:00
Merge pull request #294 from metosin/3-arity-options-handler
Support 3-arity handler for default-options-handler
This commit is contained in:
commit
6fa761c558
2 changed files with 21 additions and 7 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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"}))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue