Skip to content

Commit 0920456

Browse files
committed
Fix middleware ordering for nested wrap-routes
1 parent b4fd659 commit 0920456

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

src/compojure/core.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@
342342
(let [middleware (pre-init middleware)
343343
prep-request (fn [request]
344344
(let [mw (:route-middleware request identity)]
345-
(assoc request :route-middleware (comp middleware mw))))]
345+
(assoc request :route-middleware (comp mw middleware))))]
346346
(fn
347347
([request]
348348
(handler (prep-request request)))

test/compojure/core_test.clj

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,21 @@
286286
middleware (fn [h] (fn [r] (reset! matched (:compojure/route r)) (h r)))
287287
handler (wrap-routes route middleware)
288288
response (handler (mock/request :get "/foo"))]
289-
(is (= @matched [:get "/foo"])))))
289+
(is (= @matched [:get "/foo"]))))
290+
291+
(testing "nested route-middlewares are applied in order"
292+
(let [mw (fn [handler value]
293+
(fn [req]
294+
(let [resp (handler (update req :stack str value))]
295+
(update resp :body str value))))
296+
handler (wrap-routes
297+
(routes
298+
(wrap-routes (GET "/foo" req (:stack req)) mw "a")
299+
(wrap-routes (GET "/bar" req (:stack req)) mw "b"))
300+
mw
301+
"1")]
302+
(is (= "1aa1" (:body (handler (mock/request :get "/foo")))))
303+
(is (= "1bb1" (:body (handler (mock/request :get "/bar"))))))))
290304

291305
(deftest route-information-test
292306
(let [route (GET "/foo/:id" req req)

0 commit comments

Comments
 (0)