From 0276e82dd99a0a68833c14a59ec5174ce0336c61 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Sat, 19 Aug 2017 16:04:44 +0300 Subject: [PATCH] Polish docs, more tests --- README.md | 21 ++++++++++++++++----- src/reitit/core.cljc | 4 ++-- test/cljc/reitit/ring_test.cljc | 28 ++++++++++++++++++++++++++-- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 59a6f0c6..e1e195af 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ Same routes flattened: ["/api/ping" ::ping]] ``` -## Routers +## Routing For routing, a `Router` is needed. Reitit ships with 2 different router implementations: `LinearRouter` and `LookupRouter`, both based on the awesome [Pedestal](https://github.com/pedestal/pedestal/tree/master/route) implementation. @@ -78,7 +78,7 @@ Creating a router: (reitit/router [["/api" ["/ping" ::ping] - ["/user/:id" ::user]])) + ["/user/:id" ::user]]])) ``` `LinearRouter` is created (as there are wildcard): @@ -254,7 +254,8 @@ Routing based on `:request-method`: (def app (ring/ring-handler (ring/router - ["/ping" {:get handler + ["/ping" {:name ::ping + :get handler :post handler}]))) (app {:request-method :get, :uri "/ping"}) @@ -264,6 +265,16 @@ Routing based on `:request-method`: ; nil ``` +Reverse routing: + +```clj +(-> app + (ring/get-router) + (reitit/match-by-name ::ping) + :path) +; "/ping" +``` + Some middleware and a new handler: ```clj @@ -387,8 +398,8 @@ Routers can be configured via options. Options allow things like [`clojure.spec` | `:routes` | Initial resolved routes (default `[]`) | `:meta` | Initial expanded route-meta vector (default `[]`) | `:expand` | Function of `arg opts => meta` to expand route arg to route meta-data (default `reitit.core/expand`) - | `:coerce` | Function of `[path meta] opts => [path meta]` to coerce resolved route, can throw or return `nil` - | `:compile` | Function of `[path meta] opts => handler` to compile a route handler + | `:coerce` | Function of `route opts => route` to coerce resolved route, can throw or return `nil` + | `:compile` | Function of `route opts => handler` to compile a route handler ## Special thanks diff --git a/src/reitit/core.cljc b/src/reitit/core.cljc index 9bc35368..45bf3901 100644 --- a/src/reitit/core.cljc +++ b/src/reitit/core.cljc @@ -183,8 +183,8 @@ | `:routes` | Initial resolved routes (default `[]`) | `:meta` | Initial expanded route-meta vector (default `[]`) | `:expand` | Function of `arg opts => meta` to expand route arg to route meta-data (default `reitit.core/expand`) - | `:coerce` | Function of `[path meta] opts => [path meta]` to coerce resolved route, can throw or return `nil` - | `:compile` | Function of `[path meta] opts => handler` to compile a route handler" + | `:coerce` | Function of `route opts => route` to coerce resolved route, can throw or return `nil` + | `:compile` | Function of `route opts => handler` to compile a route handler" ([data] (router data {})) ([data opts] diff --git a/test/cljc/reitit/ring_test.cljc b/test/cljc/reitit/ring_test.cljc index bbd63e0b..641c40a7 100644 --- a/test/cljc/reitit/ring_test.cljc +++ b/test/cljc/reitit/ring_test.cljc @@ -2,7 +2,8 @@ (:require [clojure.test :refer [deftest testing is]] [reitit.middleware :as middleware] [reitit.ring :as ring] - [clojure.set :as set]) + [clojure.set :as set] + [reitit.core :as reitit]) #?(:clj (:import (clojure.lang ExceptionInfo)))) @@ -122,7 +123,30 @@ respond (partial reset! result), raise ::not-called] (app {:uri "/api/users" :request-method :post} respond raise) (is (= {:status 200, :body [:api :users :post :ok :post :users :api]} - @result))))))) + @result)))))) + + (testing "named routes" + (let [router (ring/router + [["/api" + ["/all" {:handler handler :name ::all}] + ["/get" {:get {:handler handler :name ::HIDDEN} + :name ::get}] + ["/users" {:get handler + :post handler + :handler handler + :name ::users}]]]) + app (ring/ring-handler router)] + + (testing "router can be extracted" + (is (= router (ring/get-router app)))) + + (testing "only top-level route names are matched" + (is (= [::all ::get ::users] + (reitit/route-names router)))) + + (testing "all named routes can be matched" + (doseq [name (reitit/route-names router)] + (is (= name (-> (reitit/match-by-name router name) :meta :name)))))))) (defn wrap-enforce-roles [handler] (fn [{:keys [::roles] :as request}]