Update exception_test.clj

ring.util.http-response should be enough
This commit is contained in:
Tommi Reiman 2024-12-08 14:54:14 +02:00 committed by GitHub
parent cc1cd114e4
commit 1abff4937c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -7,8 +7,7 @@
[reitit.ring :as ring] [reitit.ring :as ring]
[reitit.ring.coercion] [reitit.ring.coercion]
[reitit.ring.middleware.exception :as exception] [reitit.ring.middleware.exception :as exception]
[ring.util.http-response :as http-response] [ring.util.http-response :as http-response])
[ring.util.response :as response])
(:import (java.sql SQLException SQLWarning))) (:import (java.sql SQLException SQLWarning)))
(derive ::kikka ::kukka) (derive ::kikka ::kukka)
@ -35,8 +34,8 @@
{:data {:middleware [(exception/create-exception-middleware {:data {:middleware [(exception/create-exception-middleware
(merge (merge
exception/default-handlers exception/default-handlers
{::kikka (constantly (response/bad-request "kikka")) {::kikka (constantly (http-response/bad-request "kikka"))
SQLException (constantly (response/bad-request "sql")) SQLException (constantly (http-response/bad-request "sql"))
::exception/wrap wrap}))]}}))))] ::exception/wrap wrap}))]}}))))]
(testing "normal calls work ok" (testing "normal calls work ok"
@ -47,19 +46,19 @@
(testing "unknown exception" (testing "unknown exception"
(let [app (create (fn [_] (throw (NullPointerException.)))) (let [app (create (fn [_] (throw (NullPointerException.))))
{:keys [status body] :as resp} (app {:request-method :get, :uri "/defaults"})] {:keys [status body] :as resp} (app {:request-method :get, :uri "/defaults"})]
(is (response/response? resp)) (is (http-response/response? resp))
(is (= status 500)) (is (= status 500))
(is (= body {:type "exception" (is (= body {:type "exception"
:class "java.lang.NullPointerException"}))) :class "java.lang.NullPointerException"})))
(let [app (create (fn [_] (throw (ex-info "fail" {:type ::invalid})))) (let [app (create (fn [_] (throw (ex-info "fail" {:type ::invalid}))))
{:keys [status body] :as resp} (app {:request-method :get, :uri "/defaults"})] {:keys [status body] :as resp} (app {:request-method :get, :uri "/defaults"})]
(is (response/response? resp)) (is (http-response/response? resp))
(is (= status 500)) (is (= status 500))
(is (= body {:type "exception" (is (= body {:type "exception"
:class "clojure.lang.ExceptionInfo"})))) :class "clojure.lang.ExceptionInfo"}))))
(testing "::ring/response" (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}))))] app (create (fn [_] (throw (ex-info "fail" {:type ::ring/response, :response response}))))]
(is (= response (app {:request-method :get, :uri "/defaults"}))))) (is (= response (app {:request-method :get, :uri "/defaults"})))))
@ -78,54 +77,54 @@
(testing "::coercion/request-coercion" (testing "::coercion/request-coercion"
(let [app (create (fn [{{{:keys [x y]} :query} :parameters}] (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 (let [{:keys [status body] :as resp} (app {:request-method :get
:uri "/coercion" :uri "/coercion"
:query-params {"x" "1", "y" "2"}})] :query-params {"x" "1", "y" "2"}})]
(is (response/response? resp)) (is (http-response/response? resp))
(is (= 200 status)) (is (= 200 status))
(is (= {:total 3} body))) (is (= {:total 3} body)))
(let [{:keys [status body] :as resp} (app {:request-method :get (let [{:keys [status body] :as resp} (app {:request-method :get
:uri "/coercion" :uri "/coercion"
:query-params {"x" "abba", "y" "2"}})] :query-params {"x" "abba", "y" "2"}})]
(is (response/response? resp)) (is (http-response/response? resp))
(is (= 400 status)) (is (= 400 status))
(is (= :reitit.coercion/request-coercion (:type body)))) (is (= :reitit.coercion/request-coercion (:type body))))
(let [{:keys [status body] :as resp} (app {:request-method :get (let [{:keys [status body] :as resp} (app {:request-method :get
:uri "/coercion" :uri "/coercion"
:query-params {"x" "-10", "y" "2"}})] :query-params {"x" "-10", "y" "2"}})]
(is (response/response? resp)) (is (http-response/response? resp))
(is (= 500 status)) (is (= 500 status))
(is (= :reitit.coercion/response-coercion (:type body))))))) (is (= :reitit.coercion/response-coercion (:type body)))))))
(testing "exact :type" (testing "exact :type"
(let [app (create (fn [_] (throw (ex-info "fail" {:type ::kikka})))) (let [app (create (fn [_] (throw (ex-info "fail" {:type ::kikka}))))
{:keys [status body] :as resp} (app {:request-method :get, :uri "/defaults"})] {:keys [status body] :as resp} (app {:request-method :get, :uri "/defaults"})]
(is (response/response? resp)) (is (http-response/response? resp))
(is (= status 400)) (is (= status 400))
(is (= body "kikka")))) (is (= body "kikka"))))
(testing "parent :type" (testing "parent :type"
(let [app (create (fn [_] (throw (ex-info "fail" {:type ::kukka})))) (let [app (create (fn [_] (throw (ex-info "fail" {:type ::kukka}))))
{:keys [status body] :as resp} (app {:request-method :get, :uri "/defaults"})] {:keys [status body] :as resp} (app {:request-method :get, :uri "/defaults"})]
(is (response/response? resp)) (is (http-response/response? resp))
(is (= status 400)) (is (= status 400))
(is (= body "kikka")))) (is (= body "kikka"))))
(testing "exact Exception" (testing "exact Exception"
(let [app (create (fn [_] (throw (SQLException.)))) (let [app (create (fn [_] (throw (SQLException.))))
{:keys [status body] :as resp} (app {:request-method :get, :uri "/defaults"})] {:keys [status body] :as resp} (app {:request-method :get, :uri "/defaults"})]
(is (response/response? resp)) (is (http-response/response? resp))
(is (= status 400)) (is (= status 400))
(is (= body "sql")))) (is (= body "sql"))))
(testing "Exception SuperClass" (testing "Exception SuperClass"
(let [app (create (fn [_] (throw (SQLWarning.)))) (let [app (create (fn [_] (throw (SQLWarning.))))
{:keys [status body] :as resp} (app {:request-method :get, :uri "/defaults"})] {:keys [status body] :as resp} (app {:request-method :get, :uri "/defaults"})]
(is (response/response? resp)) (is (http-response/response? resp))
(is (= status 400)) (is (= status 400))
(is (= body "sql")))) (is (= body "sql"))))
@ -139,11 +138,11 @@
:headers {} :headers {}
:body "too many tries"})))] :body "too many tries"})))]
(let [{:keys [status body] :as resp} (app {:request-method :get, :uri "/defaults"})] (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 (= status 400))
(is (= body "sql"))) (is (= body "sql")))
(let [{:keys [status body] :as resp} (app {:request-method :get, :uri "/defaults"})] (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 (= status 500))
(is (= body "too many tries"))))))) (is (= body "too many tries")))))))
@ -155,12 +154,12 @@
{:parameters {:query {:x int?, :y int?}} {:parameters {:query {:x int?, :y int?}}
:responses {200 {:body {:total pos-int?}}} :responses {200 {:body {:total pos-int?}}}
:handler (fn [{{{:keys [x y]} :query} :parameters}] :handler (fn [{{{:keys [x y]} :query} :parameters}]
(response/response {:total (+ x y)}))}}] (http-response/ok {:total (+ x y)}))}}]
{:data {:coercion reitit.coercion.spec/coercion {:data {:coercion reitit.coercion.spec/coercion
:middleware [(exception/create-exception-middleware :middleware [(exception/create-exception-middleware
(merge (merge
exception/default-handlers 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 ::coercion/response-coercion (fn [e _] {:status 500
:headers {} :headers {}
:body (ex-data e)})})) :body (ex-data e)})}))
@ -168,13 +167,13 @@
reitit.ring.coercion/coerce-response-middleware]}}))] reitit.ring.coercion/coerce-response-middleware]}}))]
(testing "success" (testing "success"
(let [{:keys [status body] :as resp} (app {:uri "/plus", :request-method :get, :query-params {"x" "1", "y" "2"}})] (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 (= 200 status))
(is (= body {:total 3})))) (is (= body {:total 3}))))
(testing "request error" (testing "request error"
(let [{:keys [status body] :as resp} (app {:uri "/plus", :request-method :get, :query-params {"x" "1", "y" "fail"}})] (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)) (is (= 400 status))
(testing "spec error is exposed as is" (testing "spec error is exposed as is"
(let [problems (:problems body)] (let [problems (:problems body)]
@ -184,7 +183,7 @@
(testing "response error" (testing "response error"
(let [{:keys [status body] :as resp} (app {:uri "/plus", :request-method :get, :query-params {"x" "1", "y" "-2"}})] (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)) (is (= 500 status))
(testing "spec error is exposed as is" (testing "spec error is exposed as is"
(let [problems (:problems body)] (let [problems (:problems body)]