Skip to content

Commit

Permalink
Add the route context to the request map
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Chris Thunes committed Aug 25, 2021
1 parent a22e0fe commit 8549853
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/compojure/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,14 @@
(if-let [params (clout/route-matches route request)]
(let [uri (:uri request)
path (:path-info request uri)
context (or (:context request) "")
subpath (:__path-info params)
params (dissoc params :__path-info)]
params (dissoc params :__path-info)
context (remove-suffix (str route) ":__path-info")]
(-> request
(assoc-route-params (decode-route-params params))
(assoc :path-info (if (= subpath "") "/" subpath)
:context (remove-suffix uri subpath))))))
:context (remove-suffix uri subpath))
(update :compojure/context str context)))))

(defn- context-route [route]
(let [re-context {:__path-info #"|/.*"}]
Expand Down
28 changes: 28 additions & 0 deletions test/compojure/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,26 @@
"/foo/10/b%20r" "/foo/10"
"/bar/10" nil)))

(testing "compojure/context key"
(let [resp (fn [req bar] {:body [bar (:compojure/context req)] :status 200})
handler (GET "/foo/:bar" [bar :as req] (resp req bar))
cxt-handler (context "/foo/:bar" [bar] (GET "/" req (resp req bar)))
in-cxt-handler (context "/foo" [] (GET "/:bar" [bar :as req] (resp req bar)))
root-cxt-handler (context "/" [] (GET "/foo/:bar" [bar :as req] (resp req bar)))
request (mock/request :get "/foo/bar") ]
(is (= (-> request handler :body) ["bar" nil]))
(is (= (-> request cxt-handler :body) ["bar" "/foo/:bar"]))
(is (= (-> request in-cxt-handler :body) ["bar" "/foo"]))
(is (= (-> request root-cxt-handler :body) ["bar" nil]))))

(testing "compojure/context key in nested context"
(let [handler (context "/foo" []
(context "/:bar" [bar]
(GET "/baz" req {:status 200
:body [bar (:compojure/context req)]})))
request (mock/request :get "/foo/bar/baz")]
(is (= (-> request handler :body) ["bar" "/foo/:bar"]))))

(testing "path-info key"
(let [handler (context "/foo/:id" [id] :path-info)]
(are [url ctx] (= (handler (mock/request :get url)) ctx)
Expand Down Expand Up @@ -298,6 +318,14 @@
response (handler (mock/request :get "/foo"))]
(is (= @matched [:get "/foo"]))))

(testing "matched route context available in request"
(let [route (context "/foo/:bar" [] (GET "/baz" [] "foo"))
matched (atom nil)
middleware (fn [h] (fn [r] (reset! matched (:compojure/context r)) (h r)))
handler (wrap-routes route middleware)
response (handler (mock/request :get "/foo/bar/baz"))]
(is (= @matched "/foo/:bar"))))

(testing "nested route-middlewares are applied in order"
(let [mw (fn [handler value]
(fn [req]
Expand Down

0 comments on commit 8549853

Please sign in to comment.