From dbe40e01455d72eb02013d4eb6e839fc30de58bb Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Sat, 9 Dec 2017 23:07:33 +0200 Subject: [PATCH] coerce! with tests --- modules/reitit-core/src/reitit/coercion.cljc | 3 + test/cljc/reitit/coercion_test.cljc | 163 ++++--------------- test/cljc/reitit/ring_coercion_test.cljc | 147 +++++++++++++++++ 3 files changed, 178 insertions(+), 135 deletions(-) create mode 100644 test/cljc/reitit/ring_coercion_test.cljc diff --git a/modules/reitit-core/src/reitit/coercion.cljc b/modules/reitit-core/src/reitit/coercion.cljc index af9e2ba2..b05825e9 100644 --- a/modules/reitit-core/src/reitit/coercion.cljc +++ b/modules/reitit-core/src/reitit/coercion.cljc @@ -132,3 +132,6 @@ (defn compile-request-coercers [[p {:keys [parameters coercion]}] opts] (if (and parameters coercion) (request-coercers coercion parameters opts))) + +(defn coerce! [match] + (coerce-request (:result match) {:path-params (:params match)})) diff --git a/test/cljc/reitit/coercion_test.cljc b/test/cljc/reitit/coercion_test.cljc index 9442505c..6fac8a34 100644 --- a/test/cljc/reitit/coercion_test.cljc +++ b/test/cljc/reitit/coercion_test.cljc @@ -1,147 +1,40 @@ (ns reitit.coercion-test (:require [clojure.test :refer [deftest testing is]] [schema.core :as s] - [reitit.ring :as ring] - [reitit.ring.coercion-middleware :as coercion-middleware] + [reitit.core :as r] + [reitit.coercion :as coercion] [reitit.coercion.spec :as spec] [reitit.coercion.schema :as schema]) #?(:clj (:import (clojure.lang ExceptionInfo)))) -(defn handler [{{{:keys [a]} :query - {:keys [b]} :body - {:keys [c]} :form - {:keys [d]} :header - {:keys [e]} :path} :parameters}] - {:status 200 - :body {:total (+ a b c d e)}}) - -(def valid-request - {:uri "/api/plus/5" - :request-method :get - :query-params {"a" "1"} - :body-params {:b 2} - :form-params {:c 3} - :header-params {:d 4}}) - -(def invalid-request - {:uri "/api/plus/5" - :request-method :get}) - -(def invalid-request2 - {:uri "/api/plus/5" - :request-method :get - :query-params {"a" "1"} - :body-params {:b 2} - :form-params {:c 3} - :header-params {:d -40}}) - (deftest spec-coercion-test - (let [create (fn [middleware] - (ring/ring-handler - (ring/router - ["/api" - ["/plus/:e" - {:get {:parameters {:query {:a int?} - :body {:b int?} - :form {:c int?} - :header {:d int?} - :path {:e int?}} - :responses {200 {:schema {:total pos-int?}}} - :handler handler}}]] - {:data {:middleware middleware - :coercion spec/coercion}})))] + (let [r (r/router + [["/schema" {:coercion schema/coercion} + ["/:number/:keyword" {:name ::user + :parameters {:path {:number s/Int + :keyword s/Keyword}}}]] + ["/spec" {:coercion spec/coercion} + ["/:number/:keyword" {:name ::user + :parameters {:path {:number int? + :keyword keyword?}}}]]] + {:compile coercion/compile-request-coercers})] - (testing "withut exception handling" - (let [app (create [coercion-middleware/coerce-request-middleware - coercion-middleware/coerce-response-middleware])] + (testing "schema-coercion" + (testing "succeeds" + (let [m (r/match-by-path r "/schema/1/abba")] + (is (= {:path {:keyword :abba, :number 1}} + (coercion/coerce! m))))) + (testing "throws with invalid input" + (let [m (r/match-by-path r "/schema/kikka/abba")] + (is (thrown? ExceptionInfo (coercion/coerce! m)))))) - (testing "all good" - (is (= {:status 200 - :body {:total 15}} - (app valid-request)))) + (testing "spec-coercion" + (testing "succeeds" + (let [m (r/match-by-path r "/spec/1/abba")] + (is (= {:path {:keyword :abba, :number 1}} + (coercion/coerce! m))))) + (testing "throws with invalid input" + (let [m (r/match-by-path r "/spec/kikka/abba")] + (is (thrown? ExceptionInfo (coercion/coerce! m)))))))) - (testing "invalid request" - (is (thrown-with-msg? - ExceptionInfo - #"Request coercion failed" - (app invalid-request)))) - - (testing "invalid response" - (is (thrown-with-msg? - ExceptionInfo - #"Response coercion failed" - (app invalid-request2)))))) - - (testing "with exception handling" - (let [app (create [coercion-middleware/coerce-exceptions-middleware - coercion-middleware/coerce-request-middleware - coercion-middleware/coerce-response-middleware])] - - (testing "all good" - (is (= {:status 200 - :body {:total 15}} - (app valid-request)))) - - (testing "invalid request" - (let [{:keys [status body]} (app invalid-request)] - (is (= 400 status)))) - - (testing "invalid response" - (let [{:keys [status body]} (app invalid-request2)] - (is (= 500 status)))))))) - -(deftest schema-coercion-test - (let [create (fn [middleware] - (ring/ring-handler - (ring/router - ["/api" - ["/plus/:e" - {:get {:parameters {:query {:a s/Int} - :body {:b s/Int} - :form {:c s/Int} - :header {:d s/Int} - :path {:e s/Int}} - :responses {200 {:schema {:total (s/constrained s/Int pos? 'positive)}}} - :handler handler}}]] - {:data {:middleware middleware - :coercion schema/coercion}})))] - - (testing "withut exception handling" - (let [app (create [coercion-middleware/coerce-request-middleware - coercion-middleware/coerce-response-middleware])] - - (testing "all good" - (is (= {:status 200 - :body {:total 15}} - (app valid-request)))) - - (testing "invalid request" - (is (thrown-with-msg? - ExceptionInfo - #"Request coercion failed" - (app invalid-request)))) - - (testing "invalid response" - (is (thrown-with-msg? - ExceptionInfo - #"Response coercion failed" - (app invalid-request2)))) - - (testing "with exception handling" - (let [app (create [coercion-middleware/coerce-exceptions-middleware - coercion-middleware/coerce-request-middleware - coercion-middleware/coerce-response-middleware])] - - (testing "all good" - (is (= {:status 200 - :body {:total 15}} - (app valid-request)))) - - (testing "invalid request" - (let [{:keys [status body]} (app invalid-request)] - (is (= 400 status)))) - - (testing "invalid response" - (let [{:keys [status body]} (app invalid-request2)] - (is (= 500 status)))))))))) diff --git a/test/cljc/reitit/ring_coercion_test.cljc b/test/cljc/reitit/ring_coercion_test.cljc new file mode 100644 index 00000000..eef9372d --- /dev/null +++ b/test/cljc/reitit/ring_coercion_test.cljc @@ -0,0 +1,147 @@ +(ns reitit.ring-coercion-test + (:require [clojure.test :refer [deftest testing is]] + [schema.core :as s] + [reitit.ring :as ring] + [reitit.ring.coercion-middleware :as coercion-middleware] + [reitit.coercion.spec :as spec] + [reitit.coercion.schema :as schema]) + #?(:clj + (:import (clojure.lang ExceptionInfo)))) + +(defn handler [{{{:keys [a]} :query + {:keys [b]} :body + {:keys [c]} :form + {:keys [d]} :header + {:keys [e]} :path} :parameters}] + {:status 200 + :body {:total (+ a b c d e)}}) + +(def valid-request + {:uri "/api/plus/5" + :request-method :get + :query-params {"a" "1"} + :body-params {:b 2} + :form-params {:c 3} + :header-params {:d 4}}) + +(def invalid-request + {:uri "/api/plus/5" + :request-method :get}) + +(def invalid-request2 + {:uri "/api/plus/5" + :request-method :get + :query-params {"a" "1"} + :body-params {:b 2} + :form-params {:c 3} + :header-params {:d -40}}) + +(deftest spec-coercion-test + (let [create (fn [middleware] + (ring/ring-handler + (ring/router + ["/api" + ["/plus/:e" + {:get {:parameters {:query {:a int?} + :body {:b int?} + :form {:c int?} + :header {:d int?} + :path {:e int?}} + :responses {200 {:schema {:total pos-int?}}} + :handler handler}}]] + {:data {:middleware middleware + :coercion spec/coercion}})))] + + (testing "withut exception handling" + (let [app (create [coercion-middleware/coerce-request-middleware + coercion-middleware/coerce-response-middleware])] + + (testing "all good" + (is (= {:status 200 + :body {:total 15}} + (app valid-request)))) + + (testing "invalid request" + (is (thrown-with-msg? + ExceptionInfo + #"Request coercion failed" + (app invalid-request)))) + + (testing "invalid response" + (is (thrown-with-msg? + ExceptionInfo + #"Response coercion failed" + (app invalid-request2)))))) + + (testing "with exception handling" + (let [app (create [coercion-middleware/coerce-exceptions-middleware + coercion-middleware/coerce-request-middleware + coercion-middleware/coerce-response-middleware])] + + (testing "all good" + (is (= {:status 200 + :body {:total 15}} + (app valid-request)))) + + (testing "invalid request" + (let [{:keys [status body]} (app invalid-request)] + (is (= 400 status)))) + + (testing "invalid response" + (let [{:keys [status body]} (app invalid-request2)] + (is (= 500 status)))))))) + +(deftest schema-coercion-test + (let [create (fn [middleware] + (ring/ring-handler + (ring/router + ["/api" + ["/plus/:e" + {:get {:parameters {:query {:a s/Int} + :body {:b s/Int} + :form {:c s/Int} + :header {:d s/Int} + :path {:e s/Int}} + :responses {200 {:schema {:total (s/constrained s/Int pos? 'positive)}}} + :handler handler}}]] + {:data {:middleware middleware + :coercion schema/coercion}})))] + + (testing "withut exception handling" + (let [app (create [coercion-middleware/coerce-request-middleware + coercion-middleware/coerce-response-middleware])] + + (testing "all good" + (is (= {:status 200 + :body {:total 15}} + (app valid-request)))) + + (testing "invalid request" + (is (thrown-with-msg? + ExceptionInfo + #"Request coercion failed" + (app invalid-request)))) + + (testing "invalid response" + (is (thrown-with-msg? + ExceptionInfo + #"Response coercion failed" + (app invalid-request2)))) + + (testing "with exception handling" + (let [app (create [coercion-middleware/coerce-exceptions-middleware + coercion-middleware/coerce-request-middleware + coercion-middleware/coerce-response-middleware])] + + (testing "all good" + (is (= {:status 200 + :body {:total 15}} + (app valid-request)))) + + (testing "invalid request" + (let [{:keys [status body]} (app invalid-request)] + (is (= 400 status)))) + + (testing "invalid response" + (let [{:keys [status body]} (app invalid-request2)] + (is (= 500 status))))))))))