on master help sign in
Allow people to manage session state between calls to runSession. (#629)

* Allow people to manage session state between calls to runSession. * Bump version, update ChangeLog. * haddocks for runSessionWith. * Move ClientState(..) to "Internal" module. This makes import possible, but does not encourage it. * Expose ClientCookies from runSessionWith (rather than internal ClientState). * Revert "Expose ClientCookies from runSessionWith (rather than internal ClientState)." This reverts commit ed575fb2f0aa9fba37c32e93bebff2566e9a2dd4. * Move runSessionWith (and all that is needed for it to Test.Internal module.

commit4b09d13ade4a47d6a976b6416cce9c44e5f80a5b authorfisx <mf@zerobuzz.net> dateThu 22 Jun 2017, 18:15 (9 years ago) parent8c2c73e
4 files changed+44−18
wai-extra/ChangeLog.md+4 wai-extra/Network/Wai/Test.hs+3−17 wai-extra/Network/Wai/Test/Internal.hs+35 wai-extra/wai-extra.cabal+2−1
wai-extra/ChangeLog.md+4
@@ -1,3 +1,7 @@
1+## 3.0.20.0
2+
3+* runSessionWith (runSession variant that gives access to ClientState) [#629](https://github.com/yesodweb/wai/pull/629)
4+
15## 3.0.19.1
26
37* All loggers follow the autoFlush setting [#604](https://github.com/yesodweb/wai/pull/604)
wai-extra/Network/Wai/Test.hs+3−17
@@ -39,15 +39,15 @@ import Data.Monoid (mempty, mappend)
3939
4040import Network.Wai
4141import Network.Wai.Internal (ResponseReceived (ResponseReceived))
42+import Network.Wai.Test.Internal
4243import Control.Monad.IO.Class (liftIO)
4344import Control.Monad.Trans.Class (lift)
4445import qualified Control.Monad.Trans.State as ST
45−import Control.Monad.Trans.Reader (ReaderT, runReaderT, ask)
46+import Control.Monad.Trans.Reader (runReaderT, ask)
4647import Control.Monad (unless)
4748import Control.DeepSeq (deepseq)
4849import Control.Exception (throwIO, Exception)
4950import Data.Typeable (Typeable)
50−import Data.Map (Map)
5151import qualified Data.Map as Map
5252import qualified Web.Cookie as Cookie
5353import Data.ByteString (ByteString)
@@ -64,17 +64,6 @@ import qualified Data.Text.Encoding as TE
6464import Data.IORef
6565import Data.Time.Clock (getCurrentTime)
6666
67−type Session = ReaderT Application (ST.StateT ClientState IO)
68−
69−-- |
70−--
71−-- Since 3.0.6
72−type ClientCookies = Map ByteString Cookie.SetCookie
73−
74−data ClientState = ClientState
75− { clientCookies :: ClientCookies
76− }
77−
7867-- |
7968--
8069-- Since 3.0.6
@@ -104,9 +93,7 @@ deleteClientCookie cookieName =
10493 modifyClientCookies
10594 (Map.delete cookieName)
10695
107−initState :: ClientState
108−initState = ClientState Map.empty
109−
96+-- | See also: 'runSessionWith'.
11097runSession :: Session a -> Application -> IO a
11198runSession session app = ST.evalStateT (runReaderT session app) initState
11299
@@ -337,4 +324,3 @@ assertClientCookieValue s cookieName cookieValue = do
337324 ]
338325 )
339326 (Cookie.setCookieValue c == cookieValue)
340−
wai-extra/Network/Wai/Test/Internal.hsnew file+35
@@ -0,0 +1,35 @@
1+module Network.Wai.Test.Internal where
2+
3+import Network.Wai
4+import qualified Control.Monad.Trans.State as ST
5+import Control.Monad.Trans.Reader (ReaderT, runReaderT)
6+import Data.Map (Map)
7+import qualified Data.Map as Map
8+import qualified Web.Cookie as Cookie
9+import Data.ByteString (ByteString)
10+
11+type Session = ReaderT Application (ST.StateT ClientState IO)
12+
13+-- |
14+--
15+-- Since 3.0.6
16+type ClientCookies = Map ByteString Cookie.SetCookie
17+
18+data ClientState = ClientState
19+ { clientCookies :: ClientCookies
20+ }
21+
22+-- |
23+--
24+-- Since 3.0.20.0
25+initState :: ClientState
26+initState = ClientState Map.empty
27+
28+-- | Like 'runSession', but if allows you to hand in cookies and get
29+-- the updated cookies back. One use case for this is writing tests
30+-- that address the application under test alternatingly through rest
31+-- api and through db handle.
32+--
33+-- Since 3.0.20.0
34+runSessionWith :: ClientState -> Session a -> Application -> IO (a, ClientState)
35+runSessionWith st session app = ST.runStateT (runReaderT session app) st
wai-extra/wai-extra.cabal+2−1
@@ -1,5 +1,5 @@
11Name: wai-extra
2−Version: 3.0.19.1
2+Version: 3.0.20.0
33Synopsis: Provides some basic WAI handlers and middleware.
44description:
55 Provides basic WAI handler and middleware functionality:
@@ -148,6 +148,7 @@ Library
148148 Network.Wai.Request
149149 Network.Wai.UrlMap
150150 Network.Wai.Test
151+ Network.Wai.Test.Internal
151152 Network.Wai.EventSource
152153 Network.Wai.EventSource.EventStream
153154 other-modules: Network.Wai.Middleware.RequestLogger.Internal