From 62f908d988dcbd55a6aa8e2f8b8ac380db5a8c8c Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Sat, 26 Jan 2019 16:34:16 +0200 Subject: [PATCH] Second fix to #209 --- modules/reitit-ring/src/reitit/ring.cljc | 2 +- test/cljc/reitit/ring_test.cljc | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) 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"})))