Skip to content

Commit 8a80255

Browse files
committed
Add support for multiple resource/file middleware
If :resources or :files is given a collection of paths, multiple wrap-resource and wrap-file middleware will be used.
1 parent 16aaff0 commit 8a80255

File tree

5 files changed

+47
-2
lines changed

5 files changed

+47
-2
lines changed

src/ring/middleware/defaults.clj

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@
6969
(middleware handler options)
7070
handler)))
7171

72+
(defn- wrap-multi [handler middleware args]
73+
(wrap handler
74+
(fn [handler args]
75+
(if (coll? args)
76+
(reduce middleware handler args)
77+
(middleware handler args)))
78+
args))
79+
7280
(defn- wrap-xss-protection [handler options]
7381
(x/wrap-xss-protection handler (:enable? options true) (dissoc options :enable?)))
7482

@@ -97,8 +105,8 @@
97105
(wrap wrap-params (get-in config [:params :urlencoded] false))
98106
(wrap wrap-cookies (get-in config [:cookies] false))
99107
(wrap wrap-absolute-redirects (get-in config [:responses :absolute-redirects] false))
100-
(wrap wrap-resource (get-in config [:static :resources] false))
101-
(wrap wrap-file (get-in config [:static :files] false))
108+
(wrap-multi wrap-resource (get-in config [:static :resources] false))
109+
(wrap-multi wrap-file (get-in config [:static :files] false))
102110
(wrap wrap-content-type (get-in config [:responses :content-types] false))
103111
(wrap wrap-default-charset (get-in config [:responses :default-charset] false))
104112
(wrap wrap-not-modified (get-in config [:responses :not-modified-responses] false))

test/ring/assets/public1/foo.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo1

test/ring/assets/public2/bar.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bar

test/ring/assets/public2/foo.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo2

test/ring/middleware/defaults_test.clj

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,40 @@
126126
(let [handler (wrap-defaults (constantly nil) site-defaults)]
127127
(is (nil? (handler (request :get "/"))))))
128128

129+
(testing "single resource path"
130+
(let [handler (wrap-defaults
131+
(constantly nil)
132+
(assoc-in site-defaults [:static :resources] "ring/assets/public1"))]
133+
(is (= (slurp (:body (handler (request :get "/foo.txt")))) "foo1\n"))
134+
(is (nil? (handler (request :get "/bar.txt"))))))
135+
136+
(testing "multiple resource paths"
137+
(let [handler (wrap-defaults
138+
(constantly nil)
139+
(assoc-in site-defaults
140+
[:static :resources]
141+
["ring/assets/public1"
142+
"ring/assets/public2"]))]
143+
(is (= (slurp (:body (handler (request :get "/foo.txt")))) "foo2\n"))
144+
(is (= (slurp (:body (handler (request :get "/bar.txt")))) "bar\n"))))
145+
146+
(testing "single file path"
147+
(let [handler (wrap-defaults
148+
(constantly nil)
149+
(assoc-in site-defaults [:static :files] "test/ring/assets/public1"))]
150+
(is (= (slurp (:body (handler (request :get "/foo.txt")))) "foo1\n"))
151+
(is (nil? (handler (request :get "/bar.txt"))))))
152+
153+
(testing "multiple file paths"
154+
(let [handler (wrap-defaults
155+
(constantly nil)
156+
(assoc-in site-defaults
157+
[:static :files]
158+
["test/ring/assets/public1"
159+
"test/ring/assets/public2"]))]
160+
(is (= (slurp (:body (handler (request :get "/foo.txt")))) "foo2\n"))
161+
(is (= (slurp (:body (handler (request :get "/bar.txt")))) "bar\n"))))
162+
129163
(testing "async handlers"
130164
(let [handler (-> (fn [_ respond _] (respond (response "foo")))
131165
(wrap-defaults api-defaults))

0 commit comments

Comments
 (0)