mirror of
https://github.com/metosin/reitit.git
synced 2026-01-22 14:19:03 +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)
|
(->methods (:handler top) data)
|
||||||
childs))))
|
childs))))
|
||||||
|
|
||||||
(defn default-options-handler [request]
|
(def default-options-handler
|
||||||
(let [methods (->> request get-match :result (keep (fn [[k v]] (if v k))))
|
(let [handle (fn [request]
|
||||||
allow (->> methods (map (comp str/upper-case name)) (str/join ","))]
|
(let [methods (->> request get-match :result (keep (fn [[k v]] (if v k))))
|
||||||
{:status 200, :body "", :headers {"Allow" allow}}))
|
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
|
;; public api
|
||||||
|
|
|
||||||
|
|
@ -216,9 +216,17 @@
|
||||||
["/any" (constantly response)]]))]
|
["/any" (constantly response)]]))]
|
||||||
|
|
||||||
(testing "endpoint with a non-options handler"
|
(testing "endpoint with a non-options handler"
|
||||||
(is (= response (app {:request-method :get, :uri "/get"})))
|
(let [request {:request-method :options, :uri "/get"}]
|
||||||
(is (= {:status 200, :body "", :headers {"Allow" "GET,POST,OPTIONS"}}
|
(is (= response (app {:request-method :get, :uri "/get"})))
|
||||||
(app {:request-method :options, :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"
|
(testing "endpoint with a options handler"
|
||||||
(is (= response (app {:request-method :options, :uri "/options"}))))
|
(is (= response (app {:request-method :options, :uri "/options"}))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue