mirror of
https://github.com/vale981/lack
synced 2025-03-04 17:01:41 -05:00
Merge pull request #27 from mtstickney/delayed_session_control
Delayed-response session expiration
This commit is contained in:
commit
17756b63fd
2 changed files with 43 additions and 7 deletions
|
@ -35,12 +35,18 @@
|
|||
(if new-session-p
|
||||
(list :id sid :new-session t :change-id nil :expire nil)
|
||||
(list :id sid :new-session nil :change-id nil :expire nil)))
|
||||
(let ((res (funcall app env)))
|
||||
(if (and (not keep-empty)
|
||||
new-session-p
|
||||
(zerop (hash-table-count session)))
|
||||
res
|
||||
(finalize store state env res))))))
|
||||
(let ((res (funcall app env))
|
||||
(process-session (lambda (result)
|
||||
(if (and (not keep-empty)
|
||||
new-session-p
|
||||
(zerop (hash-table-count session)))
|
||||
result
|
||||
(finalize store state env result)))))
|
||||
(typecase res
|
||||
(function (lambda (responder)
|
||||
(funcall res (lambda (result)
|
||||
(funcall responder (funcall process-session result))))))
|
||||
(t (funcall process-session res)))))))
|
||||
"Middleware for session management")
|
||||
|
||||
(defun finalize (store state env res)
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
:lack.test))
|
||||
(in-package :t.lack.middleware.session)
|
||||
|
||||
(plan 6)
|
||||
(plan 7)
|
||||
|
||||
(ok (lack.session.state:make-state)
|
||||
"Base class of session state")
|
||||
|
@ -126,6 +126,36 @@
|
|||
"new session is not expired"))
|
||||
(is body '("hi") "body"))))
|
||||
|
||||
(subtest "session expiration with delayed response"
|
||||
(let ((app (builder
|
||||
:session
|
||||
(lambda (env)
|
||||
(if (equal (getf env :path-info) "/delayed-expire")
|
||||
(lambda (responder)
|
||||
(setf (getf (getf env :lack.session.options) :expire) t)
|
||||
(funcall responder '(200 () ("hi"))))
|
||||
(lambda (responder)
|
||||
(funcall responder '(200 () "hi")))))))
|
||||
session)
|
||||
;; Get a session.
|
||||
(funcall (funcall app (generate-env "/"))
|
||||
(lambda (result)
|
||||
(destructuring-bind (status headers body) result
|
||||
(declare (ignore status body))
|
||||
(setf session
|
||||
(ppcre:scan-to-strings "(?<=lack.session=)[^;]+"
|
||||
(getf headers :set-cookie ""))))))
|
||||
;; Make sure it expires when expiration is set in a delayed response.
|
||||
(funcall (funcall app (generate-env "/delayed-expire" :cookies `(("lack.session" . ,session))))
|
||||
(lambda (result)
|
||||
(destructuring-bind (status headers body) result
|
||||
(declare (ignore status body))
|
||||
(let ((cookie (cookie:parse-set-cookie-header (getf headers :set-cookie) "" "")))
|
||||
(is (cookie:cookie-value cookie) session
|
||||
"Set-Cookie header is for existing session")
|
||||
(ok (<= (cookie:cookie-expires cookie) (get-universal-time))
|
||||
"Session expired")))))))
|
||||
|
||||
(subtest ":keep-empty nil"
|
||||
(let ((app (builder
|
||||
(:session :keep-empty nil)
|
||||
|
|
Loading…
Add table
Reference in a new issue