mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 16:31:11 +00:00
Polish docs, more tests
This commit is contained in:
parent
bc2706147c
commit
0276e82dd9
3 changed files with 44 additions and 9 deletions
21
README.md
21
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
|
||||
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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}]
|
||||
|
|
|
|||
Loading…
Reference in a new issue