mirror of
https://github.com/metosin/reitit.git
synced 2026-01-20 21:19:02 +00:00
Fix spec and docstring
Document specs for the :session entity map. Fix the default session options. Add test to validate spec.
This commit is contained in:
parent
78a1cc144e
commit
f46244cc48
3 changed files with 65 additions and 39 deletions
1
modules/reitit-middleware/.nrepl-port
Normal file
1
modules/reitit-middleware/.nrepl-port
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
43803
|
||||||
|
|
@ -2,15 +2,21 @@
|
||||||
(:require
|
(:require
|
||||||
[clojure.spec.alpha :as s]
|
[clojure.spec.alpha :as s]
|
||||||
[ring.middleware.session :as session]
|
[ring.middleware.session :as session]
|
||||||
|
[ring.middleware.session.store :as session-store]
|
||||||
[ring.middleware.session.memory :as memory]))
|
[ring.middleware.session.memory :as memory]))
|
||||||
|
|
||||||
|
(s/def ::store #(satisfies? session-store/SessionStore %))
|
||||||
|
(s/def ::root string?)
|
||||||
|
(s/def ::cookie-name string?)
|
||||||
|
(s/def ::cookie-attrs map?)
|
||||||
|
(s/def ::session (s/keys :opt-un [::store ::root ::cookie-name ::cookie-attrs]))
|
||||||
(s/def ::spec (s/keys :opt-un [::session]))
|
(s/def ::spec (s/keys :opt-un [::session]))
|
||||||
|
|
||||||
(def ^:private store
|
(def ^:private store
|
||||||
"The default shared in-memory session store.
|
"The default shared in-memory session store.
|
||||||
|
|
||||||
This is used when no `:session` key is provided to the middleware."
|
This is used when no `:store` key is provided to the middleware."
|
||||||
(atom {}))
|
(memory/memory-store (atom {})))
|
||||||
|
|
||||||
(def session-middleware
|
(def session-middleware
|
||||||
"Middleware for session.
|
"Middleware for session.
|
||||||
|
|
@ -25,9 +31,9 @@
|
||||||
|
|
||||||
| key | description |
|
| key | description |
|
||||||
| -------------|-------------|
|
| -------------|-------------|
|
||||||
| `:session` | `ring.middleware.session.store/SessionStore` instance. Use `ring.middleware.session.memory/MemoryStore` by default."
|
| `: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`."
|
||||||
{:name :session
|
{:name :session
|
||||||
:spec ::spec
|
:spec ::spec
|
||||||
:compile (fn [{:keys [session]
|
:compile (fn [{session-opts :session} _]
|
||||||
:or {session {:store (memory/memory-store store)}}} _]
|
(let [session-opts (merge {:store store} session-opts)]
|
||||||
{:wrap #(session/wrap-session % session)})})
|
{:wrap #(session/wrap-session % session-opts)}))})
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,9 @@
|
||||||
(:require [clojure.test :refer [deftest testing is]]
|
(:require [clojure.test :refer [deftest testing is]]
|
||||||
[reitit.ring.middleware.session :as session]
|
[reitit.ring.middleware.session :as session]
|
||||||
[ring.middleware.session.memory :as memory]
|
[ring.middleware.session.memory :as memory]
|
||||||
[reitit.ring :as ring]))
|
[reitit.spec :as rs]
|
||||||
|
[reitit.ring :as ring]
|
||||||
|
[reitit.ring.spec :as rrs]))
|
||||||
|
|
||||||
(defn get-session-id
|
(defn get-session-id
|
||||||
"Parse the session-id out of response headers."
|
"Parse the session-id out of response headers."
|
||||||
|
|
@ -24,37 +26,54 @@
|
||||||
:session {:counter counter}}))
|
:session {:counter counter}}))
|
||||||
|
|
||||||
(deftest session-test
|
(deftest session-test
|
||||||
(let [store (atom {})
|
(testing "Custom session store"
|
||||||
app (ring/ring-handler
|
(let [store (atom {})
|
||||||
(ring/router
|
app (ring/ring-handler
|
||||||
["/api"
|
(ring/router
|
||||||
{:session {:store (memory/memory-store store)}
|
["/api"
|
||||||
:middleware [session/session-middleware]}
|
{:session {:store (memory/memory-store store)}
|
||||||
["/ping" handler]
|
:middleware [session/session-middleware]}
|
||||||
["/pong" handler]]))
|
["/ping" handler]
|
||||||
first-response (app {:request-method :get
|
["/pong" handler]]))
|
||||||
:uri "/api/ping"})
|
first-response (app {:request-method :get
|
||||||
session-id (get-session-id first-response)
|
:uri "/api/ping"})
|
||||||
second-response (app {:request-method :get
|
session-id (get-session-id first-response)
|
||||||
:uri "/api/pong"
|
second-response (app {:request-method :get
|
||||||
:cookies {"ring-session" {:value session-id}}})]
|
:uri "/api/pong"
|
||||||
(is (= (count @store)
|
:cookies {"ring-session" {:value session-id}}})]
|
||||||
1))
|
(testing "shared across routes"
|
||||||
(is (-> @store first second)
|
(is (= (count @store)
|
||||||
{:counter 2})))
|
1))
|
||||||
|
(is (-> @store first second)
|
||||||
|
{:counter 2})))))
|
||||||
|
|
||||||
(deftest default-session-test
|
(deftest default-session-test
|
||||||
(let [app (ring/ring-handler
|
(testing "Default session store"
|
||||||
(ring/router
|
(let [app (ring/ring-handler
|
||||||
["/api"
|
(ring/router
|
||||||
{:middleware [session/session-middleware]}
|
["/api"
|
||||||
["/ping" handler]
|
{:middleware [session/session-middleware]}
|
||||||
["/pong" handler]]))
|
["/ping" handler]
|
||||||
first-response (app {:request-method :get
|
["/pong" handler]]))
|
||||||
:uri "/api/ping"})
|
first-response (app {:request-method :get
|
||||||
session-id (get-session-id first-response)
|
:uri "/api/ping"})
|
||||||
second-response (app {:request-method :get
|
session-id (get-session-id first-response)
|
||||||
:uri "/api/pong"
|
second-response (app {:request-method :get
|
||||||
:cookies {"ring-session" {:value session-id}}})]
|
:uri "/api/pong"
|
||||||
(is (= (inc (get-in first-response [:body :counter]))
|
:cookies {"ring-session" {:value session-id}}})]
|
||||||
(get-in second-response [:body :counter])))))
|
(testing "shared across routes"
|
||||||
|
(is (= (inc (get-in first-response [:body :counter]))
|
||||||
|
(get-in second-response [:body :counter])))))))
|
||||||
|
|
||||||
|
(deftest session-spec-test
|
||||||
|
(testing "Session spec"
|
||||||
|
(testing "with invalid session store type"
|
||||||
|
(is
|
||||||
|
(thrown? Exception
|
||||||
|
(ring/ring-handler
|
||||||
|
(ring/router
|
||||||
|
["/api"
|
||||||
|
{:session {:store nil}
|
||||||
|
:middleware [session/session-middleware]
|
||||||
|
:handler handler}]
|
||||||
|
{:validate rrs/validate})))))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue