mirror of
https://github.com/metosin/reitit.git
synced 2025-12-16 16:01:11 +00:00
Update exception_test.clj
ring.util.http-response should be enough
This commit is contained in:
parent
cc1cd114e4
commit
1abff4937c
1 changed files with 21 additions and 22 deletions
|
|
@ -7,8 +7,7 @@
|
|||
[reitit.ring :as ring]
|
||||
[reitit.ring.coercion]
|
||||
[reitit.ring.middleware.exception :as exception]
|
||||
[ring.util.http-response :as http-response]
|
||||
[ring.util.response :as response])
|
||||
[ring.util.http-response :as http-response])
|
||||
(:import (java.sql SQLException SQLWarning)))
|
||||
|
||||
(derive ::kikka ::kukka)
|
||||
|
|
@ -35,8 +34,8 @@
|
|||
{:data {:middleware [(exception/create-exception-middleware
|
||||
(merge
|
||||
exception/default-handlers
|
||||
{::kikka (constantly (response/bad-request "kikka"))
|
||||
SQLException (constantly (response/bad-request "sql"))
|
||||
{::kikka (constantly (http-response/bad-request "kikka"))
|
||||
SQLException (constantly (http-response/bad-request "sql"))
|
||||
::exception/wrap wrap}))]}}))))]
|
||||
|
||||
(testing "normal calls work ok"
|
||||
|
|
@ -47,19 +46,19 @@
|
|||
(testing "unknown exception"
|
||||
(let [app (create (fn [_] (throw (NullPointerException.))))
|
||||
{:keys [status body] :as resp} (app {:request-method :get, :uri "/defaults"})]
|
||||
(is (response/response? resp))
|
||||
(is (http-response/response? resp))
|
||||
(is (= status 500))
|
||||
(is (= body {:type "exception"
|
||||
:class "java.lang.NullPointerException"})))
|
||||
(let [app (create (fn [_] (throw (ex-info "fail" {:type ::invalid}))))
|
||||
{:keys [status body] :as resp} (app {:request-method :get, :uri "/defaults"})]
|
||||
(is (response/response? resp))
|
||||
(is (http-response/response? resp))
|
||||
(is (= status 500))
|
||||
(is (= body {:type "exception"
|
||||
:class "clojure.lang.ExceptionInfo"}))))
|
||||
|
||||
(testing "::ring/response"
|
||||
(let [response (response/response "ok")
|
||||
(let [response (http-response/ok "ok")
|
||||
app (create (fn [_] (throw (ex-info "fail" {:type ::ring/response, :response response}))))]
|
||||
(is (= response (app {:request-method :get, :uri "/defaults"})))))
|
||||
|
||||
|
|
@ -78,54 +77,54 @@
|
|||
|
||||
(testing "::coercion/request-coercion"
|
||||
(let [app (create (fn [{{{:keys [x y]} :query} :parameters}]
|
||||
(response/response {:total (+ x y)})))]
|
||||
(http-response/ok {:total (+ x y)})))]
|
||||
|
||||
(let [{:keys [status body] :as resp} (app {:request-method :get
|
||||
:uri "/coercion"
|
||||
:query-params {"x" "1", "y" "2"}})]
|
||||
(is (response/response? resp))
|
||||
(is (http-response/response? resp))
|
||||
(is (= 200 status))
|
||||
(is (= {:total 3} body)))
|
||||
|
||||
(let [{:keys [status body] :as resp} (app {:request-method :get
|
||||
:uri "/coercion"
|
||||
:query-params {"x" "abba", "y" "2"}})]
|
||||
(is (response/response? resp))
|
||||
(is (http-response/response? resp))
|
||||
(is (= 400 status))
|
||||
(is (= :reitit.coercion/request-coercion (:type body))))
|
||||
|
||||
(let [{:keys [status body] :as resp} (app {:request-method :get
|
||||
:uri "/coercion"
|
||||
:query-params {"x" "-10", "y" "2"}})]
|
||||
(is (response/response? resp))
|
||||
(is (http-response/response? resp))
|
||||
(is (= 500 status))
|
||||
(is (= :reitit.coercion/response-coercion (:type body)))))))
|
||||
|
||||
(testing "exact :type"
|
||||
(let [app (create (fn [_] (throw (ex-info "fail" {:type ::kikka}))))
|
||||
{:keys [status body] :as resp} (app {:request-method :get, :uri "/defaults"})]
|
||||
(is (response/response? resp))
|
||||
(is (http-response/response? resp))
|
||||
(is (= status 400))
|
||||
(is (= body "kikka"))))
|
||||
|
||||
(testing "parent :type"
|
||||
(let [app (create (fn [_] (throw (ex-info "fail" {:type ::kukka}))))
|
||||
{:keys [status body] :as resp} (app {:request-method :get, :uri "/defaults"})]
|
||||
(is (response/response? resp))
|
||||
(is (http-response/response? resp))
|
||||
(is (= status 400))
|
||||
(is (= body "kikka"))))
|
||||
|
||||
(testing "exact Exception"
|
||||
(let [app (create (fn [_] (throw (SQLException.))))
|
||||
{:keys [status body] :as resp} (app {:request-method :get, :uri "/defaults"})]
|
||||
(is (response/response? resp))
|
||||
(is (http-response/response? resp))
|
||||
(is (= status 400))
|
||||
(is (= body "sql"))))
|
||||
|
||||
(testing "Exception SuperClass"
|
||||
(let [app (create (fn [_] (throw (SQLWarning.))))
|
||||
{:keys [status body] :as resp} (app {:request-method :get, :uri "/defaults"})]
|
||||
(is (response/response? resp))
|
||||
(is (http-response/response? resp))
|
||||
(is (= status 400))
|
||||
(is (= body "sql"))))
|
||||
|
||||
|
|
@ -139,11 +138,11 @@
|
|||
:headers {}
|
||||
:body "too many tries"})))]
|
||||
(let [{:keys [status body] :as resp} (app {:request-method :get, :uri "/defaults"})]
|
||||
(is (response/response? resp))
|
||||
(is (http-response/response? resp))
|
||||
(is (= status 400))
|
||||
(is (= body "sql")))
|
||||
(let [{:keys [status body] :as resp} (app {:request-method :get, :uri "/defaults"})]
|
||||
(is (response/response? resp))
|
||||
(is (http-response/response? resp))
|
||||
(is (= status 500))
|
||||
(is (= body "too many tries")))))))
|
||||
|
||||
|
|
@ -155,12 +154,12 @@
|
|||
{:parameters {:query {:x int?, :y int?}}
|
||||
:responses {200 {:body {:total pos-int?}}}
|
||||
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
||||
(response/response {:total (+ x y)}))}}]
|
||||
(http-response/ok {:total (+ x y)}))}}]
|
||||
{:data {:coercion reitit.coercion.spec/coercion
|
||||
:middleware [(exception/create-exception-middleware
|
||||
(merge
|
||||
exception/default-handlers
|
||||
{::coercion/request-coercion (fn [e _] (response/bad-request (ex-data e)) )
|
||||
{::coercion/request-coercion (fn [e _] (http-response/bad-request (ex-data e)) )
|
||||
::coercion/response-coercion (fn [e _] {:status 500
|
||||
:headers {}
|
||||
:body (ex-data e)})}))
|
||||
|
|
@ -168,13 +167,13 @@
|
|||
reitit.ring.coercion/coerce-response-middleware]}}))]
|
||||
(testing "success"
|
||||
(let [{:keys [status body] :as resp} (app {:uri "/plus", :request-method :get, :query-params {"x" "1", "y" "2"}})]
|
||||
(is (response/response? resp))
|
||||
(is (http-response/response? resp))
|
||||
(is (= 200 status))
|
||||
(is (= body {:total 3}))))
|
||||
|
||||
(testing "request error"
|
||||
(let [{:keys [status body] :as resp} (app {:uri "/plus", :request-method :get, :query-params {"x" "1", "y" "fail"}})]
|
||||
(is (response/response? resp))
|
||||
(is (http-response/response? resp))
|
||||
(is (= 400 status))
|
||||
(testing "spec error is exposed as is"
|
||||
(let [problems (:problems body)]
|
||||
|
|
@ -184,7 +183,7 @@
|
|||
|
||||
(testing "response error"
|
||||
(let [{:keys [status body] :as resp} (app {:uri "/plus", :request-method :get, :query-params {"x" "1", "y" "-2"}})]
|
||||
(is (response/response? resp))
|
||||
(is (http-response/response? resp))
|
||||
(is (= 500 status))
|
||||
(testing "spec error is exposed as is"
|
||||
(let [problems (:problems body)]
|
||||
|
|
|
|||
Loading…
Reference in a new issue