Skip to content

Commit 5fc870b

Browse files
committed
Treat context for '/' the same as a blank string
Fixes #125.
1 parent d093486 commit 5fc870b

2 files changed

Lines changed: 26 additions & 13 deletions

File tree

src/compojure/core.clj

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -208,18 +208,20 @@
208208
(subs path 0 (- (count path) (count suffix))))
209209

210210
(defn- if-context [route handler]
211-
(fn [request]
212-
(if-let [params (clout/route-matches route request)]
213-
(let [uri (:uri request)
214-
path (:path-info request uri)
215-
context (or (:context request) "")
216-
subpath (:__path-info params)
217-
params (dissoc params :__path-info)]
218-
(handler
219-
(-> request
220-
(assoc-route-params (decode-route-params params))
221-
(assoc :path-info (if (= subpath "") "/" subpath)
222-
:context (remove-suffix uri subpath))))))))
211+
(if (#{":__path-info" "/:__path-info"} (:source route))
212+
handler
213+
(fn [request]
214+
(if-let [params (clout/route-matches route request)]
215+
(let [uri (:uri request)
216+
path (:path-info request uri)
217+
context (or (:context request) "")
218+
subpath (:__path-info params)
219+
params (dissoc params :__path-info)]
220+
(handler
221+
(-> request
222+
(assoc-route-params (decode-route-params params))
223+
(assoc :path-info (if (= subpath "") "/" subpath)
224+
:context (remove-suffix uri subpath)))))))))
223225

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

test/compojure/core_test.clj

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,18 @@
214214
request (mock/request :get "/emote/%5C%3F%2F") ]
215215
(is (= (-> request handler :body) "\\?/"))
216216
(is (= (-> request cxt-handler :body) "\\?/"))
217-
(is (= (-> request in-cxt-handler :body) "\\?/")))))
217+
(is (= (-> request in-cxt-handler :body) "\\?/"))))
218+
219+
(testing "root context"
220+
(let [handler (context "/" []
221+
(GET "/" [] "root")
222+
(GET "/foo" [] "foo")
223+
(GET "/foo/:x" [x] x))]
224+
(are [url body] (= (:body (handler (mock/request :get url)))
225+
body)
226+
"/" "root"
227+
"/foo" "foo"
228+
"/foo/2" "2"))))
218229

219230
(deftest let-routes-test
220231
(let [handler (let-routes [a "foo", b "bar"]

0 commit comments

Comments
 (0)