From 6bf4335f8413b5efb4ba807fd98dadde6328151b Mon Sep 17 00:00:00 2001 From: Toni Vanhala Date: Wed, 16 Oct 2019 13:18:19 +0300 Subject: [PATCH] Add tests for intermediate paths in Ring Added failing tests for resolving intermediate paths with Ring. Handlers are inherited by children, but names are consumed by intermediate routes See Github issue #175 --- test/cljc/reitit/ring_test.cljc | 40 +++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/test/cljc/reitit/ring_test.cljc b/test/cljc/reitit/ring_test.cljc index 7c1d3432..d5b7f319 100644 --- a/test/cljc/reitit/ring_test.cljc +++ b/test/cljc/reitit/ring_test.cljc @@ -1,5 +1,5 @@ (ns reitit.ring-test - (:require [clojure.test :refer [deftest testing is]] + (:require [clojure.test :refer [deftest testing is are]] [clojure.set :as set] [reitit.middleware :as middleware] [reitit.ring :as ring] @@ -116,7 +116,43 @@ (testing "all named routes can be matched" (doseq [name (r/route-names router)] - (is (= name (-> (r/match-by-name router name) :data :name)))))))) + (is (= name (-> (r/match-by-name router name) :data :name))))))) + + (testing "intermediate paths" + (testing "without conflicts" + (let [routes ["/" {:get {:handler handler} :name ::root} + ["foo" {:name ::foo}] ;; Inherits handler from above + ["bar" {:get {:handler handler} :name ::bar} + ["/baz" {:get {:handler handler} :name ::baz}]] + ["bang" {} + ["/bang"]] + ["ping" {:name ::ping} + ["/pong" {:get {:handler handler} :name ::pong}]]] + router (ring/router routes) + match #(r/match-by-path router %)] + (are [path name] + (is (= name (-> (match path) :data :name))) + "/" ::root + "/foo" ::foo + "/bar" ::bar + "/bar/baz" ::baz + "/ping" ::ping + "/ping/pong" ::pong) + (is (nil? (match "/bang"))) + (is (-> (match "/bang/bang") :data :get :handler)))) + + (testing "with conflicts" + (let [routes ["/" {:get {:handler handler} :name ::root} + ["" {:name ::other-root}] ;; Conflicts with ::root path + ["foo" {:name ::foo}] + ["bar" {:get {:handler handler} :name ::bar} + ["/baz" {:get {:handler handler} :name ::baz}]] + ["ping" {:name ::ping} + ["/pong" {:get {:handler handler} :name ::pong}]]]] + (is (thrown-with-msg? + ExceptionInfo + #"Router contains conflicting route paths" + (ring/router routes))))))) (defn wrap-enforce-roles [handler] (fn [{::keys [roles] :as request}]