summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-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"))))))