summaryrefslogtreecommitdiff
path: root/test/reverse_routing
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@clearboxsystems.com.au>2013-09-01 02:26:15 +1000
committerCarlo Zancanaro <carlo@clearboxsystems.com.au>2013-09-01 02:26:15 +1000
commitd8bbc050c528f0ee358fdf16b564e0a0abaaf24a (patch)
tree398f6bce6adc95e0fc746a2701092f288dc35369 /test/reverse_routing
parent809dee0523d267c49b970a91a5cbe7aa119627fd (diff)
Clean it up a bit - it's mostly macros now, unfortunately
Diffstat (limited to 'test/reverse_routing')
-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")))))