reitit/test/cljc/reitit/coercion_test.cljc

89 lines
4.6 KiB
Text
Raw Normal View History

2017-08-30 10:24:01 +00:00
(ns reitit.coercion-test
(:require [clojure.test :refer [deftest testing is]]
2017-11-27 06:00:27 +00:00
[schema.core :as s]
2018-06-14 10:40:32 +00:00
[spec-tools.data-spec :as ds]
2017-12-09 21:07:33 +00:00
[reitit.core :as r]
[reitit.coercion :as coercion]
2018-06-14 10:40:32 +00:00
[reitit.coercion.spec]
2019-12-01 18:42:12 +00:00
[reitit.coercion.malli]
2018-06-14 10:40:32 +00:00
[reitit.coercion.schema])
#?(:clj
(:import (clojure.lang ExceptionInfo))))
2018-06-14 10:40:32 +00:00
(deftest coercion-test
2017-12-09 21:07:33 +00:00
(let [r (r/router
2018-06-14 10:40:32 +00:00
[["/schema" {:coercion reitit.coercion.schema/coercion}
2018-07-21 05:49:20 +00:00
["/:number/:keyword" {:parameters {:path {:number s/Int
2018-06-14 10:40:32 +00:00
:keyword s/Keyword}
2019-06-15 08:49:11 +00:00
:query (s/maybe {:int s/Int, :ints [s/Int], :map {s/Int s/Int}})}}]]
2019-12-01 18:42:12 +00:00
["/malli" {:coercion reitit.coercion.malli/coercion}
["/:number/:keyword" {:parameters {:path [:map [:number int?] [:keyword keyword?]]
:query [:maybe [:map [:int int?]
[:ints [:vector int?]]
[:map [:map-of int? int?]]]]}}]]
2018-06-14 10:40:32 +00:00
["/spec" {:coercion reitit.coercion.spec/coercion}
2018-07-21 05:49:20 +00:00
["/:number/:keyword" {:parameters {:path {:number int?
2018-06-14 10:40:32 +00:00
:keyword keyword?}
2019-06-15 08:49:11 +00:00
:query (ds/maybe {:int int?, :ints [int?], :map {int? int?}})}}]]
2017-12-10 14:57:09 +00:00
["/none"
2018-07-21 05:49:20 +00:00
["/:number/:keyword" {:parameters {:path {:number int?
2017-12-09 21:07:33 +00:00
:keyword keyword?}}}]]]
{:compile coercion/compile-request-coercers})]
(testing "schema-coercion"
(testing "succeeds"
(let [m (r/match-by-path r "/schema/1/abba")]
2018-06-14 10:40:32 +00:00
(is (= {:path {:keyword :abba, :number 1}, :query nil}
(coercion/coerce! m))))
(let [m (r/match-by-path r "/schema/1/abba")]
2019-06-15 08:49:11 +00:00
(is (= {:path {:keyword :abba, :number 1}, :query {:int 10, :ints [1,2,3], :map {1 1}}}
2019-12-01 18:42:12 +00:00
(coercion/coerce! (assoc m :query-params {"int" "10", "ints" ["1" "2" "3"], "map" {:1 "1"}}))))))
2017-12-09 21:07:33 +00:00
(testing "throws with invalid input"
(let [m (r/match-by-path r "/schema/kikka/abba")]
(is (thrown? ExceptionInfo (coercion/coerce! m))))))
2019-12-01 18:42:12 +00:00
(testing "malli-coercion"
(testing "succeeds"
(let [m (r/match-by-path r "/malli/1/abba")]
(is (= {:path {:keyword :abba, :number 1}, :query nil}
(coercion/coerce! m))))
(let [m (r/match-by-path r "/malli/1/abba")]
(is (= {:path {:keyword :abba, :number 1}, :query {:int 10, :ints [1,2,3], :map {1 1}}}
(coercion/coerce! (assoc m :query-params {"int" "10", "ints" ["1" "2" "3"], "map" {:1 "1"}}))))))
(testing "throws with invalid input"
(let [m (r/match-by-path r "/malli/kikka/abba")]
(is (thrown? ExceptionInfo (coercion/coerce! m))))))
2017-12-09 21:07:33 +00:00
(testing "spec-coercion"
(testing "succeeds"
(let [m (r/match-by-path r "/spec/1/abba")]
2018-06-14 10:40:32 +00:00
(is (= {:path {:keyword :abba, :number 1}, :query nil}
(coercion/coerce! m))))
(let [m (r/match-by-path r "/schema/1/abba")]
2019-06-15 08:49:11 +00:00
(is (= {:path {:keyword :abba, :number 1}, :query {:int 10, :ints [1,2,3], :map {1 1}}}
2019-12-01 18:42:12 +00:00
(coercion/coerce! (assoc m :query-params {"int" "10", "ints" ["1" "2" "3"], "map" {:1 "1"}}))))))
2017-12-09 21:07:33 +00:00
(testing "throws with invalid input"
(let [m (r/match-by-path r "/spec/kikka/abba")]
2017-12-10 14:57:09 +00:00
(is (thrown? ExceptionInfo (coercion/coerce! m))))))
(testing "no coercion defined"
(testing "doesn't coerce"
(let [m (r/match-by-path r "/none/1/abba")]
(is (= nil (coercion/coerce! m))))
(let [m (r/match-by-path r "/none/kikka/abba")]
(is (= nil (coercion/coerce! m))))))))
2018-09-22 18:19:44 +00:00
(defn match-by-path-and-coerce! [router path]
(if-let [match (r/match-by-path router path)]
(assoc match :parameters (coercion/coerce! match))))
(deftest data-spec-example-test
(let [router (r/router
["/:company/users/:user-id" {:name ::user-view
:coercion reitit.coercion.spec/coercion
:parameters {:path {:company string?
:user-id int?}}}]
{:compile coercion/compile-request-coercers})]
(is (= {:path {:user-id 123, :company "metosin"}}
(:parameters (match-by-path-and-coerce! router "/metosin/users/123"))))))