reitit/test/cljc/reitit/spec_test.cljc

137 lines
3.5 KiB
Text
Raw Normal View History

2017-09-01 08:32:08 +00:00
(ns reitit.spec-test
2018-06-04 07:30:48 +00:00
(:require [clojure.test :refer [deftest testing is are use-fixtures]]
[#?(: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]
2018-06-04 07:30:48 +00:00
[reitit.spec :as rs])
2017-09-01 08:32:08 +00:00
#?(:clj
(:import (clojure.lang ExceptionInfo))))
2018-06-04 07:30:48 +00:00
(defn instrument-all [f]
(try
(stest/instrument)
(f)
(finally
(stest/unstrument))))
(use-fixtures :each instrument-all)
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 {}]
2018-06-04 07:30:48 +00:00
;; nested path
["/api"
[:ipa]])))
2017-09-01 08:32:08 +00:00
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"
(are [opts]
2017-12-31 09:34:13 +00:00
(is (r/router? (r/router ["/api"] opts)))
{:path "/"}
2017-11-18 10:47:16 +00:00
{:data {}}
{:expand (fn [_ _] {})}
{:coerce (fn [route _] route)}
{:compile (fn [_ _])}
{:conflicts (fn [_])}
2017-10-02 05:05:42 +00:00
{:router r/linear-router})
2018-06-05 05:49:30 +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
["/api"] opts)))
2017-12-27 19:37:04 +00:00
{:path :api}
{:path nil}
2017-11-18 10:47:16 +00:00
{:data nil}
{:expand nil}
{:coerce nil}
{:compile nil}
2018-07-21 05:49:20 +00:00
{:conflicts "invalid"}
{:router nil}))))
(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"}]))))
(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!})))))
(deftest parameters-test
(is (s/valid?
::rs/parameters
{:parameters {:query {:a string?}
:body {:b string?}
:form {:c string?}
:header {:d string?}
:path {:e string?}}}))
2018-10-16 13:57:33 +00:00
(is (s/valid?
::rs/parameters
{:parameters {:header {"d" string?}}}))
(is (s/valid?
::rs/responses
2018-01-01 20:10:49 +00:00
{:responses {200 {:description "ok", :body string?}
400 {:description "fail"}
2018-01-01 20:10:49 +00:00
500 {:body string?}
:default {}}}))
(is (not (s/valid?
::rs/responses
2018-01-01 20:10:49 +00:00
{:responses {"200" {:description "ok", :body string?}}})))
(is (not (s/valid?
::rs/responses
2018-01-01 20:10:49 +00:00
{:responses {200 {:description :ok, :body string?}}}))))