Merge pull request #385 from caioaao/default-options-resource

Change default-options-handler to default-options-resource
This commit is contained in:
Tommi Reiman 2020-05-12 19:39:32 +03:00 committed by GitHub
commit 3cdd4963ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 10 deletions

View file

@ -28,11 +28,11 @@
(update acc method expand opts)
acc)) data http-methods)])
(defn compile-result [[path data] {::keys [default-options-handler] :as opts}]
(defn compile-result [[path data] {::keys [default-options-endpoint] :as opts}]
(let [[top childs] (group-keys data)
childs (cond-> childs
(and (not (:options childs)) (not (:handler top)) default-options-handler)
(assoc :options {:no-doc true, :handler default-options-handler}))
(and (not (:options childs)) (not (:handler top)) default-options-endpoint)
(assoc :options default-options-endpoint))
->endpoint (fn [p d m s]
(-> (middleware/compile-result [p d] opts s)
(map->Endpoint)
@ -65,6 +65,10 @@
([request respond _]
(respond (handle request))))))
(def default-options-endpoint
{:no-doc true
:handler default-options-handler})
;;
;; public api
;;
@ -75,10 +79,10 @@
available options. In addition, the following options are available:
| key | description
| ---------------------------------------|-------------
| ----------------------------------------|-------------
| `:reitit.middleware/transform` | Function or vector of functions of type `[Middleware] => [Middleware]` to transform the expanded Middleware (default: identity)
| `:reitit.middleware/registry` | Map of `keyword => IntoMiddleware` to replace keyword references into Middleware
| `:reitit.ring/default-options-handler` | Default handler for `:options` method in endpoints (default: default-options-handler)
| `:reitit.ring/default-options-endpoint` | Default resource for `:options` method in endpoints (default: default-options-endpoint)
Example:
@ -93,7 +97,12 @@
([data opts]
(let [opts (merge {:coerce coerce-handler
:compile compile-result
::default-options-handler default-options-handler} opts)]
::default-options-endpoint default-options-endpoint}
opts)]
(assert (not (contains? opts ::default-options-handler))
(str
"Option `:reitit.ring/default-options-handler` is deprecated."
" Use `:reitit.ring/default-options-endpoint` instead."))
(r/router data opts))))
(defn routes

View file

@ -205,6 +205,10 @@
(is (= 405 (:status (app {:request-method :post, :uri "/ping"}))))))))))
(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 [_] )})))))
(deftest default-options-endpoint-test
(let [response {:status 200, :body "ok"}]
(testing "with defaults"
@ -241,7 +245,7 @@
[["/get" {:get (constantly response)}]
["/options" {:options (constantly response)}]
["/any" (constantly response)]]
{::ring/default-options-handler nil}))]
{::ring/default-options-endpoint nil}))]
(testing "endpoint with a non-options handler"
(is (= response (app {:request-method :get, :uri "/get"})))