Skip to content

Commit 4b575e2

Browse files
committed
Merge pull request #321 from sonwh98/better-file-tests
Add option to change priority of wrap-file
2 parents 6e4e7f1 + 1a06cfe commit 4b575e2

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

ring-core/src/ring/middleware/file.clj

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,28 @@
4040
Accepts the following options:
4141
4242
:index-files? - look for index.* files in directories, defaults to true
43-
:allow-symlinks? - serve files through symbolic links, defaults to false"
43+
:allow-symlinks? - serve files through symbolic links, defaults to false
44+
:prefer-handler? - prioritize handler response over files, defaults to false"
4445
([handler root-path]
4546
(wrap-file handler root-path {}))
4647
([handler root-path options]
4748
(ensure-dir root-path)
4849
(fn
4950
([request]
50-
(or (file-request request root-path options) (handler request)))
51+
(if (:prefer-handler? options)
52+
(let [response (handler request)]
53+
(if (= 404 (:status response))
54+
(file-request request root-path options)
55+
response))
56+
(or (file-request request root-path options) (handler request))))
5157
([request respond raise]
52-
(if-let [response (file-request request root-path options)]
53-
(respond response)
54-
(handler request respond raise))))))
58+
(if (:prefer-handler? options)
59+
(handler request
60+
(fn [response]
61+
(if (= 404 (:status response))
62+
(respond (file-request request root-path options))
63+
(respond response)))
64+
raise)
65+
(if-let [response (file-request request root-path options)]
66+
(respond response)
67+
(handler request respond raise)))))))

ring-core/test/ring/middleware/test/file.clj

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
(deftest wrap-file-no-directory
1010
(is (thrown-with-msg? Exception #".*Directory does not exist.*"
11-
(wrap-file (constantly test-response) "not_here"))))
11+
(wrap-file (constantly test-response) "not_here"))))
1212

1313
(def public-dir "test/ring/assets")
1414
(def index-html (File. ^String public-dir "index.html"))
@@ -93,3 +93,42 @@
9393
(is (= 200 status))
9494
(is (= (into #{} (keys headers)) #{"Content-Length" "Last-Modified"}))
9595
(is (= foo-html body)))))
96+
97+
(defn- prefer-foo-handler
98+
([request]
99+
(if (= (:uri request) "/foo.html")
100+
{:status 200, :headers {}, :body "override"}
101+
{:status 404, :headers {}, :body "not found"}))
102+
([request respond raise]
103+
(respond (prefer-foo-handler request))))
104+
105+
(deftest test-wrap-file-with-prefer-handler
106+
(let [handler (wrap-file prefer-foo-handler
107+
(File. public-dir)
108+
{:prefer-handler? true})]
109+
110+
(testing "middleware serves file (synchronously)"
111+
(let [response (handler {:request-method :get, :uri "/index.html"})]
112+
(is (= 200 (:status response)))
113+
(is (= index-html (:body response)))))
114+
115+
(testing "middleware serves file (asynchronously)"
116+
(let [response (promise)
117+
error (promise)]
118+
(handler {:request-method :get, :uri "/index.html"} response error)
119+
(is (= 200 (:status @response)))
120+
(is (= index-html (:body @response)))
121+
(is (not (realized? error)))))
122+
123+
(testing "handler serves file (synchronously)"
124+
(let [response (handler {:request-method :get, :uri "/foo.html"})]
125+
(is (= 200 (:status response)))
126+
(is (= "override" (:body response)))))
127+
128+
(testing "handler serves file (asynchronously)"
129+
(let [response (promise)
130+
error (promise)]
131+
(handler {:request-method :get, :uri "/foo.html"} response error)
132+
(is (= 200 (:status @response)))
133+
(is (= "override" (:body @response)))
134+
(is (not (realized? error)))))))

0 commit comments

Comments
 (0)