2018-06-08 13:00:49 +00:00
|
|
|
(ns reitit.frontend.core-test
|
|
|
|
|
(:require [clojure.test :refer [deftest testing is are]]
|
|
|
|
|
[reitit.core :as r]
|
|
|
|
|
[reitit.frontend :as rf]
|
2018-07-11 07:26:53 +00:00
|
|
|
[reitit.coercion :as rc]
|
2018-06-08 13:00:49 +00:00
|
|
|
[schema.core :as s]
|
2018-07-11 09:55:16 +00:00
|
|
|
[reitit.coercion.schema :as rsc]
|
|
|
|
|
[reitit.frontend.test-utils :refer [capture-console]]))
|
2018-06-08 13:00:49 +00:00
|
|
|
|
2018-07-11 06:52:35 +00:00
|
|
|
(defn m [x]
|
|
|
|
|
(assoc x :data nil :result nil))
|
|
|
|
|
|
2018-06-08 13:00:49 +00:00
|
|
|
(deftest match-by-path-test
|
|
|
|
|
(testing "simple"
|
|
|
|
|
(let [router (r/router ["/"
|
|
|
|
|
["" ::frontpage]
|
|
|
|
|
["foo" ::foo]
|
|
|
|
|
["bar" ::bar]])]
|
2018-06-12 10:49:24 +00:00
|
|
|
(is (= (r/map->Match
|
|
|
|
|
{:template "/"
|
|
|
|
|
:data {:name ::frontpage}
|
|
|
|
|
:path-params {}
|
|
|
|
|
:path "/"
|
|
|
|
|
:parameters {:query {}
|
|
|
|
|
:path {}}})
|
2018-06-08 13:00:49 +00:00
|
|
|
(rf/match-by-path router "/")))
|
2018-07-11 06:52:35 +00:00
|
|
|
|
|
|
|
|
(is (= "/"
|
|
|
|
|
(r/match->path (rf/match-by-name router ::frontpage))))
|
|
|
|
|
|
2018-06-12 10:49:24 +00:00
|
|
|
(is (= (r/map->Match
|
|
|
|
|
{:template "/foo"
|
|
|
|
|
:data {:name ::foo}
|
|
|
|
|
:path-params {}
|
|
|
|
|
:path "/foo"
|
|
|
|
|
:parameters {:query {}
|
|
|
|
|
:path {}}})
|
2018-07-11 06:52:35 +00:00
|
|
|
(rf/match-by-path router "/foo")))
|
|
|
|
|
|
|
|
|
|
(is (= "/foo"
|
2018-07-11 09:55:16 +00:00
|
|
|
(r/match->path (rf/match-by-name router ::foo))))
|
|
|
|
|
|
2018-08-22 07:47:04 +00:00
|
|
|
(testing "console warning about missing route"
|
|
|
|
|
(is (= [{:type :warn
|
|
|
|
|
:message ["missing route" ::asd]}]
|
|
|
|
|
(:messages
|
|
|
|
|
(capture-console
|
|
|
|
|
(fn []
|
|
|
|
|
(rf/match-by-name! router ::asd)))))))))
|
2018-06-08 13:00:49 +00:00
|
|
|
|
|
|
|
|
(testing "schema coercion"
|
|
|
|
|
(let [router (r/router ["/"
|
2018-06-12 10:49:24 +00:00
|
|
|
[":id" {:name ::foo
|
|
|
|
|
:parameters {:path {:id s/Int}
|
|
|
|
|
:query {(s/optional-key :mode) s/Keyword}}}]]
|
2018-07-11 07:26:53 +00:00
|
|
|
{:compile rc/compile-request-coercers
|
|
|
|
|
:data {:coercion rsc/coercion}})]
|
2018-08-22 07:47:04 +00:00
|
|
|
|
2018-06-12 10:49:24 +00:00
|
|
|
(is (= (r/map->Match
|
|
|
|
|
{:template "/:id"
|
|
|
|
|
:path-params {:id "5"}
|
|
|
|
|
:path "/5"
|
|
|
|
|
:parameters {:query {}
|
|
|
|
|
:path {:id 5}}})
|
2018-07-11 06:52:35 +00:00
|
|
|
(m (rf/match-by-path router "/5"))))
|
|
|
|
|
|
|
|
|
|
(is (= "/5"
|
|
|
|
|
(r/match->path (rf/match-by-name router ::foo {:id 5}))))
|
|
|
|
|
|
2018-08-22 07:47:04 +00:00
|
|
|
(testing "query param is read"
|
|
|
|
|
(is (= (r/map->Match
|
|
|
|
|
{:template "/:id"
|
|
|
|
|
:path-params {:id "5"}
|
|
|
|
|
;; Note: query not included in path
|
|
|
|
|
:path "/5"
|
|
|
|
|
:parameters {:path {:id 5}
|
|
|
|
|
:query {:mode :foo}}})
|
|
|
|
|
(m (rf/match-by-path router "/5?mode=foo"))))
|
|
|
|
|
|
|
|
|
|
(is (= "/5?mode=foo"
|
|
|
|
|
(r/match->path (rf/match-by-name router ::foo {:id 5}) {:mode :foo}))))
|
2018-07-11 06:52:35 +00:00
|
|
|
|
2018-08-22 07:47:04 +00:00
|
|
|
(testing "fragment is ignored"
|
|
|
|
|
(is (= (r/map->Match
|
|
|
|
|
{:template "/:id"
|
|
|
|
|
:path-params {:id "5"}
|
|
|
|
|
:path "/5"
|
|
|
|
|
:parameters {:path {:id 5}
|
|
|
|
|
:query {:mode :foo}}})
|
|
|
|
|
(m (rf/match-by-path router "/5?mode=foo#fragment")))))
|
2018-07-11 09:55:16 +00:00
|
|
|
|
2018-08-22 07:47:04 +00:00
|
|
|
(testing "console warning about missing params"
|
|
|
|
|
(is (= [{:type :warn
|
|
|
|
|
:message ["missing path-params for route" ::foo
|
|
|
|
|
{:template "/:id"
|
|
|
|
|
:missing #{:id}
|
|
|
|
|
:required #{:id}
|
|
|
|
|
:path-params {}}]}]
|
|
|
|
|
(:messages
|
|
|
|
|
(capture-console
|
|
|
|
|
(fn []
|
|
|
|
|
(rf/match-by-name! router ::foo {}))))))))))
|