From ae1a8f7919c199af71ba76bb3b8f68a17995e84d Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Sat, 9 Feb 2019 16:01:06 +0200 Subject: [PATCH] Fail fast with multiple terminators. --- modules/reitit-core/src/reitit/trie.cljc | 11 +++- test/cljc/reitit/core_test.cljc | 69 +++++++++++++----------- 2 files changed, 48 insertions(+), 32 deletions(-) diff --git a/modules/reitit-core/src/reitit/trie.cljc b/modules/reitit-core/src/reitit/trie.cljc index 78d57db1..78b4ca49 100644 --- a/modules/reitit-core/src/reitit/trie.cljc +++ b/modules/reitit-core/src/reitit/trie.cljc @@ -260,8 +260,15 @@ matchers (-> [] (cond-> data (conj (data-matcher data))) (into (for [[p c] children] (static-matcher p (compile c)))) - (into (for [[p c] wilds, end (ends c)] - (wild-matcher (:value p) (first end) (compile (update c :children select-keys [end]))))) + (into + (for [[p c] wilds] + (let [p (:value p) + ends (ends c)] + (if (seq (rest ends)) + (throw + (ex-info + (str "Trie compliation error: wild " p " has two terminators: " ends) {})) + (wild-matcher p (ffirst ends) (compile c)))))) (into (for [[p c] catch-all] (catch-all-matcher (:value p) (:data c)))))] (cond (> (count matchers) 1) (linear-matcher matchers) diff --git a/test/cljc/reitit/core_test.cljc b/test/cljc/reitit/core_test.cljc index 75a88c56..6f266816 100644 --- a/test/cljc/reitit/core_test.cljc +++ b/test/cljc/reitit/core_test.cljc @@ -90,37 +90,46 @@ (is (= ::wild (by-path "/olipa/kerran/avaruus/vaan/ei/toista/kertaa"))))) (testing "bracket-params" - (let [router (r/router - [["/{abba}" ::abba] - ["/abba/1" ::abba2] - ["/{jabba}/2" ::jabba2] - ["/{abba}/{dabba}/doo" ::doo] - ["/abba/dabba/boo/baa" ::baa] - ["/abba/{dabba}/boo" ::boo] - ["/{a/jabba}/{a.b/dabba}/{a.b.c/doo}/{a.b.c.d/daa}/{*foo/bar}" ::wild] - ["/files/file-{name}.html" ::html] - ["/files/file-{name}.json" ::json] - ["/files/file-{name}-large.json" ::large]] - {:router r}) - by-path #(-> router (r/match-by-path %) ((juxt (comp :name :data) :path-params)))] - (is (= [::abba {:abba "abba"}] (by-path "/abba"))) - (is (= [::abba2 {}] (by-path "/abba/1"))) - (is (= [::jabba2 {:jabba "abba"}] (by-path "/abba/2"))) - (is (= [::doo {:abba "abba", :dabba "1"}] (by-path "/abba/1/doo"))) - (is (= [::boo {:dabba "1"}] (by-path "/abba/1/boo"))) - (is (= [::baa {}] (by-path "/abba/dabba/boo/baa"))) - (is (= [::boo {:dabba "dabba"}] (by-path "/abba/dabba/boo"))) - (is (= [::wild {:a/jabba "olipa" - :a.b/dabba "kerran" - :a.b.c/doo "avaruus" - :a.b.c.d/daa "vaan" - :foo/bar "ei/toista/kertaa"}] - (by-path "/olipa/kerran/avaruus/vaan/ei/toista/kertaa"))) - (is (= [::html {:name "10"}] (by-path "/files/file-10.html"))) - (is (= [::json {:name "10"}] (by-path "/files/file-10.json"))) - ;(is (= [::large {:name "10"}] (by-path "/files/file-10-large.json"))) + (testing "successful" + (let [router (r/router + [["/{abba}" ::abba] + ["/abba/1" ::abba2] + ["/{jabba}/2" ::jabba2] + ["/{abba}/{dabba}/doo" ::doo] + ["/abba/dabba/boo/baa" ::baa] + ["/abba/{dabba}/boo" ::boo] + ["/{a/jabba}/{a.b/dabba}/{a.b.c/doo}/{a.b.c.d/daa}/{*foo/bar}" ::wild] + ["/files/file-{name}.html" ::html] + ["/files/file-{name}.json" ::json]] + {:router r}) + by-path #(-> router (r/match-by-path %) ((juxt (comp :name :data) :path-params)))] + (is (= [::abba {:abba "abba"}] (by-path "/abba"))) + (is (= [::abba2 {}] (by-path "/abba/1"))) + (is (= [::jabba2 {:jabba "abba"}] (by-path "/abba/2"))) + (is (= [::doo {:abba "abba", :dabba "1"}] (by-path "/abba/1/doo"))) + (is (= [::boo {:dabba "1"}] (by-path "/abba/1/boo"))) + (is (= [::baa {}] (by-path "/abba/dabba/boo/baa"))) + (is (= [::boo {:dabba "dabba"}] (by-path "/abba/dabba/boo"))) + (is (= [::wild {:a/jabba "olipa" + :a.b/dabba "kerran" + :a.b.c/doo "avaruus" + :a.b.c.d/daa "vaan" + :foo/bar "ei/toista/kertaa"}] + (by-path "/olipa/kerran/avaruus/vaan/ei/toista/kertaa"))) + (is (= [::html {:name "10"}] (by-path "/files/file-10.html"))))) - )) + (testing "invalid syntax fails fast" + (testing "unbalanced brackets" + (is (thrown-with-msg? + ExceptionInfo + #"^Unbalanced brackets" + (r/router ["/kikka/{kukka"])))) + (testing "multiple terminators" + (is (thrown-with-msg? + ExceptionInfo + #"^Trie compliation error: wild :kukka has two terminators" + (r/router [["/{kukka}.json"] + ["/{kukka}-json"]])))))) (testing "empty path segments" (let [router (r/router