HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulehspec-wai-0.11.1Haskell2010

Test.Hspec.Wai

Have a look at the README for an example of how to use this library.

  • 6 types
  • 15 values

Types

2 declarations
newtypenewtype WaiSession st a
#

A WAI test session that carries the Application under test and some client state.

Instances9Monad, Functor, MonadFail, Applicative, MonadIO, Example, …

Performing requests

7 declarations

Posting HTML forms

Perform a POST request to the application under test.

The specified list of key-value pairs is encoded as application/x-www-form-urlencoded and used as request body.

In addition the Content-Type is set to application/x-www-form-urlencoded.

Matching on the response

6 declarations

Set the expectation that a response matches a specified ResponseMatcher.

A ResponseMatcher matches a response if:

  • the specified status matches the HTTP response status code

  • the specified body (if any) matches the response body

  • the response has all of the specified Header fields (the response may have arbitrary additional Header fields)

You can use ResponseMatcher's (broken) Num instance to match for a HTTP status code:

get "/" `shouldRespondWith` 200
-- matches if status is 200

You can use ResponseMatcher's IsString instance to match for a HTTP status 200 and a body:

get "/" `shouldRespondWith` "foo"
-- matches if body is "foo" and status is 200

If you want to match for a different HTTP status, you can use record update notation to specify matchStatus explicitly:

get "/" `shouldRespondWith` "foo" {matchStatus = 404}
-- matches if body is "foo" and status is 404

If you want to require a specific header field you can specify matchHeaders:

get "/" `shouldRespondWith` "foo" {matchHeaders = ["Content-Type" <:> "text/plain"]}
-- matches if body is "foo", status is 200 and there is a header field "Content-Type: text/plain"

Helpers and re-exports

6 declarations
methodliftIO :: IO a -> m a
#

Lift a computation from the IO monad. This allows us to run IO computations in any monadic stack, so long as it supports these kinds of operations (i.e. IO is the base monad for the stack).

Example
import Control.Monad.Trans.State -- from the "transformers" library

printState :: Show s => StateT s IO ()
printState = do
  state <- get
  liftIO $ print state

Had we omitted liftIO, we would have ended up with this error:

• Couldn't match type ‘IO’ with ‘StateT s IO’
 Expected type: StateT s IO ()
   Actual type: IO ()

The important part here is the mismatch between StateT s IO () and IO ().

Luckily, we know of a function that takes an IO a and returns an (m a): liftIO, enabling us to run the program and see the expected results:

> evalStateT printState "hello"
"hello"

> evalStateT printState 3
3