mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 08:21:11 +00:00
Support expand on default-options-endpoint
This commit is contained in:
parent
9921479c46
commit
9e7df6ca03
2 changed files with 18 additions and 3 deletions
|
|
@ -28,11 +28,11 @@
|
||||||
(update acc method expand opts)
|
(update acc method expand opts)
|
||||||
acc)) data http-methods)])
|
acc)) data http-methods)])
|
||||||
|
|
||||||
(defn compile-result [[path data] {::keys [default-options-endpoint] :as opts}]
|
(defn compile-result [[path data] {:keys [::default-options-endpoint expand] :as opts}]
|
||||||
(let [[top childs] (group-keys data)
|
(let [[top childs] (group-keys data)
|
||||||
childs (cond-> childs
|
childs (cond-> childs
|
||||||
(and (not (:options childs)) (not (:handler top)) default-options-endpoint)
|
(and (not (:options childs)) (not (:handler top)) default-options-endpoint)
|
||||||
(assoc :options default-options-endpoint))
|
(assoc :options (expand default-options-endpoint opts)))
|
||||||
->endpoint (fn [p d m s]
|
->endpoint (fn [p d m s]
|
||||||
(-> (middleware/compile-result [p d] opts s)
|
(-> (middleware/compile-result [p d] opts s)
|
||||||
(map->Endpoint)
|
(map->Endpoint)
|
||||||
|
|
|
||||||
|
|
@ -206,7 +206,7 @@
|
||||||
|
|
||||||
(deftest default-options-handler-test
|
(deftest default-options-handler-test
|
||||||
(testing "Assertion fails when using deprecated options-handler"
|
(testing "Assertion fails when using deprecated options-handler"
|
||||||
(is (thrown? java.lang.AssertionError (ring/router [] {::ring/default-options-handler (fn [_] )})))))
|
(is (thrown? java.lang.AssertionError (ring/router [] {::ring/default-options-handler (fn [_])})))))
|
||||||
|
|
||||||
(deftest default-options-endpoint-test
|
(deftest default-options-endpoint-test
|
||||||
(let [response {:status 200, :body "ok"}]
|
(let [response {:status 200, :body "ok"}]
|
||||||
|
|
@ -239,6 +239,21 @@
|
||||||
(is (= response (app {:request-method :get, :uri "/any"})))
|
(is (= response (app {:request-method :get, :uri "/any"})))
|
||||||
(is (= response (app {:request-method :options, :uri "/any"}))))))
|
(is (= response (app {:request-method :options, :uri "/any"}))))))
|
||||||
|
|
||||||
|
(testing "custom endpoint works (and expands automatically)"
|
||||||
|
(doseq [endpoint [{:handler (constantly {:status 200, :body "ok"})}
|
||||||
|
(constantly {:status 200, :body "ok"})]]
|
||||||
|
(let [response {:status 200, :body "ok"}
|
||||||
|
app (ring/ring-handler
|
||||||
|
(ring/router
|
||||||
|
["/get" {:get (constantly response)
|
||||||
|
:post (constantly response)}]
|
||||||
|
{::ring/default-options-endpoint endpoint}))]
|
||||||
|
|
||||||
|
(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 "ok"} (app request))))))))
|
||||||
|
|
||||||
(testing "disabled via options"
|
(testing "disabled via options"
|
||||||
(let [app (ring/ring-handler
|
(let [app (ring/ring-handler
|
||||||
(ring/router
|
(ring/router
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue