Skip to content

Commit

Permalink
Merge pull request metosin#471 from metosin/resource-handler-issue
Browse files Browse the repository at this point in the history
Support not-found-handler with path in resource handler (metosin#464)
  • Loading branch information
Miikka Koskinen authored Feb 19, 2021
2 parents e64490f + 902b33f commit ff55f85
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
3 changes: 2 additions & 1 deletion modules/reitit-ring/src/reitit/ring.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@
(fn [request]
(let [uri (:uri request)]
(if (str/starts-with? uri path)
(path-or-index-response (subs uri path-size) uri))))
(or (path-or-index-response (subs uri path-size) uri)
(not-found-handler request)))))
(fn [request]
(let [uri (:uri request)
path (-> request :path-params parameter)]
Expand Down
15 changes: 10 additions & 5 deletions test/cljc/reitit/ring_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@
(let [app (ring/ring-handler
(ring/router [])
(ring/routes
(create {:path "/"})
(create {:path "/" :not-found-handler (fn [x] {:status 404 :body "resource-handler"})})
(ring/create-default-handler)))]
(testing test
(testing "different file-types"
Expand All @@ -568,7 +568,8 @@

(testing "not found"
(let [response (app (request "/not-found"))]
(is (= 404 (:status response)))))
(is (= 404 (:status response)))
(is (= "resource-handler" (:body response)))))

(testing "3-arity"
(let [result (atom nil)
Expand All @@ -583,7 +584,7 @@
(let [app (ring/ring-handler
(ring/router [])
(ring/routes
(create {:path "/files"})
(create {:path "/files" :not-found-handler (fn [x] {:status 404 :body "resource-handler"})})
(ring/create-default-handler)))
request #(request (str "/files" %))
redirect #(redirect (str "/files" %))]
Expand All @@ -605,8 +606,12 @@
(is (= (redirect "/docs/index.html") response))))

(testing "not found"
(let [response (app (request "/not-found"))]
(is (= 404 (:status response)))))
(let [response (app {:uri "/not-found" :request-method :get})]
(is (= 404 (:status response)))
(is (= "" (:body response))))
(let [response (app {:uri "/files/not-found" :request-method :get})]
(is (= 404 (:status response)))
(is (= "resource-handler" (:body response)))))

(testing "3-arity"
(let [result (atom nil)
Expand Down

0 comments on commit ff55f85

Please sign in to comment.