Skip to content

Commit

Permalink
Merge pull request #212 from circleci/feature/route-context
Browse files Browse the repository at this point in the history
Add :compojure/route-context to the request map
  • Loading branch information
weavejester authored May 12, 2022
2 parents 409b14b + 6ae24d4 commit 3ef7d5f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/compojure/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@
(defn- remove-suffix [path suffix]
(subs path 0 (- (count path) (count suffix))))

(defn- context-request [request route]
(defn- context-request [request route route-context]
(if-let [params (clout/route-matches route request)]
(let [uri (:uri request)
path (:path-info request uri)
Expand All @@ -261,7 +261,8 @@
(-> 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/route-context str route-context)))))

(defn- context-route [route]
(let [re-context {:__path-info #"|/.*"}]
Expand All @@ -279,7 +280,7 @@
:else
`(clout/route-compile (str ~route ":__path-info") ~re-context))))

(defn ^:no-doc make-context [route make-handler]
(defn ^:no-doc make-context [route path make-handler]
(letfn [(handler
([request]
(when-let [context-handler (make-handler request)]
Expand All @@ -292,10 +293,10 @@
handler
(fn
([request]
(if-let [request (context-request request route)]
(if-let [request (context-request request route path)]
(handler request)))
([request respond raise]
(if-let [request (context-request request route)]
(if-let [request (context-request request route path)]
(handler request respond raise)
(respond nil)))))))

Expand All @@ -311,6 +312,7 @@
[path args & routes]
`(make-context
~(context-route path)
~path
(fn [request#]
(let-request [~args request#]
(routes ~@routes)))))
Expand Down
17 changes: 17 additions & 0 deletions test/compojure/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,23 @@
"/foo/10/b%20r" "/b%20r"
"/bar/10" nil)))

(testing ":compojure/route-context key"
(let [handler (context "/foo/:id" [_] :compojure/route-context)]
(are [url route-context] (= (handler (mock/request :get url)) route-context)
"/foo/10" "/foo/:id"
"/foo/10/bar" "/foo/:id"
"/foo/10/b%20r" "/foo/:id"
"/bar/10" nil)))

(testing ":compojure/route-context key in nested context"
(let [handler (context "/foo/:foo-id" [_]
(context "/bar/:bar-id" [_] :compojure/route-context))]
(are [url route-context] (= (handler (mock/request :get url)) route-context)
"/foo/1/bar/2" "/foo/:foo-id/bar/:bar-id"
"/foo/1/bar/2/baz" "/foo/:foo-id/bar/:bar-id"
"/foo/1/bar/2/b%20r" "/foo/:foo-id/bar/:bar-id"
"/bar/10" nil)))

(testing "routes"
(let [handler (context "/foo/:id" [id]
(GET "/" [] "root")
Expand Down

0 comments on commit 3ef7d5f

Please sign in to comment.