Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add route information to request
  • Loading branch information
cbui authored and cbui committed Jul 5, 2015
1 parent d4bd824 commit fdef421
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/compojure/core.clj
Expand Up @@ -106,11 +106,13 @@
"Returns a function that will only call the handler if the method and path
match the request."
[method path handler]
(if-method method
(if-route path
(wrap-route-middleware
(fn [request]
(response/render (handler request) request))))))
(let [route-info [(or method :any) (str path)]]
(if-method method
(if-route path
(wrap-route-middleware
(fn [request]
(let [request (assoc request :compojure/route route-info)]
(response/render (handler request) request))))))))

(defn compile-route
"Compile a route in the form (method path bindings & body) into a function.
Expand Down
15 changes: 14 additions & 1 deletion test/compojure/core_test.clj
Expand Up @@ -37,7 +37,7 @@
(assoc :params {:y "bar"}))]
((GET "/:x" [x :as r]
(is (= x "foo"))
(is (= (dissoc r :params :route-params)
(is (= (dissoc r :params :route-params :compojure/route)
(dissoc req :params)))
nil)
req)))
Expand Down Expand Up @@ -249,3 +249,16 @@
(dotimes [_ 10]
(handler (mock/request :get "/foo")))
(is (= @counter 1)))))

(deftest route-information-test
(let [route (GET "/foo/:id" req req)
request (route (mock/request :get "/foo/1"))]
(testing "request has matched route information"
(is (= (request :compojure/route)
[:get "/foo/:id"]))))

(let [route (ANY "/foo/:id" req req)
request (route (mock/request :post "/foo/1" {}))]
(testing "ANY request has matched route information"
(is (= (request :compojure/route)
[:any "/foo/:id"])))))

0 comments on commit fdef421

Please sign in to comment.