Skip to content

Commit 8549853

Browse files
author
Chris Thunes
committed
Add the route context to the request map
This provides access to the route prefix as a companion to the existing `:compojure/route` data allowing the full route to be reconstructed. Since the `context` macro only accepts a path the new `:compojure/context` value simply contains the route path instead of the `[method route]` pair present in `:compojure/route`. This data was introduced as a new field in the request path, rather than updating `:compojure/route` to include the context for the sake of backwards compatibility.
1 parent a22e0fe commit 8549853

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/compojure/core.clj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,14 @@
255255
(if-let [params (clout/route-matches route request)]
256256
(let [uri (:uri request)
257257
path (:path-info request uri)
258-
context (or (:context request) "")
259258
subpath (:__path-info params)
260-
params (dissoc params :__path-info)]
259+
params (dissoc params :__path-info)
260+
context (remove-suffix (str route) ":__path-info")]
261261
(-> request
262262
(assoc-route-params (decode-route-params params))
263263
(assoc :path-info (if (= subpath "") "/" subpath)
264-
:context (remove-suffix uri subpath))))))
264+
:context (remove-suffix uri subpath))
265+
(update :compojure/context str context)))))
265266

266267
(defn- context-route [route]
267268
(let [re-context {:__path-info #"|/.*"}]

test/compojure/core_test.clj

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,26 @@
188188
"/foo/10/b%20r" "/foo/10"
189189
"/bar/10" nil)))
190190

191+
(testing "compojure/context key"
192+
(let [resp (fn [req bar] {:body [bar (:compojure/context req)] :status 200})
193+
handler (GET "/foo/:bar" [bar :as req] (resp req bar))
194+
cxt-handler (context "/foo/:bar" [bar] (GET "/" req (resp req bar)))
195+
in-cxt-handler (context "/foo" [] (GET "/:bar" [bar :as req] (resp req bar)))
196+
root-cxt-handler (context "/" [] (GET "/foo/:bar" [bar :as req] (resp req bar)))
197+
request (mock/request :get "/foo/bar") ]
198+
(is (= (-> request handler :body) ["bar" nil]))
199+
(is (= (-> request cxt-handler :body) ["bar" "/foo/:bar"]))
200+
(is (= (-> request in-cxt-handler :body) ["bar" "/foo"]))
201+
(is (= (-> request root-cxt-handler :body) ["bar" nil]))))
202+
203+
(testing "compojure/context key in nested context"
204+
(let [handler (context "/foo" []
205+
(context "/:bar" [bar]
206+
(GET "/baz" req {:status 200
207+
:body [bar (:compojure/context req)]})))
208+
request (mock/request :get "/foo/bar/baz")]
209+
(is (= (-> request handler :body) ["bar" "/foo/:bar"]))))
210+
191211
(testing "path-info key"
192212
(let [handler (context "/foo/:id" [id] :path-info)]
193213
(are [url ctx] (= (handler (mock/request :get url)) ctx)
@@ -298,6 +318,14 @@
298318
response (handler (mock/request :get "/foo"))]
299319
(is (= @matched [:get "/foo"]))))
300320

321+
(testing "matched route context available in request"
322+
(let [route (context "/foo/:bar" [] (GET "/baz" [] "foo"))
323+
matched (atom nil)
324+
middleware (fn [h] (fn [r] (reset! matched (:compojure/context r)) (h r)))
325+
handler (wrap-routes route middleware)
326+
response (handler (mock/request :get "/foo/bar/baz"))]
327+
(is (= @matched "/foo/:bar"))))
328+
301329
(testing "nested route-middlewares are applied in order"
302330
(let [mw (fn [handler value]
303331
(fn [req]

0 commit comments

Comments
 (0)