Skip to content

Commit

Permalink
Update exception_test.clj
Browse files Browse the repository at this point in the history
ring.util.http-response should be enough
  • Loading branch information
ikitommi authored Dec 8, 2024
1 parent cc1cd11 commit 1abff49
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions test/clj/reitit/ring/middleware/exception_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
Expand All @@ -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"})))))

Expand All @@ -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"))))

Expand All @@ -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")))))))

Expand All @@ -155,26 +154,26 @@
{: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)})}))
reitit.ring.coercion/coerce-request-middleware
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)]
Expand All @@ -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)]
Expand Down

0 comments on commit 1abff49

Please sign in to comment.