Permalink
Browse files

Added routing function

  • Loading branch information...
1 parent 7bfdb21 commit 876e5f33ef7e2f826f0d3c3c7edbddfdfb477542 @weavejester committed Dec 29, 2010
Showing with 19 additions and 2 deletions.
  1. +6 −1 src/compojure/core.clj
  2. +13 −1 test/compojure/test/core.clj
@@ -80,10 +80,15 @@
(bind-request request# ~bindings
(render (do ~@body) request#))))))))
+(defn routing
+ "Apply a list of routes to a Ring request map."
+ [request & handlers]
+ (some #(% request) handlers))
+
(defn routes
"Create a Ring handler by combining several handlers into one."
[& handlers]
- (-> (fn [req] (some #(% req) handlers))
+ (-> #(apply routing % handlers)
wrap-keyword-params
wrap-nested-params
wrap-params
@@ -50,14 +50,25 @@
resp {:status 200, :headers {}, :body "bar"}
route (PUT "/foo" [] resp)]
(is (= (route req) resp))))
-
+
(testing "custom regular expressions"
(expect [route-compile
(has-args ["/foo/:id" {:id "[0-9]+"}]
(times 1))]
(eval `(GET ["/foo/:id" :id "[0-9]+"] [])))))
+(deftest routing-test
+ (routing (request :get "/bar")
+ (GET "/foo" [] (is false) nil)
+ (GET "/bar" [] (is true) nil)))
+
(deftest routes-test
+ (testing "multiple routes"
+ ((routes
+ (GET "/foo" [] (is false) nil)
+ (GET "/bar" [] (is true) nil))
+ (request :get "/bar")))
+
(testing "keyword parameters"
((routes
(GET "/:x" [x y & more]
@@ -66,6 +77,7 @@
(is (= more {:z "baz"}))
nil))
(request :get "/foo" {:y "bar", :z "baz"})))
+
(testing "nested parameters"
((routes
(GET "/" [x y]

0 comments on commit 876e5f3

Please sign in to comment.