diff options
-rw-r--r-- | src/reverse_routing/core.clj | 2 | ||||
-rw-r--r-- | test/reverse_routing/core_test.clj | 34 |
2 files changed, 24 insertions, 12 deletions
diff --git a/src/reverse_routing/core.clj b/src/reverse_routing/core.clj index 2907fb4..4299eba 100644 --- a/src/reverse_routing/core.clj +++ b/src/reverse_routing/core.clj @@ -8,7 +8,7 @@ (defn wrap-reverse-routing [handler & {:keys [root] :or {:root ""}}] (fn [request] - (binding [*lookup-route* (->> handler meta ::lookup) + (binding [*lookup-route* (->> handler deref-if-var meta ::lookup) *root* root] (handler request)))) 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")))))) |