mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 00:11:11 +00:00
Fail fast with multiple terminators.
This commit is contained in:
parent
29a54a3262
commit
ae1a8f7919
2 changed files with 48 additions and 32 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue