Skip to content

Commit 9cbd488

Browse files
lrennweavejester
authored andcommitted
New middleware: with-context, ignore-trailing-slash, with-uri-rewrite.
Signed-off-by: James Reeves <jreeves@weavejester.com>
1 parent 8ca5f09 commit 9cbd488

2 files changed

Lines changed: 78 additions & 1 deletion

File tree

src/compojure/http/middleware.clj

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,51 @@
4646
(with-headers handler
4747
{"Cache-Control" (header-options header-map ", ")}))
4848

49+
(defn with-uri-rewrite
50+
"Rewrites a request uri with the result of calling f with the
51+
request's original uri. If f returns nil the handler is not called."
52+
[handler f]
53+
(fn [request]
54+
(let [uri (:uri request)
55+
rewrite (f uri)]
56+
(if rewrite
57+
(handler (assoc request :uri rewrite))
58+
nil))))
59+
60+
(defn- remove-or-nil-context
61+
"Removes a context string from the front of a uri. If it wasn't there,
62+
returns nil."
63+
[uri context]
64+
(if (.startsWith uri context)
65+
(if-not (= uri context)
66+
(subs uri (count context))
67+
"/")
68+
nil))
69+
70+
(defn with-context
71+
"Removes the context string from the beginning of the request uri
72+
such that route matching is done without it. If the context is not
73+
present, the handler will not be called."
74+
[handler context]
75+
(with-uri-rewrite handler #(remove-or-nil-context % context)))
76+
77+
(defn- uri-snip-slash
78+
"Removes a trailing slash from all uris except \"/\"."
79+
[uri]
80+
(if (and (not (= "/" uri))
81+
(.endsWith uri "/"))
82+
(chop uri)
83+
uri))
84+
85+
(defn ignore-trailing-slash
86+
"Makes routes match regardless of whether or not a uri ends in a slash."
87+
[handler]
88+
(with-uri-rewrite handler uri-snip-slash))
89+
4990
(defvar default-mimetypes
5091
{"css" "text/css"
5192
"gif" "image/gif"
93+
"gz" "application/gzip"
5294
"htm" "text/html"
5395
"html" "text/html"
5496
"jpg" "image/jpeg"

test/compojure/http/middleware.clj

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,45 @@
5151
(is (= "max-age=3600, must-revalidate"
5252
(get (:headers response) "Cache-Control"))))))
5353

54+
(defn run-ignore-trailing-slash-paths
55+
[route-path uri]
56+
(let [routes (routes (GET route-path "foo"))
57+
request {:request-method :get
58+
:uri uri}
59+
response ((ignore-trailing-slash routes) request)]
60+
(= (:body response) "foo")))
61+
62+
(deftest test-ignore-trailing-slash-paths
63+
(are (run-ignore-trailing-slash-paths _1 _2)
64+
"/" "/"
65+
"/foo" "/foo"
66+
"/foo" "/foo/"
67+
"/foo/bar" "/foo/bar/"))
68+
69+
(defn run-with-context
70+
[route-path uri context]
71+
(let [routes (routes (GET route-path "foo"))
72+
request {:request-method :get
73+
:uri uri}
74+
response ((with-context routes context) request)]
75+
(= (:body response) "foo")))
76+
77+
(deftest test-with-context
78+
(are (run-with-context _1 _2 "/context")
79+
"/" "/context"
80+
"/home" "/context/home"
81+
"/asset/1" "/context/asset/1"))
82+
83+
(deftest test-without-context
84+
(are (not (run-with-context _1 _2 "/context"))
85+
"/" "/"
86+
"/home" "/home"
87+
"/asset/1" "/asset/1"))
88+
5489
(defn run-mimetypes
5590
[uri type options]
5691
(let [routes (routes (GET uri "foo"))
57-
request {:request-method :get,
92+
request {:request-method :get
5893
:uri uri}
5994
response ((with-mimetypes routes options) request)
6095
result (get (:headers response) "Content-Type")]

0 commit comments

Comments
 (0)