summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/reverse_routing/core_test.clj50
1 files changed, 50 insertions, 0 deletions
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")))))