mirror of
https://github.com/metosin/reitit.git
synced 2025-12-18 17: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]])
|
[ring.util.response :as response]])
|
||||||
[clojure.string :as str]))
|
[clojure.string :as str]))
|
||||||
|
|
||||||
|
(declare get-match)
|
||||||
|
(declare get-router)
|
||||||
|
|
||||||
(def http-methods #{:get :head :post :put :delete :connect :options :trace :patch})
|
(def http-methods #{:get :head :post :put :delete :connect :options :trace :patch})
|
||||||
(defrecord 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])
|
(defrecord Endpoint [data handler path method middleware])
|
||||||
|
|
@ -53,8 +56,10 @@
|
||||||
(->methods (:handler top) data)
|
(->methods (:handler top) data)
|
||||||
childs))))
|
childs))))
|
||||||
|
|
||||||
(defn default-options-handler [_]
|
(defn default-options-handler [request]
|
||||||
{:status 200, :body ""})
|
(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
|
;; public api
|
||||||
|
|
|
||||||
|
|
@ -198,19 +198,20 @@
|
||||||
(is (= -406 (:status (app {:request-method :get, :uri "/pong"}))))))))))
|
(is (= -406 (:status (app {:request-method :get, :uri "/pong"}))))))))))
|
||||||
|
|
||||||
(deftest default-options-handler-test
|
(deftest default-options-handler-test
|
||||||
(let [response {:status 200, :body "ok"}
|
(let [response {:status 200, :body "ok"}]
|
||||||
options-response (ring/default-options-handler :request)]
|
|
||||||
|
|
||||||
(testing "with defaults"
|
(testing "with defaults"
|
||||||
(let [app (ring/ring-handler
|
(let [app (ring/ring-handler
|
||||||
(ring/router
|
(ring/router
|
||||||
[["/get" {:get (constantly response)}]
|
[["/get" {:get (constantly response)
|
||||||
|
:post (constantly response)}]
|
||||||
["/options" {:options (constantly response)}]
|
["/options" {:options (constantly response)}]
|
||||||
["/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"})))
|
(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"
|
(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