Skip to content

Commit dd1f84c

Browse files
committed
Fix file/resource matching with multiple paths
1 parent f968995 commit dd1f84c

File tree

4 files changed

+50
-11
lines changed

4 files changed

+50
-11
lines changed

src/duct/handler/util.clj

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
[ring.middleware.content-type :as type]
44
[ring.util.async :as async]))
55

6-
(defn- match-path [uri paths]
7-
(first (filter #(str/starts-with? uri (key %)) paths)))
8-
96
(defn path-handler [paths make-response not-found-response]
10-
(let [paths (sort-by (comp count key) paths)]
7+
(let [paths (reverse (sort-by (comp count key) paths))]
118
(fn handler
129
([{:keys [uri] :as req}]
13-
(if-some [[path opts] (match-path uri paths)]
14-
(or (some-> (make-response (subs uri (count path)) opts)
15-
(type/content-type-response req opts))
16-
not-found-response)
17-
not-found-response))
10+
(or (some (fn [[path opts]]
11+
(when (str/starts-with? uri path)
12+
(some-> (make-response (subs uri (count path)) opts)
13+
(type/content-type-response req opts))))
14+
paths)
15+
not-found-response))
1816
([request respond raise]
1917
(async/raising raise (respond (handler request)))))))

test/duct/handler/file_test.clj

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(ns duct.handler.file-test
22
(:require [clojure.java.io :as io]
33
[clojure.test :refer [deftest is]]
4-
[duct.handler.file :as file]
4+
[duct.handler.file]
55
[integrant.core :as ig]))
66

77
(deftest test-file-handler
@@ -53,3 +53,22 @@
5353
:body (io/file root "foo.txt")}
5454
(-> (deref respond 100 nil)
5555
(update :headers dissoc "Last-Modified"))))))
56+
57+
(deftest test-multiple-paths
58+
(let [paths {"/" {:root "test/duct/handler/files"}
59+
"/sub" {:root "test/duct/handler/files/subdir"}}
60+
handler (ig/init-key :duct.handler/file {:paths paths})]
61+
(is (= {:status 200
62+
:headers {"Content-Type" "text/plain"
63+
"Content-Length" "4"}
64+
:body (io/file "test/duct/handler/files/foo.txt")}
65+
(-> (handler {:request-method :get, :uri "/foo.txt"})
66+
(update :headers dissoc "Last-Modified"))))
67+
(is (= {:status 200
68+
:headers {"Content-Type" "text/plain"
69+
"Content-Length" "4"}
70+
:body (io/file "test/duct/handler/files/subdir/bar.txt")}
71+
(-> (handler {:request-method :get, :uri "/sub/bar.txt"})
72+
(update :headers dissoc "Last-Modified"))))
73+
(is (nil? (handler {:request-method :get, :uri "/bar.txt"})))
74+
(is (nil? (handler {:request-method :get, :uri "/sub/foo.txt"})))))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bar

test/duct/handler/resource_test.clj

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(ns duct.handler.resource-test
22
(:require [clojure.test :refer [deftest is]]
3-
[duct.handler.resource :as resource]
3+
[duct.handler.resource]
44
[integrant.core :as ig]))
55

66
(deftest test-resource-handler
@@ -55,3 +55,24 @@
5555
(-> (deref respond 100 nil)
5656
(update :headers dissoc "Last-Modified")
5757
(update :body slurp))))))
58+
59+
(deftest test-multiple-paths
60+
(let [paths {"/" {:root "duct/handler/files"}
61+
"/sub" {:root "duct/handler/files/subdir"}}
62+
handler (ig/init-key :duct.handler/resource {:paths paths})]
63+
(is (= {:status 200
64+
:headers {"Content-Type" "text/plain"
65+
"Content-Length" "4"}
66+
:body "foo\n"}
67+
(-> (handler {:request-method :get, :uri "/foo.txt"})
68+
(update :headers dissoc "Last-Modified")
69+
(update :body slurp))))
70+
(is (= {:status 200
71+
:headers {"Content-Type" "text/plain"
72+
"Content-Length" "4"}
73+
:body "bar\n"}
74+
(-> (handler {:request-method :get, :uri "/sub/bar.txt"})
75+
(update :headers dissoc "Last-Modified")
76+
(update :body slurp))))
77+
(is (nil? (handler {:request-method :get, :uri "/bar.txt"})))
78+
(is (nil? (handler {:request-method :get, :uri "/sub/foo.txt"})))))

0 commit comments

Comments
 (0)