Skip to content

Commit

Permalink
Change session middleware's default to off
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dawranliou committed Oct 4, 2019
1 parent 99efdee commit 5152e46
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@
{:name :session
:spec ::spec
:compile (fn [{session-opts :session} _]
(let [session-opts (merge {:store store} session-opts)]
{:wrap #(session/wrap-session % session-opts)}))})
(if session-opts
(let [session-opts (merge {:store store} session-opts)]
{:wrap #(session/wrap-session % session-opts)})))})
25 changes: 19 additions & 6 deletions test/clj/reitit/ring/middleware/session_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
[request]
(let [pattern #"ring-session=([-\w]+);Path=/;HttpOnly"
parse-fn (partial re-find pattern)]
(-> request
(get-in [:headers "Set-Cookie"])
first
parse-fn
second)))
(some-> request
(get-in [:headers "Set-Cookie"])
first
parse-fn
second)))

(defn handler
"The handler that increments the counter."
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down

0 comments on commit 5152e46

Please sign in to comment.