Support expand on default-options-endpoint

This commit is contained in:
Tommi Reiman 2020-05-12 20:09:32 +03:00
parent 9921479c46
commit 9e7df6ca03
2 changed files with 18 additions and 3 deletions

View file

@ -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)

View file

@ -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