summaryrefslogtreecommitdiff
path: root/test/reverse_routing
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@clearboxsystems.com.au>2013-09-02 00:29:52 +1000
committerCarlo Zancanaro <carlo@clearboxsystems.com.au>2013-09-02 00:29:52 +1000
commitc27eaaeef4ec415e8e5645cd5e477643aec56d2c (patch)
treede8825c8f56fb8fa012c6c28e6a9fa34dd338d51 /test/reverse_routing
parentd8bbc050c528f0ee358fdf16b564e0a0abaaf24a (diff)
Rewrite to handle dynamic vars with sub-routes - resolve routes ring-style
Diffstat (limited to 'test/reverse_routing')
-rw-r--r--test/reverse_routing/core_test.clj46
1 files changed, 45 insertions, 1 deletions
diff --git a/test/reverse_routing/core_test.clj b/test/reverse_routing/core_test.clj
index d611dea..5dc50ba 100644
--- a/test/reverse_routing/core_test.clj
+++ b/test/reverse_routing/core_test.clj
@@ -4,6 +4,8 @@
[compojure.core :only [GET]]))
(defn make-request [handler uri]
+ ;; Technically most of this map is required by the ring spec
+ ;; I'm fairly sure we could get away with not having most of it
(-> {:request-method :get
:scheme :http
:uri uri
@@ -25,7 +27,7 @@
(is (thrown? java.lang.AssertionError (make-request handler "/fail1")))
(is (thrown? java.lang.AssertionError (make-request handler "/fail2")))))
-(deftest test-contest-routes
+(deftest test-context-routes
(let [handler (-> (routes
(register-route :user
(GET "/user/" [id] (str "user list")))
@@ -48,3 +50,45 @@
(is (thrown? java.lang.AssertionError (make-request handler "/fail1")))
(is (thrown? java.lang.AssertionError (make-request handler "/fail2")))
(is (thrown? java.lang.AssertionError (make-request handler "/fail3")))))
+
+(deftest test-context-in-context-routes
+ (let [handler (-> (routes
+ (context "/user" []
+ (register-route :user
+ (GET "/" [id] (str "user list")))
+ (context "/:id" []
+ (register-route :user
+ (GET "/" [id] (str "user " id)))
+ (register-route :edit-user
+ (GET "/edit" [id] (str "edit user " id)))))
+ (GET "/succeed1" [] (url-for :user))
+ (GET "/succeed2" [] (url-for :user 10))
+ (GET "/succeed3" [] (url-for :edit-user 10))
+ (GET "/fail1" [] (url-for :user 10 20))
+ (GET "/fail2" [] (url-for :edit-user))
+ (GET "/fail3" [] (url-for :edit-user 10 20)))
+ wrap-reverse-routing)]
+ (is (= (make-request handler "/succeed1") "/user/"))
+ (is (= (make-request handler "/succeed2") "/user/10/"))
+ (is (= (make-request handler "/succeed3") "/user/10/edit"))
+
+ (is (thrown? java.lang.AssertionError (make-request handler "/fail1")))
+ (is (thrown? java.lang.AssertionError (make-request handler "/fail2")))
+ (is (thrown? java.lang.AssertionError (make-request handler "/fail3")))))
+
+
+(def ^:dynamic *subroutes* (register-route :user
+ (GET "/user" [] (str "user"))))
+(deftest test-with-rebinding-vars
+ (let [handler (-> (routes
+ #'*subroutes*
+ (GET "/succeed" [] (url-for :user))
+ (GET "/fail" [] (url-for :user 10)))
+ wrap-reverse-routing)]
+ (is (= (make-request handler "/succeed") "/user"))
+ (is (thrown? java.lang.AssertionError (make-request handler "/fail")))
+
+ (binding [*subroutes* (register-route :user
+ (GET "/not-user" [] (str "user")))]
+ (is (= (make-request handler "/succeed") "/not-user"))
+ (is (thrown? java.lang.AssertionError (make-request handler "/fail"))))))