diff --git a/modules/reitit-ring/src/reitit/ring.cljc b/modules/reitit-ring/src/reitit/ring.cljc index 619ae9e5..8625cda9 100644 --- a/modules/reitit-ring/src/reitit/ring.cljc +++ b/modules/reitit-ring/src/reitit/ring.cljc @@ -28,11 +28,11 @@ (update acc method expand opts) 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) childs (cond-> childs (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] (-> (middleware/compile-result [p d] opts s) (map->Endpoint) diff --git a/test/cljc/reitit/ring_test.cljc b/test/cljc/reitit/ring_test.cljc index cdb919f9..97250638 100644 --- a/test/cljc/reitit/ring_test.cljc +++ b/test/cljc/reitit/ring_test.cljc @@ -206,7 +206,7 @@ (deftest default-options-handler-test (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 (let [response {:status 200, :body "ok"}] @@ -239,6 +239,21 @@ (is (= response (app {:request-method :get, :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" (let [app (ring/ring-handler (ring/router