mirror of
https://github.com/metosin/reitit.git
synced 2025-12-16 16:01:11 +00:00
Set Allow-header on default-options-handler
This commit is contained in:
parent
f14c982202
commit
a620ec5999
2 changed files with 12 additions and 6 deletions
|
|
@ -7,6 +7,9 @@
|
|||
[ring.util.response :as response]])
|
||||
[clojure.string :as str]))
|
||||
|
||||
(declare get-match)
|
||||
(declare get-router)
|
||||
|
||||
(def http-methods #{:get :head :post :put :delete :connect :options :trace :patch})
|
||||
(defrecord Methods [get head post put delete connect options trace patch])
|
||||
(defrecord Endpoint [data handler path method middleware])
|
||||
|
|
@ -53,8 +56,10 @@
|
|||
(->methods (:handler top) data)
|
||||
childs))))
|
||||
|
||||
(defn default-options-handler [_]
|
||||
{:status 200, :body ""})
|
||||
(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}}))
|
||||
|
||||
;;
|
||||
;; public api
|
||||
|
|
|
|||
|
|
@ -198,19 +198,20 @@
|
|||
(is (= -406 (:status (app {:request-method :get, :uri "/pong"}))))))))))
|
||||
|
||||
(deftest default-options-handler-test
|
||||
(let [response {:status 200, :body "ok"}
|
||||
options-response (ring/default-options-handler :request)]
|
||||
(let [response {:status 200, :body "ok"}]
|
||||
|
||||
(testing "with defaults"
|
||||
(let [app (ring/ring-handler
|
||||
(ring/router
|
||||
[["/get" {:get (constantly response)}]
|
||||
[["/get" {:get (constantly response)
|
||||
:post (constantly response)}]
|
||||
["/options" {:options (constantly response)}]
|
||||
["/any" (constantly response)]]))]
|
||||
|
||||
(testing "endpoint with a non-options handler"
|
||||
(is (= response (app {:request-method :get, :uri "/get"})))
|
||||
(is (= options-response (app {:request-method :options, :uri "/get"}))))
|
||||
(is (= {:status 200, :body "", :headers {"Allow" "GET,POST,OPTIONS"}}
|
||||
(app {:request-method :options, :uri "/get"}))))
|
||||
|
||||
(testing "endpoint with a options handler"
|
||||
(is (= response (app {:request-method :options, :uri "/options"}))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue