This commit is contained in:
Tommi Reiman 2017-12-31 11:34:13 +02:00
parent 708fa24590
commit 07bcd2ea59
5 changed files with 29 additions and 31 deletions

View file

@ -1,7 +1,6 @@
(ns reitit.spec (ns reitit.spec
(:require [clojure.spec.alpha :as s] (:require [clojure.spec.alpha :as s]
[clojure.spec.gen.alpha :as gen] [clojure.spec.gen.alpha :as gen]
[clojure.string :as str]
[reitit.core :as reitit])) [reitit.core :as reitit]))
;; ;;

View file

@ -48,4 +48,3 @@
(is (= nil (coercion/coerce! m)))) (is (= nil (coercion/coerce! m))))
(let [m (r/match-by-path r "/none/kikka/abba")] (let [m (r/match-by-path r "/none/kikka/abba")]
(is (= nil (coercion/coerce! m)))))))) (is (= nil (coercion/coerce! m))))))))

View file

@ -16,7 +16,7 @@
(is (= name (r/router-name router))) (is (= name (r/router-name router)))
(is (= [["/api/ipa/:size" {:name ::beer} nil]] (is (= [["/api/ipa/:size" {:name ::beer} nil]]
(r/routes router))) (r/routes router)))
(is (= true (map? (r/options router)))) (is (map? (r/options router)))
(is (= (r/map->Match (is (= (r/map->Match
{:template "/api/ipa/:size" {:template "/api/ipa/:size"
:data {:name ::beer} :data {:name ::beer}
@ -39,7 +39,7 @@
:required #{:size} :required #{:size}
:params nil}) :params nil})
(r/match-by-name router ::beer))) (r/match-by-name router ::beer)))
(is (= true (r/partial-match? (r/match-by-name router ::beer)))) (is (r/partial-match? (r/match-by-name router ::beer)))
(is (thrown-with-msg? (is (thrown-with-msg?
ExceptionInfo ExceptionInfo
#"^missing path-params for route /api/ipa/:size -> \#\{:size\}$" #"^missing path-params for route /api/ipa/:size -> \#\{:size\}$"
@ -72,7 +72,7 @@
(is (= name (r/router-name router))) (is (= name (r/router-name router)))
(is (= [["/api/ipa/large" {:name ::beer} nil]] (is (= [["/api/ipa/large" {:name ::beer} nil]]
(r/routes router))) (r/routes router)))
(is (= true (map? (r/options router)))) (is (map? (r/options router)))
(is (= (r/map->Match (is (= (r/map->Match
{:template "/api/ipa/large" {:template "/api/ipa/large"
:data {:name ::beer} :data {:name ::beer}

View file

@ -12,9 +12,9 @@
(deftest route-data-validation-test (deftest route-data-validation-test
(testing "validation is turned off by default" (testing "validation is turned off by default"
(is (true? (r/router? (is (r/router?
(r/router (r/router
["/api" {:handler "identity"}]))))) ["/api" {:handler "identity"}]))))
(testing "with default spec validates :name, :handler and :middleware" (testing "with default spec validates :name, :handler and :middleware"
(is (thrown-with-msg? (is (thrown-with-msg?
@ -40,11 +40,11 @@
{:validate rrs/validate-spec!})))) {:validate rrs/validate-spec!}))))
(testing "spec can be overridden" (testing "spec can be overridden"
(is (true? (r/router? (is (r/router?
(ring/router (ring/router
["/api" {:handler "identity"}] ["/api" {:handler "identity"}]
{:spec (s/spec any?) {:spec (s/spec any?)
:validate rrs/validate-spec!})))) :validate rrs/validate-spec!})))
(testing "predicates are not allowed" (testing "predicates are not allowed"
(is (thrown-with-msg? (is (thrown-with-msg?
@ -56,15 +56,15 @@
:validate rrs/validate-spec!}))))) :validate rrs/validate-spec!})))))
(testing "middleware can contribute to specs" (testing "middleware can contribute to specs"
(is (true? (r/router? (is (r/router?
(ring/router (ring/router
["/api" {:get {:handler identity ["/api" {:get {:handler identity
:roles #{:admin}}}] :roles #{:admin}}}]
{:validate rrs/validate-spec! {:validate rrs/validate-spec!
:data {:middleware [{:spec (s/keys :opt-un [::roles]) :data {:middleware [{:spec (s/keys :opt-un [::roles])
:wrap (fn [handler] :wrap (fn [handler]
(fn [request] (fn [request]
(handler request)))}]}})))) (handler request)))}]}})))
(is (thrown-with-msg? (is (thrown-with-msg?
ExceptionInfo ExceptionInfo
#"Invalid route data" #"Invalid route data"

View file

@ -16,7 +16,7 @@
(testing "route-data" (testing "route-data"
(are [data] (are [data]
(is (= true (r/router? (r/router data)))) (is (r/router? (r/router data)))
["/api" {}] ["/api" {}]
@ -45,12 +45,12 @@
["/ipa"]]))) ["/ipa"]])))
(testing "routes conform to spec (can't spec protocol functions)" (testing "routes conform to spec (can't spec protocol functions)"
(is (= true (s/valid? ::rs/routes (r/routes (r/router ["/ping"])))))) (is (s/valid? ::rs/routes (r/routes (r/router ["/ping"])))))
(testing "options" (testing "options"
(are [opts] (are [opts]
(is (= true (r/router? (r/router ["/api"] opts)))) (is (r/router? (r/router ["/api"] opts)))
{:path "/"} {:path "/"}
{:data {}} {:data {}}
@ -78,8 +78,8 @@
(deftest route-data-validation-test (deftest route-data-validation-test
(testing "validation is turned off by default" (testing "validation is turned off by default"
(is (true? (r/router? (r/router (is (r/router? (r/router
["/api" {:handler "identity"}]))))) ["/api" {:handler "identity"}]))))
(testing "with default spec validates :name and :handler" (testing "with default spec validates :name and :handler"
(is (thrown-with-msg? (is (thrown-with-msg?
@ -96,7 +96,7 @@
{:validate rs/validate-spec!})))) {:validate rs/validate-spec!}))))
(testing "spec can be overridden" (testing "spec can be overridden"
(is (true? (r/router? (r/router (is (r/router? (r/router
["/api" {:handler "identity"}] ["/api" {:handler "identity"}]
{:spec any? {:spec any?
:validate rs/validate-spec!})))))) :validate rs/validate-spec!})))))