Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ring session middleware #316

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions modules/reitit-middleware/src/reitit/ring/middleware/session.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(ns reitit.ring.middleware.session
(:require
[clojure.spec.alpha :as s]
[ring.middleware.session :as session]
[ring.middleware.session.memory :as memory]))

(s/def ::spec (s/keys :opt-un [::session]))
dawranliou marked this conversation as resolved.
Show resolved Hide resolved

(def ^:private store
"The default shared in-memory session store.

This is used when no `:session` key is provided to the middleware."
(atom {}))

(def session-middleware
"Middleware for session.

Enter:
Add the `:session` key into the request map based on the `:cookies`
in the request map.

Exit:
When `:session` key presents in the response map, update the session
store with its value. Then remove `:session` from the response map.

| key | description |
| -------------|-------------|
| `:session` | `ring.middleware.session.store/SessionStore` instance. Use `ring.middleware.session.memory/MemoryStore` by default."
dawranliou marked this conversation as resolved.
Show resolved Hide resolved
{:name :session
:spec ::spec
:compile (fn [{:keys [session]
:or {session {:store (memory/memory-store store)}}} _]
{:wrap #(session/wrap-session % session)})})
60 changes: 60 additions & 0 deletions test/clj/reitit/ring/middleware/session_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
(ns reitit.ring.middleware.session-test
(:require [clojure.test :refer [deftest testing is]]
[reitit.ring.middleware.session :as session]
[ring.middleware.session.memory :as memory]
[reitit.ring :as ring]))

(defn get-session-id
"Parse the session-id out of response headers."
[request]
(let [pattern #"ring-session=([-\w]+);Path=/;HttpOnly"
parse-fn (partial re-find pattern)]
(-> request
(get-in [:headers "Set-Cookie"])
first
parse-fn
second)))

(defn handler
"The handler that increments the counter."
[{session :session}]
(let [counter (inc (:counter session 0))]
{:status 200
:body {:counter counter}
:session {:counter counter}}))

(deftest session-test
(let [store (atom {})
app (ring/ring-handler
(ring/router
["/api"
{:session {:store (memory/memory-store store)}
:middleware [session/session-middleware]}
["/ping" handler]
["/pong" handler]]))
first-response (app {:request-method :get
:uri "/api/ping"})
session-id (get-session-id first-response)
second-response (app {:request-method :get
:uri "/api/pong"
:cookies {"ring-session" {:value session-id}}})]
(is (= (count @store)
1))
(is (-> @store first second)
{:counter 2})))

(deftest default-session-test
(let [app (ring/ring-handler
(ring/router
["/api"
{:middleware [session/session-middleware]}
["/ping" handler]
["/pong" handler]]))
first-response (app {:request-method :get
:uri "/api/ping"})
session-id (get-session-id first-response)
second-response (app {:request-method :get
:uri "/api/pong"
:cookies {"ring-session" {:value session-id}}})]
(is (= (inc (get-in first-response [:body :counter]))
(get-in second-response [:body :counter])))))