Sessions with runHandler

2 views
Skip to first unread message

nadine.a...@pobox.com

unread,
Dec 29, 2011, 9:25:21 AM12/29/11
to sn...@snapframework.com
Dear Snappers,

This should probably be easy, but I can't seem to figure it out. I'm trying
to run a sequence of request/responses with runHandler from Snap.Test. I am
using CookieSession to maintain a session. I get the cookie back from the
intial response just fine, but I don't see how to add it to the request so
that the session is maintained. Any pointers would be appreciated.

Best wishes,
Henry Laxen

--
Nadine & Henry Laxen Belle, Venus, Aphrodite
10580 N. McCarran Blvd. Adonis, Jarod
Suite 115-396 Via Alta # 6
Reno, Nevada Chapala, Jalisco, Mexico
89503-1896 CP 45900
http://www.nadineloveshenry.com/
The rest is silence. (Hamlet)

Henry Laxen

unread,
Jan 9, 2012, 11:41:34 PM1/9/12
to sn...@snapframework.com
<nadine.and.henry <at> pobox.com> writes:

>
> Dear Snappers,
>
> This should probably be easy, but I can't seem to figure it out. I'm trying
> to run a sequence of request/responses with runHandler from Snap.Test. I am
> using CookieSession to maintain a session. I get the cookie back from the
> intial response just fine, but I don't see how to add it to the request so
> that the session is maintained. Any pointers would be appreciated.
>
> Best wishes,
> Henry Laxen
>


I'm answering my own question, just in case someone else is in the
same position. While it is possible to do this (awkwardly) by using
RequestBuilder to build the request, then modifyRequest to add the
previous cookies and then run your own "runHandler" on the new
request, I think this is the long way around. It would be far easier
if an addCookies function were added to RequestBuilder.hs, the diff
I've included below:

diff --git a/src/Snap/Internal/Test/RequestBuilder.hs
b/src/Snap/Internal/Test/RequestBuilder.
index d224cf1..1e79b30 100644
--- a/src/Snap/Internal/Test/RequestBuilder.hs
+++ b/src/Snap/Internal/Test/RequestBuilder.hs
@@ -14,6 +14,7 @@ module Snap.Internal.Test.RequestBuilder
, setRequestType
, addHeader
, setHeader
+ , addCookies
, setContentType
, setSecure
, setHttpVersion
@@ -388,6 +389,11 @@ setHeader k v = rModify (H.setHeader k v)
addHeader :: (Monad m) => CI ByteString -> ByteString -> RequestBuilder m ()
addHeader k v = rModify (H.addHeader k v)

+------------------------------------------------------------------------------
+-- | Adds the given cookies to the request being built.
+addCookies :: (Monad m) => [Cookie] -> RequestBuilder m ()
+addCookies cookies = do
+ rModify $ \rq -> rq { rqCookies = rqCookies rq ++ cookies }

------------------------------------------------------------------------------
-- | Sets the request's @content-type@ to the given MIME type.
diff --git a/src/Snap/Test.hs b/src/Snap/Test.hs
index 13a0cca..df1b046 100644
--- a/src/Snap/Test.hs
+++ b/src/Snap/Test.hs
@@ -24,6 +24,7 @@ module Snap.Test

-- *** Precise control over building Requests
, addHeader
+ , addCookies
, setContentType
, setHeader
, setHttpVersion


I would also like to recommend adding a function like runHandler, that
instead of taking a RequestBuilder as an argument, takes and actual
Request. That would allow you to build a "base request" and then
modify it with all of the different rq... functions, and call the new
runHandler with the new request.

This way you can simulate a session programmatically, without ever
having to fire up a real web browser.

Best wishes,
Henry Laxen


Reply all
Reply to author
Forward
0 new messages