Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #141 from mjroghelia/matched-route-middleware

Make matched route info available to middleware
  • Loading branch information...
commit 7bd602d5ff28b6421993d32bee2c26da0460e0a2 2 parents 61d8061 + cc1cf18
@weavejester authored
Showing with 19 additions and 8 deletions.
  1. +10 −7 src/compojure/core.clj
  2. +9 −1 test/compojure/core_test.clj
View
17 src/compojure/core.clj
@@ -117,17 +117,20 @@
((mw handler) request)
(handler request))))
+(defn- wrap-route-info [handler route-info]
+ (fn [request]
+ (handler (assoc request :compojure/route route-info))))
+
(defn make-route
"Returns a function that will only call the handler if the method and path
match the request."
[method path handler]
- (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))))))))
+ (let [route-info [(or method :any) (str path)]]
+ (if-method method
+ (if-route path
+ (-> (fn [request] (response/render (handler request) request))
+ (wrap-route-middleware)
+ (wrap-route-info route-info))))))
(defn compile-route
"Compile a route in the form (method path bindings & body) into a function.
View
10 test/compojure/core_test.clj
@@ -265,7 +265,15 @@
handler (wrap-routes route middleware)]
(dotimes [_ 10]
(handler (mock/request :get "/foo")))
- (is (= @counter 1)))))
+ (is (= @counter 1))))
+
+ (testing "matched route available in request"
+ (let [route (GET "/foo" [] "foo")
+ matched (atom nil)
+ middleware (fn [h] (fn [r] (reset! matched (:compojure/route r)) (h r)))
+ handler (wrap-routes route middleware)
+ response (handler (mock/request :get "/foo"))]
+ (is (= @matched [:get "/foo"])))))
(deftest route-information-test
(let [route (GET "/foo/:id" req req)
Please sign in to comment.
Something went wrong with that request. Please try again.