From b8b442b5985e38e1948c632b10c3f8cfe571134f Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Sat, 9 May 2020 17:18:17 +0300 Subject: [PATCH] Fix #394 --- CHANGELOG.md | 4 ++++ modules/reitit-ring/src/reitit/ring.cljc | 2 +- test/cljc/reitit/ring_test.cljc | 21 +++++++++++++++------ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6de9d4ba..8606ebe9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,10 @@ is called the first time, so that `rfe/push-state` and such can be called [metosin/malli "0.0.1-20200404.091302-14"] is available but we use "0.0.1-20200305.102752-13" ``` +### `reitit-ring` + +* `reitit.ring/routes` strips away `nil` routes, fixes [#394](https://github.com/metosin/reitit/issues/394) + ## 0.4.2 (2020-01-17) ### `reitit` diff --git a/modules/reitit-ring/src/reitit/ring.cljc b/modules/reitit-ring/src/reitit/ring.cljc index 463e911e..f919b40c 100644 --- a/modules/reitit-ring/src/reitit/ring.cljc +++ b/modules/reitit-ring/src/reitit/ring.cljc @@ -108,7 +108,7 @@ (defn routes "Create a ring handler by combining several handlers into one." [& handlers] - (let [single-arity (apply some-fn handlers)] + (let [single-arity (apply some-fn (keep identity handlers))] (fn ([request] (single-arity request)) diff --git a/test/cljc/reitit/ring_test.cljc b/test/cljc/reitit/ring_test.cljc index 1c40defd..8710f178 100644 --- a/test/cljc/reitit/ring_test.cljc +++ b/test/cljc/reitit/ring_test.cljc @@ -24,6 +24,15 @@ ([request respond _] (respond (handler request)))) +(deftest routes-test + (testing "nils are removed" + (is (= 123 + ((ring/routes + (constantly nil) + nil + (constantly 123)) + ::irrelevant))))) + (deftest ring-router-test (testing "all paths should have a handler" @@ -161,8 +170,8 @@ (deftest default-handler-test (let [response {:status 200, :body "ok"} router (ring/router - [["/ping" {:get (constantly response)}] - ["/pong" (constantly nil)]]) + [["/ping" {:get (constantly response)}] + ["/pong" (constantly nil)]]) app (ring/ring-handler router)] (testing "match" @@ -188,9 +197,9 @@ (testing "with custom http responses" (let [app (ring/ring-handler router (ring/create-default-handler - {:not-found (constantly {:status -404}) - :method-not-allowed (constantly {:status -405}) - :not-acceptable (constantly {:status -406})}))] + {:not-found (constantly {:status -404}) + :method-not-allowed (constantly {:status -405}) + :not-acceptable (constantly {:status -406})}))] (testing "route doesn't match" (is (= -404 (:status (app {:request-method :get, :uri "/"}))))) (testing "method doesn't match" @@ -200,7 +209,7 @@ (testing "with some custom http responses" (let [app (ring/ring-handler router (ring/create-default-handler - {:not-found (constantly {:status -404})}))] + {:not-found (constantly {:status -404})}))] (testing "route doesn't match" (is (= 405 (:status (app {:request-method :post, :uri "/ping"}))))))))))