summaryrefslogtreecommitdiff
path: root/test/reverse_routing
diff options
context:
space:
mode:
Diffstat (limited to 'test/reverse_routing')
-rw-r--r--test/reverse_routing/core_test.clj86
1 files changed, 74 insertions, 12 deletions
diff --git a/test/reverse_routing/core_test.clj b/test/reverse_routing/core_test.clj
index ef2fa93..fb37c94 100644
--- a/test/reverse_routing/core_test.clj
+++ b/test/reverse_routing/core_test.clj
@@ -16,6 +16,21 @@
:body))
(deftest test-basic-route
+ (let [handler (-> (register-route :user
+ (GET "/succeed" [id] (url-for :user 10)))
+ wrap-reverse-routing)]
+ (is (= (make-request handler "/succeed") "/user/10"))))
+
+(-> (routes
+ (register-route :user
+ (GET "/user/:id" [id] (str "user " id)))
+ (GET "/succeed" [] (url-for :user 10))
+ (GET "/fail1" [] (url-for :user))
+ (GET "/fail2" [] (url-for :user 10 20)))
+ wrap-reverse-routing
+ (make-request "/succeed"))
+
+(deftest test-basic-route
(let [handler (-> (routes
(register-route :user
(GET "/user/:id" [id] (str "user " id)))
@@ -24,8 +39,25 @@
(GET "/fail2" [] (url-for :user 10 20)))
wrap-reverse-routing)]
(is (= (make-request handler "/succeed") "/user/10"))
- (is (thrown? java.lang.AssertionError (make-request handler "/fail1")))
- (is (thrown? java.lang.AssertionError (make-request handler "/fail2")))))
+ (is (nil? (make-request handler "/fail1")))
+ (is (nil? (make-request handler "/fail2")))))
+
+(-> (routes
+ (register-route :user
+ (GET "/user/" [id] (str "user list")))
+ (context "/user/: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
+ (make-request "/fail2"))
(deftest test-context-routes
(let [handler (-> (routes
@@ -47,9 +79,9 @@
(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")))))
+ (is (nil? (make-request handler "/fail1")))
+ (is (nil? (make-request handler "/fail2")))
+ (is (nil? (make-request handler "/fail3")))))
(deftest test-context-in-context-routes
(let [handler (-> (routes
@@ -72,9 +104,9 @@
(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")))))
+ (is (nil? (make-request handler "/fail1")))
+ (is (nil? (make-request handler "/fail2")))
+ (is (nil? (make-request handler "/fail3")))))
(deftest test-with-rebinding-vars
@@ -86,12 +118,12 @@
(GET "/fail" [] (url-for :user 10)))
wrap-reverse-routing)]
(is (= (make-request handler "/succeed") "/user"))
- (is (thrown? java.lang.AssertionError (make-request handler "/fail")))
+ (is (nil? (make-request handler "/fail")))
(var-set 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"))))))
+ (is (nil? (make-request handler "/fail"))))))
(deftest top-level-is-a-var
(with-local-vars [bare-handler (routes
@@ -102,5 +134,35 @@
(GET "/fail2" [] (url-for :user 10 20)))]
(let [handler (wrap-reverse-routing bare-handler)]
(is (= (make-request handler "/succeed") "/user/10"))
- (is (thrown? java.lang.AssertionError (make-request handler "/fail1")))
- (is (thrown? java.lang.AssertionError (make-request handler "/fail2"))))))
+ (is (nil? (make-request handler "/fail1")))
+ (is (nil? (make-request handler "/fail2"))))))
+
+
+(deftest context-requiring-not-nil-value
+ (let [handler (-> (routes
+ (context "/:type" [type]
+ (if (not= type nil)
+ (register-route :get-object
+ (GET "/:id" [id]
+ (str [type id])))))
+ (GET "/succeed" [] (url-for :get-object "user" 10))
+ (GET "/fail" [] (url-for :get-object nil 10)))
+ wrap-reverse-routing)]
+ (is (= (make-request handler "/succeed"), "/user/10"))
+ (is (nil? (make-request handler "/fail")))))
+
+(deftest context-with-limited-options
+ (let [valid-type? #{"user"}
+ handler (-> (routes
+ (context "/:type" [type]
+ (when-routes (valid-type? type)
+ (register-route :get-object
+ (GET "/:id" [id]
+ (str [type id])))))
+ (GET "/succeed" [] (url-for :get-object "user" 10))
+ (GET "/fail1" [] (url-for :get-object nil 10))
+ (GET "/fail2" [] (url-for :get-object "person" 10)))
+ wrap-reverse-routing)]
+ (is (= (make-request handler "/succeed"), "/user/10"))
+ (is (nil? (make-request handler "/fail1")))
+ (is (nil? (make-request handler "/fail2")))))