mirror of
https://github.com/metosin/reitit.git
synced 2025-12-20 09:31:11 +00:00
Change session middleware's default to off
When `:session` key is absent in the route data, the session middleware will not
be attached to the route. To enable the middleware, the user at least need to
use an empty map `{}` for the `:session`, which uses the default options.
This commit is contained in:
parent
99efdeea2d
commit
a3c64baeba
2 changed files with 23 additions and 9 deletions
|
|
@ -31,9 +31,10 @@
|
|||
|
||||
| key | description |
|
||||
| -------------|-------------|
|
||||
| `:session` | A map of options that passes into the [`ring.middleware.session/wrap-session](http://ring-clojure.github.io/ring/ring.middleware.session.html#var-wrap-session) function`."
|
||||
| `:session` | A map of options that passes into the [`ring.middleware.session/wrap-session](http://ring-clojure.github.io/ring/ring.middleware.session.html#var-wrap-session) function`, or an empty map for the default options. The absence of this value will disable the middleware."
|
||||
{:name :session
|
||||
:spec ::spec
|
||||
:compile (fn [{session-opts :session} _]
|
||||
(if session-opts
|
||||
(let [session-opts (merge {:store store} session-opts)]
|
||||
{:wrap #(session/wrap-session % session-opts)}))})
|
||||
{:wrap #(session/wrap-session % session-opts)})))})
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@
|
|||
[request]
|
||||
(let [pattern #"ring-session=([-\w]+);Path=/;HttpOnly"
|
||||
parse-fn (partial re-find pattern)]
|
||||
(-> request
|
||||
(some-> request
|
||||
(get-in [:headers "Set-Cookie"])
|
||||
first
|
||||
parse-fn
|
||||
|
|
@ -52,7 +52,8 @@
|
|||
(let [app (ring/ring-handler
|
||||
(ring/router
|
||||
["/api"
|
||||
{:middleware [session/session-middleware]}
|
||||
{:middleware [session/session-middleware]
|
||||
:session {}}
|
||||
["/ping" handler]
|
||||
["/pong" handler]]))
|
||||
first-response (app {:request-method :get
|
||||
|
|
@ -65,6 +66,18 @@
|
|||
(is (= (inc (get-in first-response [:body :counter]))
|
||||
(get-in second-response [:body :counter])))))))
|
||||
|
||||
(deftest default-session-off-test
|
||||
(testing "Default session middleware"
|
||||
(let [app (ring/ring-handler
|
||||
(ring/router
|
||||
["/api"
|
||||
{:middleware [session/session-middleware]}
|
||||
["/ping" handler]]))
|
||||
resp (app {:request-method :get
|
||||
:uri "/api/ping"})]
|
||||
(testing "off by default"
|
||||
(is (nil? (get-session-id resp)))))))
|
||||
|
||||
(deftest session-spec-test
|
||||
(testing "Session spec"
|
||||
(testing "with invalid session store type"
|
||||
|
|
|
|||
Loading…
Reference in a new issue