Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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