{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ConstraintKinds #-}
module Test.Hspec.Wai (
WaiSession
, WaiExpectation
, get
, post
, put
, patch
, options
, delete
, request
, postHtmlForm
, shouldRespondWith
, ResponseMatcher(..)
, MatchHeader(..)
, MatchBody(..)
, Body
, (<:>)
, liftIO
, with
, withState
, getState
, pending
, pendingWith
) where
import Prelude ()
import Prelude.Compat
import Data.Foldable
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as LB
import Control.Monad.IO.Class
import Network.Wai (Request(..))
import Network.HTTP.Types
import Network.Wai.Test hiding (request)
import qualified Network.Wai.Test as Wai
import Test.Hspec.Expectations
import Test.Hspec.Core.Spec hiding (pending, pendingWith)
import qualified Test.Hspec.Core.Spec as Core
import Test.Hspec.Core.Hooks
import Test.Hspec.Wai.Util
import Test.Hspec.Wai.Internal
import Test.Hspec.Wai.Matcher
import Network.Wai (Application)
with :: IO Application -> SpecWith ((), Application) -> Spec
with :: IO Application -> SpecWith ((), Application) -> Spec
with action :: IO Application
action = IO ((), Application) -> SpecWith ((), Application) -> Spec
forall a. IO a -> SpecWith a -> Spec
before ((,) () (Application -> ((), Application))
-> IO Application -> IO ((), Application)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO Application
action)
withState :: IO (st, Application) -> SpecWith (st, Application) -> Spec
withState :: IO (st, Application) -> SpecWith (st, Application) -> Spec
withState = IO (st, Application) -> SpecWith (st, Application) -> Spec
forall a. IO a -> SpecWith a -> Spec
before
pending :: WaiSession st ()
pending :: WaiSession st ()
pending = IO () -> WaiSession st ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO ()
HasCallStack => IO ()
Core.pending
pendingWith :: String -> WaiSession st ()
pendingWith :: String -> WaiSession st ()
pendingWith = IO () -> WaiSession st ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> WaiSession st ())
-> (String -> IO ()) -> String -> WaiSession st ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => String -> IO ()
String -> IO ()
Core.pendingWith
shouldRespondWith :: HasCallStack => WaiSession st SResponse -> ResponseMatcher -> WaiExpectation st
shouldRespondWith :: WaiSession st SResponse -> ResponseMatcher -> WaiExpectation st
shouldRespondWith action :: WaiSession st SResponse
action matcher :: ResponseMatcher
matcher = do
SResponse
r <- WaiSession st SResponse
action
Maybe String -> (String -> WaiExpectation st) -> WaiExpectation st
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (SResponse -> ResponseMatcher -> Maybe String
match SResponse
r ResponseMatcher
matcher) (IO () -> WaiExpectation st
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> WaiExpectation st)
-> (String -> IO ()) -> String -> WaiExpectation st
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => String -> IO ()
String -> IO ()
expectationFailure)
get :: ByteString -> WaiSession st SResponse
get :: ByteString -> WaiSession st SResponse
get path :: ByteString
path = ByteString
-> ByteString -> [Header] -> ByteString -> WaiSession st SResponse
forall st.
ByteString
-> ByteString -> [Header] -> ByteString -> WaiSession st SResponse
request ByteString
methodGet ByteString
path [] ""
post :: ByteString -> LB.ByteString -> WaiSession st SResponse
post :: ByteString -> ByteString -> WaiSession st SResponse
post path :: ByteString
path = ByteString
-> ByteString -> [Header] -> ByteString -> WaiSession st SResponse
forall st.
ByteString
-> ByteString -> [Header] -> ByteString -> WaiSession st SResponse
request ByteString
methodPost ByteString
path []
put :: ByteString -> LB.ByteString -> WaiSession st SResponse
put :: ByteString -> ByteString -> WaiSession st SResponse
put path :: ByteString
path = ByteString
-> ByteString -> [Header] -> ByteString -> WaiSession st SResponse
forall st.
ByteString
-> ByteString -> [Header] -> ByteString -> WaiSession st SResponse
request ByteString
methodPut ByteString
path []
patch :: ByteString -> LB.ByteString -> WaiSession st SResponse
patch :: ByteString -> ByteString -> WaiSession st SResponse
patch path :: ByteString
path = ByteString
-> ByteString -> [Header] -> ByteString -> WaiSession st SResponse
forall st.
ByteString
-> ByteString -> [Header] -> ByteString -> WaiSession st SResponse
request ByteString
methodPatch ByteString
path []
options :: ByteString -> WaiSession st SResponse
options :: ByteString -> WaiSession st SResponse
options path :: ByteString
path = ByteString
-> ByteString -> [Header] -> ByteString -> WaiSession st SResponse
forall st.
ByteString
-> ByteString -> [Header] -> ByteString -> WaiSession st SResponse
request ByteString
methodOptions ByteString
path [] ""
delete :: ByteString -> WaiSession st SResponse
delete :: ByteString -> WaiSession st SResponse
delete path :: ByteString
path = ByteString
-> ByteString -> [Header] -> ByteString -> WaiSession st SResponse
forall st.
ByteString
-> ByteString -> [Header] -> ByteString -> WaiSession st SResponse
request ByteString
methodDelete ByteString
path [] ""
request :: Method -> ByteString -> [Header] -> LB.ByteString -> WaiSession st SResponse
request :: ByteString
-> ByteString -> [Header] -> ByteString -> WaiSession st SResponse
request method :: ByteString
method path :: ByteString
path headers :: [Header]
headers body :: ByteString
body = WaiSession st Application
forall st. WaiSession st Application
getApp WaiSession st Application
-> (Application -> WaiSession st SResponse)
-> WaiSession st SResponse
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= IO SResponse -> WaiSession st SResponse
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO SResponse -> WaiSession st SResponse)
-> (Application -> IO SResponse)
-> Application
-> WaiSession st SResponse
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Session SResponse -> Application -> IO SResponse
forall a. Session a -> Application -> IO a
runSession (SRequest -> Session SResponse
Wai.srequest (SRequest -> Session SResponse) -> SRequest -> Session SResponse
forall a b. (a -> b) -> a -> b
$ Request -> ByteString -> SRequest
SRequest Request
req ByteString
body)
where
req :: Request
req = Request -> ByteString -> Request
setPath Request
defaultRequest {requestMethod :: ByteString
requestMethod = ByteString
method, requestHeaders :: [Header]
requestHeaders = [Header]
headers} ByteString
path
postHtmlForm :: ByteString -> [(String, String)] -> WaiSession st SResponse
postHtmlForm :: ByteString -> [(String, String)] -> WaiSession st SResponse
postHtmlForm path :: ByteString
path = ByteString
-> ByteString -> [Header] -> ByteString -> WaiSession st SResponse
forall st.
ByteString
-> ByteString -> [Header] -> ByteString -> WaiSession st SResponse
request ByteString
methodPost ByteString
path [(HeaderName
hContentType, "application/x-www-form-urlencoded")] (ByteString -> WaiSession st SResponse)
-> ([(String, String)] -> ByteString)
-> [(String, String)]
-> WaiSession st SResponse
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(String, String)] -> ByteString
formUrlEncodeQuery