reitit/test/cljc/reitit/ring_spec_test.cljc

135 lines
4.7 KiB
Text
Raw Normal View History

2017-12-26 20:41:17 +00:00
(ns reitit.ring-spec-test
(:require [clojure.test :refer [deftest testing is]]
[reitit.ring :as ring]
[reitit.ring.spec :as rrs]
[reitit.ring.coercion :as rrc]
[reitit.coercion.spec]
[clojure.spec.alpha :as s]
2017-12-29 09:41:12 +00:00
[reitit.core :as r])
2017-12-26 20:41:17 +00:00
#?(:clj
(:import (clojure.lang ExceptionInfo))))
(s/def ::role #{:admin :user})
(s/def ::roles (s/and (s/coll-of ::role :into #{}) set?))
2017-12-26 20:41:17 +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:41:17 +00:00
(testing "with default spec validates :name, :handler and :middleware"
(is (thrown-with-msg?
ExceptionInfo
2019-04-28 14:36:43 +00:00
#"Invalid route data"
2017-12-26 20:41:17 +00:00
(ring/router
["/api" {:handler "identity"}]
{:validate rrs/validate})))
2017-12-26 20:41:17 +00:00
(is (thrown-with-msg?
ExceptionInfo
2019-04-28 14:36:43 +00:00
#"Invalid route data"
2017-12-26 20:41:17 +00:00
(ring/router
["/api" {:handler identity
:name "kikka"}]
{:validate rrs/validate}))))
2017-12-26 20:41:17 +00:00
(testing "all endpoints are validated"
(is (thrown-with-msg?
ExceptionInfo
2019-04-28 14:36:43 +00:00
#"Invalid route data"
2017-12-26 20:41:17 +00:00
(ring/router
["/api" {:patch {:handler "identity"}}]
{:validate rrs/validate}))))
2017-12-26 20:41:17 +00:00
(testing "spec can be overridden"
2017-12-31 09:34:13 +00:00
(is (r/router?
(ring/router
["/api" {:handler "identity"}]
{:spec (s/spec any?)
:validate rrs/validate})))
(testing "predicates are not allowed"
(is (thrown-with-msg?
ExceptionInfo
#":reitit.ring.spec/invalid-specs"
(ring/router
["/api" {:handler "identity"}]
{:spec any?
:validate rrs/validate})))))
(testing "middleware can contribute to specs"
2017-12-31 09:34:13 +00:00
(is (r/router?
(ring/router
["/api" {:get {:handler identity
:roles #{:admin}}}]
{:validate rrs/validate
2017-12-31 09:34:13 +00:00
:data {:middleware [{:spec (s/keys :opt-un [::roles])
:wrap (fn [handler]
(fn [request]
(handler request)))}]}})))
(is (thrown-with-msg?
ExceptionInfo
2019-04-28 14:36:43 +00:00
#"Invalid route data"
(ring/router
["/api" {:get {:handler identity
:roles #{:adminz}}}]
{:validate rrs/validate
:data {:middleware [{:spec (s/keys :opt-un [::roles])
:wrap (fn [handler]
(fn [request]
(handler request)))}]}}))))
(testing "middleware cannot be a list"
(is (thrown-with-msg?
ExceptionInfo
#":reitit.ring.spec/invalid-specs"
(ring/router
["/api" {:handler identity
:middleware '()}]
{:validate rrs/validate})))))
(deftest coercion-spec-test
(is (r/router?
(ring/router
["/api"
["/plus/:e"
{:get {:parameters {:query {:a string?}
:body {:b string?}
:form {:c string?}
:header {:d string?}
:path {:e string?}}
2018-06-24 16:28:50 +00:00
:responses {200 {:body {:total pos-int?}}
400 {:description "fail"}
500 {}}
:handler identity}}]]
{:data {:middleware [rrc/coerce-exceptions-middleware
rrc/coerce-request-middleware
rrc/coerce-response-middleware]
:coercion reitit.coercion.spec/coercion}
:validate rrs/validate})))
2018-10-16 13:57:33 +00:00
(is (r/router?
(ring/router
["/api"
["/plus/:e"
2018-10-16 17:25:43 +00:00
{:get {:parameters {:query (s/keys)}
:handler identity}}]]
{:data {:middleware [rrc/coerce-exceptions-middleware
rrc/coerce-request-middleware
rrc/coerce-response-middleware]
:coercion reitit.coercion.spec/coercion}
:validate rrs/validate})))
(is (thrown-with-msg?
ExceptionInfo
2019-04-28 14:36:43 +00:00
#"Invalid route data"
(ring/router
["/api"
["/plus/:e"
{:get {:responses {"200" {}}
:handler identity}}]]
{:data {:middleware [rrc/coerce-exceptions-middleware
rrc/coerce-request-middleware
rrc/coerce-response-middleware]
:coercion reitit.coercion.spec/coercion}
:validate rrs/validate}))))