From 523e00ccda6559c57cce87e488ecfcb722d1fc19 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Mon, 2 Sep 2013 00:56:29 +1000 Subject: Handle the case of the top-level route being a var --- test/reverse_routing/core_test.clj | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) (limited to 'test/reverse_routing') diff --git a/test/reverse_routing/core_test.clj b/test/reverse_routing/core_test.clj index 8752de9..ef2fa93 100644 --- a/test/reverse_routing/core_test.clj +++ b/test/reverse_routing/core_test.clj @@ -77,18 +77,30 @@ (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"))) + (with-local-vars [subroutes (register-route :user + (GET "/user" [] (str "user")))] + (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")))] + (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")))))) + +(deftest top-level-is-a-var + (with-local-vars [bare-handler (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)))] + (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")))))) -- cgit v1.2.3