diff --git a/modules/reitit-ring/src/reitit/ring.cljc b/modules/reitit-ring/src/reitit/ring.cljc index 6fb9ce6a..e9a095ba 100644 --- a/modules/reitit-ring/src/reitit/ring.cljc +++ b/modules/reitit-ring/src/reitit/ring.cljc @@ -231,7 +231,7 @@ :or {parameter (keyword "") root "public" index-files ["index.html"] - index-redirect? true + index-redirect? false canonicalize-uris? true paths (constantly nil)}}] (let [options {:root root @@ -259,8 +259,14 @@ (loop [[file & files] index-files] (if file (if-let [resp (response (join-paths path file))] - (if index-redirect? + (cond + index-redirect? (response/redirect (join-paths uri file)) + + (not (str/ends-with? uri "/")) + (response/redirect (str uri "/")) + + :else resp) (recur files))))))) handler (if path @@ -287,8 +293,8 @@ | :path | path to mount the handler to. Required when mounted outside of a router, does not work inside a router. | :loader | optional class loader to resolve the resources | :index-files | optional vector of index-files to look in a resource directory, defaults to `[\"index.html\"]` - | :index-redirect? | optional boolean: if true (default), redirect to index file, if false serve it directly - | :canonicalize-uris? | optional boolean: if true (default), try to serve index files for non directory paths (paths that end with slash) + | :index-redirect? | optional boolean: if true, redirect to index file, if false serve it directly + | :canonicalize-uris? | optional boolean: if true (default), if path points to a folder with an index file, add a trailing slash (redirect) if it is missing. | :not-found-handler | optional handler function to use if the requested resource is missing (404 Not Found)" ([] (create-resource-handler nil)) @@ -306,8 +312,8 @@ | :path | path to mount the handler to. Required when mounted outside of a router, does not work inside a router. | :loader | optional class loader to resolve the resources | :index-files | optional vector of index-files to look in a resource directory, defaults to `[\"index.html\"]` - | :index-redirect? | optional boolean: if true (default), redirect to index file, if false serve it directly - | :canonicalize-uris? | optional boolean: if true (default), try to serve index files for non directory paths (paths that end with slash) + | :index-redirect? | optional boolean: if true, redirect to index file, if false serve it directly + | :canonicalize-uris? | optional boolean: if true (default), if path points to a folder with an index file, add a trailing slash (redirect) if it is missing. | :not-found-handler | optional handler function to use if the requested resource is missing (404 Not Found)" ([] (create-file-handler nil)) diff --git a/test/clj/reitit/http_test.clj b/test/clj/reitit/http_test.clj index c373a7af..1f04f72c 100644 --- a/test/clj/reitit/http_test.clj +++ b/test/clj/reitit/http_test.clj @@ -386,9 +386,9 @@ (testing "index-files" (let [response (app (request "/docs"))] - (is (= (redirect "/docs/index.html") response))) + (is (= (redirect "/docs/") response))) (let [response (app (request "/docs/"))] - (is (= (redirect "/docs/index.html") response)))) + (is (= 200 (:status response))))) (testing "not found" (let [response (app (request "/not-found"))] @@ -424,9 +424,9 @@ (testing "index-files" (let [response (app (request "/docs"))] - (is (= (redirect "/docs/index.html") response))) + (is (= (redirect "/docs/") response))) (let [response (app (request "/docs/"))] - (is (= (redirect "/docs/index.html") response)))) + (is (= 200 (:status response))))) (testing "not found" (let [response (app (request "/not-found"))] @@ -463,9 +463,9 @@ (testing "index-files" (let [response (app (request "/docs"))] - (is (= (redirect "/docs/index.html") response))) + (is (= (redirect "/docs/") response))) (let [response (app (request "/docs/"))] - (is (= (redirect "/docs/index.html") response)))) + (is (= 200 (:status response))))) (testing "not found" (let [response (app (request "/not-found"))] @@ -502,9 +502,9 @@ (testing "index-files" (let [response (app (request "/docs"))] - (is (= (redirect "/docs/index.html") response))) + (is (= (redirect "/docs/") response))) (let [response (app (request "/docs/"))] - (is (= (redirect "/docs/index.html") response)))) + (is (= 200 (:status response))))) (testing "not found" (let [response (app (request "/not-found"))] diff --git a/test/cljc/reitit/openapi_test.clj b/test/cljc/reitit/openapi_test.clj index 1b61f428..c474fbf3 100644 --- a/test/cljc/reitit/openapi_test.clj +++ b/test/cljc/reitit/openapi_test.clj @@ -337,7 +337,7 @@ {:path "/" :url "/openapi.json" :config {:jsonEditor true}})] - (is (= 302 (:status (app {:request-method :get, :uri "/"})))) + (is (= 200 (:status (app {:request-method :get, :uri "/"})))) (is (= 200 (:status (app {:request-method :get, :uri "/index.html"})))) (is (= {:jsonEditor true, :url "/openapi.json"} (->> {:request-method :get, :uri "/config.json"} diff --git a/test/cljc/reitit/ring_test.cljc b/test/cljc/reitit/ring_test.cljc index 57a11db6..27d3aa58 100644 --- a/test/cljc/reitit/ring_test.cljc +++ b/test/cljc/reitit/ring_test.cljc @@ -526,9 +526,9 @@ (testing "index-files" (let [response (app (request "/docs"))] - (is (= (redirect "/docs/index.html") response))) + (is (= (redirect "/docs/") response))) (let [response (app (request "/docs/"))] - (is (= (redirect "/docs/index.html") response)))) + (is (= 200 (:status response))))) (testing "not found" (let [response (app (request "/not-found"))] @@ -567,9 +567,9 @@ (testing "index-files" (let [response (app (request "/docs"))] - (is (= (redirect "/docs/index.html") response))) + (is (= (redirect "/docs/") response))) (let [response (app (request "/docs/"))] - (is (= (redirect "/docs/index.html") response)))) + (is (= 200 (:status response))))) (testing "not found" (let [response (app (request "/not-found"))] @@ -609,9 +609,9 @@ (testing "index-files" (let [response (app (request "/docs"))] - (is (= (redirect "/docs/index.html") response))) + (is (= (redirect "/docs/") response))) (let [response (app (request "/docs/"))] - (is (= (redirect "/docs/index.html") response)))) + (is (= 200 (:status response))))) (testing "not found" (let [response (app (request "/not-found"))] @@ -652,9 +652,9 @@ (testing "index-files" (let [response (app (request "/docs"))] - (is (= (redirect "/docs/index.html") response))) + (is (= (redirect "/docs/") response))) (let [response (app (request "/docs/"))] - (is (= (redirect "/docs/index.html") response)))) + (is (= 200 (:status response))))) (testing "not found" (let [response (app {:uri "/not-found" :request-method :get})] @@ -688,13 +688,13 @@ (testing "without index-redirect" (let [app (ring/ring-handler (ring/router - ["/*" (create {:index-redirect? false})]) + ["/*" (create {:canonicalize-uris? false + :index-redirect? false})]) (ring/create-default-handler))] (testing "index-files" (let [response (app (request "/docs"))] - (is (= 200 (:status response))) - (is (= "