From d8bbc050c528f0ee358fdf16b564e0a0abaaf24a Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Sun, 1 Sep 2013 02:26:15 +1000 Subject: Clean it up a bit - it's mostly macros now, unfortunately --- test/reverse_routing/core_test.clj | 50 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 test/reverse_routing/core_test.clj (limited to 'test') diff --git a/test/reverse_routing/core_test.clj b/test/reverse_routing/core_test.clj new file mode 100644 index 0000000..d611dea --- /dev/null +++ b/test/reverse_routing/core_test.clj @@ -0,0 +1,50 @@ +(ns reverse-routing.core-test + (:use clojure.test + reverse-routing.core + [compojure.core :only [GET]])) + +(defn make-request [handler uri] + (-> {:request-method :get + :scheme :http + :uri uri + :remote-addr "127.0.0.1" + :server-port 8080 + :server-name "something"} + handler + :body)) + +(deftest test-basic-route + (let [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))) + 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"))))) + +(deftest test-contest-routes + (let [handler (-> (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)] + (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"))))) -- cgit v1.2.3