diff --git a/modules/reitit-ring/src/reitit/ring.cljc b/modules/reitit-ring/src/reitit/ring.cljc index 3b6909e8..4f2abef5 100644 --- a/modules/reitit-ring/src/reitit/ring.cljc +++ b/modules/reitit-ring/src/reitit/ring.cljc @@ -121,7 +121,7 @@ ([] (redirect-trailing-slash-handler {:method :both})) ([{:keys [method]}] (letfn [(maybe-redirect [request path] - (if (r/match-by-path (::r/router request) path) + (if (and (seq path) (r/match-by-path (::r/router request) path)) {:status (if (= (:request-method request) :get) 301 308) :headers {"Location" path} :body ""})) diff --git a/test/cljc/reitit/ring_test.cljc b/test/cljc/reitit/ring_test.cljc index 3edc486a..bce7314d 100644 --- a/test/cljc/reitit/ring_test.cljc +++ b/test/cljc/reitit/ring_test.cljc @@ -241,7 +241,9 @@ (deftest trailing-slash-handler-test (let [ok {:status 200, :body "ok"} - routes [["/slash-less" {:get (constantly ok), + routes [["" {:summary "unreachable" + :get (constantly ok)}] + ["/slash-less" {:get (constantly ok), :post (constantly ok)}] ["/with-slash/" {:get (constantly ok), :post (constantly ok)}]]] @@ -269,6 +271,9 @@ (ring/router routes) (ring/redirect-trailing-slash-handler {:method :strip}))] + (testing "stripping to empty string doesn't match" + (is (= nil (:status (app {:request-method :get, :uri "/"}))))) + (testing "exact matches work" (is (= ok (app {:request-method :get, :uri "/slash-less"}))) (is (= ok (app {:request-method :post, :uri "/slash-less"})))