reitit/test/cljc/reitit/ring_spec_test.cljc

135 lines
4.6 KiB
Text
Raw Normal View History

2017-12-26 20:41:17 +00:00
(ns reitit.ring-spec-test
2022-02-14 14:59:20 +00:00
(:require [clojure.spec.alpha :as s]
[clojure.test :refer [deftest is testing]]
[reitit.coercion.spec]
[reitit.core :as r]
[reitit.ring :as ring]
[reitit.ring.coercion :as rrc]
[reitit.ring.spec :as rrs])
2017-12-26 20:41:17 +00:00
#?(:clj
2022-02-14 14:59:20 +00:00
(:import (clojure.lang ExceptionInfo))))
2017-12-26 20:41:17 +00:00
(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?
2022-02-12 20:34:26 +00:00
(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?
2022-02-12 20:34:26 +00:00
ExceptionInfo
#"Invalid route data"
(ring/router
["/api" {:handler "identity"}]
{:validate rrs/validate})))
2017-12-26 20:41:17 +00:00
(is (thrown-with-msg?
2022-02-12 20:34:26 +00:00
ExceptionInfo
#"Invalid route data"
(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?
2022-02-12 20:34:26 +00:00
ExceptionInfo
#"Invalid route data"
(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?
2022-02-12 20:34:26 +00:00
(ring/router
["/api" {:handler "identity"}]
{:spec (s/spec any?)
:validate rrs/validate})))
(testing "predicates are not allowed"
(is (thrown-with-msg?
2022-02-12 20:34:26 +00:00
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?
2022-02-12 20:34:26 +00:00
(ring/router
["/api" {:get {:handler identity
:roles #{:admin}}}]
{:validate rrs/validate
:data {:middleware [{:spec (s/keys :opt-un [::roles])
:wrap (fn [handler]
(fn [request]
(handler request)))}]}})))
(is (thrown-with-msg?
2022-02-12 20:34:26 +00:00
ExceptionInfo
#"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
#"Invalid route data"
(ring/router
2023-01-10 06:05:42 +00:00
["/api" {:handler identity
:middleware '()}]
{:validate rrs/validate})))))
(deftest coercion-spec-test
(is (r/router?
2022-02-12 20:34:26 +00:00
(ring/router
["/api"
["/plus/:e"
{:get {:parameters {:query {:a string?}
:body {:b string?}
:form {:c string?}
:header {:d string?}
:path {:e string?}}
: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?
2022-02-12 20:34:26 +00:00
(ring/router
["/api"
["/plus/:e"
{: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?
2022-02-12 20:34:26 +00:00
ExceptionInfo
#"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}))))