2017-09-01 08:32:08 +00:00
|
|
|
(ns reitit.spec-test
|
|
|
|
|
(:require [clojure.test :refer [deftest testing is are]]
|
2017-09-08 08:25:53 +00:00
|
|
|
[#?(:clj clojure.spec.test.alpha :cljs cljs.spec.test.alpha) :as stest]
|
2017-09-01 08:32:08 +00:00
|
|
|
[clojure.spec.alpha :as s]
|
2017-10-02 05:05:42 +00:00
|
|
|
[reitit.core :as r]
|
2017-12-26 20:40:34 +00:00
|
|
|
[reitit.spec :as rs]
|
|
|
|
|
[expound.alpha :as e])
|
2017-09-01 08:32:08 +00:00
|
|
|
#?(:clj
|
|
|
|
|
(:import (clojure.lang ExceptionInfo))))
|
|
|
|
|
|
2017-10-02 05:05:42 +00:00
|
|
|
(stest/instrument)
|
2017-09-01 08:32:08 +00:00
|
|
|
|
|
|
|
|
(deftest router-spec-test
|
|
|
|
|
|
|
|
|
|
(testing "router"
|
|
|
|
|
|
|
|
|
|
(testing "route-data"
|
|
|
|
|
(are [data]
|
2017-12-31 09:34:13 +00:00
|
|
|
(is (r/router? (r/router data)))
|
2017-09-01 08:32:08 +00:00
|
|
|
|
|
|
|
|
["/api" {}]
|
|
|
|
|
|
2017-12-27 19:37:04 +00:00
|
|
|
["api" {}]
|
|
|
|
|
|
2017-09-01 08:32:08 +00:00
|
|
|
[["/api" {}]]
|
|
|
|
|
|
|
|
|
|
["/api"
|
|
|
|
|
["/ipa" ::ipa]
|
|
|
|
|
["/tea"
|
|
|
|
|
["/room"]]])
|
|
|
|
|
|
|
|
|
|
(testing "with invalid routes"
|
|
|
|
|
(are [data]
|
|
|
|
|
(is (thrown-with-msg?
|
|
|
|
|
ExceptionInfo
|
|
|
|
|
#"Call to #'reitit.core/router did not conform to spec"
|
2017-10-02 05:05:42 +00:00
|
|
|
(r/router
|
2017-09-01 08:32:08 +00:00
|
|
|
data)))
|
|
|
|
|
|
|
|
|
|
;; path
|
|
|
|
|
[:invalid {}]
|
|
|
|
|
|
2017-11-18 10:47:16 +00:00
|
|
|
;; vector data
|
2017-09-01 08:32:08 +00:00
|
|
|
["/api" []
|
|
|
|
|
["/ipa"]])))
|
|
|
|
|
|
2017-09-08 05:29:31 +00:00
|
|
|
(testing "routes conform to spec (can't spec protocol functions)"
|
2017-12-31 09:34:13 +00:00
|
|
|
(is (s/valid? ::rs/routes (r/routes (r/router ["/ping"])))))
|
2017-09-08 05:29:31 +00:00
|
|
|
|
2017-09-01 08:32:08 +00:00
|
|
|
(testing "options"
|
|
|
|
|
|
2017-09-03 11:55:58 +00:00
|
|
|
(are [opts]
|
2017-12-31 09:34:13 +00:00
|
|
|
(is (r/router? (r/router ["/api"] opts)))
|
2017-09-03 11:55:58 +00:00
|
|
|
|
|
|
|
|
{:path "/"}
|
2017-11-18 10:47:16 +00:00
|
|
|
{:data {}}
|
2017-09-03 11:55:58 +00:00
|
|
|
{:expand (fn [_ _] {})}
|
|
|
|
|
{:coerce (fn [route _] route)}
|
|
|
|
|
{:compile (fn [_ _])}
|
|
|
|
|
{:conflicts (fn [_])}
|
2017-10-02 05:05:42 +00:00
|
|
|
{:router r/linear-router})
|
2017-09-03 11:55:58 +00:00
|
|
|
|
|
|
|
|
(are [opts]
|
|
|
|
|
(is (thrown-with-msg?
|
|
|
|
|
ExceptionInfo
|
|
|
|
|
#"Call to #'reitit.core/router did not conform to spec"
|
2017-10-02 05:05:42 +00:00
|
|
|
(r/router
|
2017-09-03 11:55:58 +00:00
|
|
|
["/api"] opts)))
|
|
|
|
|
|
2017-12-27 19:37:04 +00:00
|
|
|
{:path :api}
|
2017-09-03 11:55:58 +00:00
|
|
|
{:path nil}
|
2017-11-18 10:47:16 +00:00
|
|
|
{:data nil}
|
2017-09-03 11:55:58 +00:00
|
|
|
{:expand nil}
|
|
|
|
|
{:coerce nil}
|
|
|
|
|
{:compile nil}
|
|
|
|
|
{:conflicts nil}
|
|
|
|
|
{:router nil}))))
|
2017-12-26 20:40:34 +00:00
|
|
|
|
|
|
|
|
(deftest route-data-validation-test
|
|
|
|
|
(testing "validation is turned off by default"
|
2017-12-31 09:34:13 +00:00
|
|
|
(is (r/router? (r/router
|
|
|
|
|
["/api" {:handler "identity"}]))))
|
2017-12-26 20:40:34 +00:00
|
|
|
|
|
|
|
|
(testing "with default spec validates :name and :handler"
|
|
|
|
|
(is (thrown-with-msg?
|
|
|
|
|
ExceptionInfo
|
|
|
|
|
#"Invalid route data"
|
|
|
|
|
(r/router
|
|
|
|
|
["/api" {:handler "identity"}]
|
|
|
|
|
{:validate rs/validate-spec!})))
|
|
|
|
|
(is (thrown-with-msg?
|
|
|
|
|
ExceptionInfo
|
|
|
|
|
#"Invalid route data"
|
|
|
|
|
(r/router
|
|
|
|
|
["/api" {:name "kikka"}]
|
|
|
|
|
{:validate rs/validate-spec!}))))
|
|
|
|
|
|
|
|
|
|
(testing "spec can be overridden"
|
2017-12-31 09:34:13 +00:00
|
|
|
(is (r/router? (r/router
|
|
|
|
|
["/api" {:handler "identity"}]
|
|
|
|
|
{:spec any?
|
|
|
|
|
:validate rs/validate-spec!})))))
|