MyNixOS website logo
Description

Test 'wai' endpoints via Test.Tasty.

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

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
Metadata

Version

0.1.2.0

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows