tasty-wai: Test 'wai' endpoints via Test.Tasty

[ bsd3, library, testing, web ] [ Propose Tags ] [ Report a vulnerability ]

Helper functions and runners for testing wai endpoints using the Tasty testing infrastructure.


[Skip to Readme]

Modules

[Index] [Quick Jump]

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1.0.0, 0.1.0.1, 0.1.1.0, 0.1.1.1, 0.1.2.0
Change log CHANGELOG.md
Dependencies base (>=4.8 && <4.20), bytestring (>=0.10 && <0.13), http-types (>=0.9 && <0.13), HUnit (>=1.6 && <1.7), tasty (>=0.8 && <1.6), wai (>=3.2 && <3.3), wai-extra (>=3 && <3.2) [details]
Tested with ghc ==7.10.3, ghc ==8.0.2, ghc ==8.2.2, ghc ==8.4.4, ghc ==8.6.5, ghc ==8.8.3, ghc ==8.10.1, ghc ==9.0.1
License BSD-3-Clause
Copyright Copyright (C) 2018 Commonwealth Scientific and Industrial Research Organisation (CSIRO)
Author QFPL @ Data61
Maintainer sean.chalmers@data61.csiro.au
Revised Revision 2 made by jack at 2023-10-19T05:43:33Z
Category Testing, Web
Source repo head: git clone https://github.com/qfpl/tasty-wai
Uploaded by GeorgeWilson at 2022-07-27T10:56:32Z
Distributions LTSHaskell:0.1.2.0, NixOS:0.1.2.0
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 2399 total (25 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2022-07-27 [all 1 reports]

Readme for tasty-wai-0.1.2.0

[back to package description]

CSIRO's Data61 Logo

Build Status

tasty-wai

This provides tasty integration for wai via the components provided by wai-extra.

This is a simple package, it does not provide any resource management for anything that your Application may require. Test databases and the like are not handled. This package provides a nicer interface to running tests again the endpoints and interrogating their results.

An example of usage

There is an example of usage in test/Test.hs and it is included here.

Given this trivial Application:

import           Network.Wai        (Application)
import qualified Network.Wai        as W

import qualified Network.HTTP.Types as H

testApp :: Application
testApp rq cb = do
  let
    mkresp s = W.responseLBS s []
    resp404 = mkresp H.status404
    resp200 = mkresp H.status200

  resp <- case (W.requestMethod rq, W.pathInfo rq) of

    -- Ye olde...
    ("GET", ["hello"]) -> pure $ resp200 "world!"

    -- Echo me this!
    ("POST", ["echo"]) -> resp200 <$> W.strictRequestBody rq

    -- Well, then...
    _ -> pure $ resp404 "no route"

  cb resp

We can write some tests to check the endpoints behave as we expect:

testWai testApp "Hello to World" $ do
  res <- get "hello"
  assertBody "world!" res

testWai testApp "Echo to thee" $ do
  res <- post "echo" "thus"
  assertStatus' H.status200 res -- Use functions from Network.HTTP.Types
  assertStatus 200 res          -- Use raw ints
  assertBody "thus" res

We can check that our fall-through route works as intended:

testWai testApp "Will die!" $ do
  res <- get "not-a-thing"
  assertStatus' H.status404 res
  assertBody "no route" res

These can be grouped up and run as per the tasty TestTree:

import           Test.Tasty         (defaultMain, testGroup)
import           Test.Tasty.Wai     (assertBody, assertStatus, assertStatus',
                                     get, post, testWai)

main :: IO ()
main = defaultMain $ testGroup "Tasty-Wai Tests"

  [ testWai testApp "Hello to World" $ do
      res <- get "hello"
      assertBody "wrld!" res

  , testWai testApp "Echo to thee" $ do
      res <- post "echo" "thus"
      assertStatus' H.status200 res -- Use functions from Network.HTTP.Types
      assertStatus 200 res          -- Use raw ints
      assertBody "thus" res

  , testWai testApp "Will die!" $ do
      res <- get "not-a-thing"
      assertStatus' H.status404 res
      assertBody "no route" res
  ]

Tasty then provides nicely formatted and grouped output, as you've come to expect:

Test suite tests: RUNNING...
Tasty-Wai Tests
  Hello to World: OK
  Echo to thee:   OK
  Will die!:      OK

With the errors from wai-extra helping us understanding where our tests went wrong:

Test suite tests: RUNNING...
Tasty-Wai Tests
  Hello to World: FAIL
    Expected response body "wrld!", but received "world!"
  Echo to thee:   OK
  Will die!:      OK