From 0095aece21ff5680f57eb98f82389f75716d4cf0 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Sun, 28 Oct 2018 18:20:35 +0200 Subject: [PATCH] Router is injected into request in the default branch * releated to #159 --- CHANGELOG.md | 10 ++++++++++ modules/reitit-http/src/reitit/http.cljc | 4 ++-- modules/reitit-ring/src/reitit/ring.cljc | 4 ++-- test/clj/reitit/http_test.clj | 17 +++++++++++++++++ test/cljc/reitit/ring_test.cljc | 12 ++++++++++++ 5 files changed, 43 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b6168bd..e4156c7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +## UNRELEASED + +## `reitit-ring` + +* router is injected into request also in the default branch + +## `reitit-http` + +* router is injected into request also in the default branch + ## 0.2.4 (21.10.2018) ## `reitit-ring` diff --git a/modules/reitit-http/src/reitit/http.cljc b/modules/reitit-http/src/reitit/http.cljc index fff47f52..bf307bd4 100644 --- a/modules/reitit-http/src/reitit/http.cljc +++ b/modules/reitit-http/src/reitit/http.cljc @@ -139,7 +139,7 @@ (impl/fast-assoc ::r/router router))] (or (interceptor/execute executor interceptors request) (interceptor/execute executor default-queue request))) - (interceptor/execute executor default-queue request))) + (interceptor/execute executor default-queue (impl/fast-assoc request ::r/router router)))) ([request respond raise] (let [default #(interceptor/execute executor default-queue % respond raise)] (if-let [match (r/match-by-path router (:uri request))] @@ -158,7 +158,7 @@ (if interceptors (interceptor/execute executor interceptors request respond' raise) (default request))) - (default request))) + (default (impl/fast-assoc request ::r/router router)))) nil)) {::r/router router})))) diff --git a/modules/reitit-ring/src/reitit/ring.cljc b/modules/reitit-ring/src/reitit/ring.cljc index 933ba065..e8fb737b 100644 --- a/modules/reitit-ring/src/reitit/ring.cljc +++ b/modules/reitit-ring/src/reitit/ring.cljc @@ -226,7 +226,7 @@ (impl/fast-assoc ::r/match match) (impl/fast-assoc ::r/router router))] (or (handler request) (default-handler request))) - (default-handler request))) + (default-handler (impl/fast-assoc request ::r/router router)))) ([request respond raise] (if-let [match (r/match-by-path router (:uri request))] (let [method (:request-method request) @@ -238,7 +238,7 @@ (impl/fast-assoc ::r/match match) (impl/fast-assoc ::r/router router))] ((routes handler default-handler) request respond raise)) - (default-handler request respond raise)) + (default-handler (impl/fast-assoc request ::r/router router) respond raise)) nil))) {::r/router router})))) diff --git a/test/clj/reitit/http_test.clj b/test/clj/reitit/http_test.clj index f964b049..387ef13d 100644 --- a/test/clj/reitit/http_test.clj +++ b/test/clj/reitit/http_test.clj @@ -487,3 +487,20 @@ (is (= {:enter {:top 1, :api 1, :ping 1, :get 1} :leave {:get 1, :ping 1, :api 1, :top 1}} @times)))) + +(deftest router-available-in-default-branch + (testing "1-arity" + ((http/ring-handler + (http/router []) + (fn [{:keys [::r/router]}] + (is router)) + {:executor sieppari/executor}) + {})) + (testing "3-arity" + ((http/ring-handler + (http/router []) + (fn [{:keys [::r/router]}] + (println "here...") + (is router)) + {:executor sieppari/executor}) + {} (promise) (promise)))) diff --git a/test/cljc/reitit/ring_test.cljc b/test/cljc/reitit/ring_test.cljc index e339c73a..f91086e8 100644 --- a/test/cljc/reitit/ring_test.cljc +++ b/test/cljc/reitit/ring_test.cljc @@ -487,3 +487,15 @@ (is (= "text/xml" (get-in @result [:headers "Content-Type"]))) (is (get-in @result [:headers "Last-Modified"])) (is (= "file\n" (slurp (:body @result))))))))))))) + +(deftest router-available-in-default-branch + (testing "1-arity" + ((ring/ring-handler + (ring/router []) + (fn [{:keys [::r/router]}] + (is router))) {})) + (testing "3-arity" + ((ring/ring-handler + (ring/router []) + (fn [{:keys [::r/router]} _ _] + (is router))) {} (promise) (promise))))