reitit/test/cljc/reitit/coercion_test.cljc

52 lines
2.1 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]
2017-12-09 21:07:33 +00:00
[reitit.core :as r]
[reitit.coercion :as coercion]
2017-12-09 20:49:32 +00:00
[reitit.coercion.spec :as spec]
[reitit.coercion.schema :as schema])
#?(:clj
(:import (clojure.lang ExceptionInfo))))
2017-11-26 19:51:43 +00:00
(deftest spec-coercion-test
2017-12-09 21:07:33 +00:00
(let [r (r/router
[["/schema" {:coercion schema/coercion}
["/:number/:keyword" {:name ::user
:parameters {:path {:number s/Int
:keyword s/Keyword}}}]]
["/spec" {:coercion spec/coercion}
2017-12-10 14:57:09 +00:00
["/:number/:keyword" {:name ::user
:parameters {:path {:number int?
:keyword keyword?}}}]]
["/none"
2017-12-09 21:07:33 +00:00
["/:number/:keyword" {:name ::user
:parameters {:path {:number int?
:keyword keyword?}}}]]]
{:compile coercion/compile-request-coercers})]
(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 "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")]
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))))))))
2017-11-26 19:51:43 +00:00