mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 08:21:11 +00:00
coerce! with tests
This commit is contained in:
parent
1fede510cc
commit
dbe40e0145
3 changed files with 178 additions and 135 deletions
|
|
@ -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)}))
|
||||
|
|
|
|||
|
|
@ -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))))))))))
|
||||
|
|
|
|||
147
test/cljc/reitit/ring_coercion_test.cljc
Normal file
147
test/cljc/reitit/ring_coercion_test.cljc
Normal file
|
|
@ -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))))))))))
|
||||
Loading…
Reference in a new issue