packages feed

sydtest-wai (empty) → 0.0.0.0

raw patch · 6 files changed

+135/−0 lines, 6 filesdep +basedep +http-clientdep +http-types

Dependencies added: base, http-client, http-types, stm, sydtest, sydtest-wai, wai, warp

Files

+ LICENSE.md view
@@ -0,0 +1,5 @@+# Sydtest License++Copyright (c) 2020-2021 Tom Sydney Kerckhove++See the Sydtest License at https://github.com/NorfairKing/sydtest/blob/master/sydtest/LICENSE.md for the full license text.
+ src/Test/Syd/Wai.hs view
@@ -0,0 +1,44 @@+{-# LANGUAGE DataKinds #-}+{-# LANGUAGE RankNTypes #-}+{-# LANGUAGE TypeOperators #-}++module Test.Syd.Wai where++import Network.HTTP.Client as HTTP+import Network.Wai as Wai+import Network.Wai.Handler.Warp as Warp+import Test.Syd++-- | Run a given 'Wai.Application' around every test.+--+-- This provides the port on which the application is running.+waiSpec :: Wai.Application -> TestDef l Port -> TestDef l ()+waiSpec application = waiSpecWithSetupFunc $ pure application++-- | Run a 'Wai.Application' around every test by setting it up with the given setup function.+--+-- This provides the port on which the application is running.+waiSpecWith :: (forall r. (Application -> IO r) -> IO r) -> TestDef l Port -> TestDef l ()+waiSpecWith appFunc = waiSpecWithSetupFunc $ makeSimpleSetupFunc appFunc++-- | Run a 'Wai.Application' around every test by setting it up with the given setup function that can take an argument.+-- a+-- This provides the port on which the application is running.+waiSpecWith' :: (forall r. (Application -> IO r) -> (a -> IO r)) -> TestDef l Port -> TestDef l a+waiSpecWith' appFunc = waiSpecWithSetupFunc $ SetupFunc appFunc++-- | Run a 'Wai.Application' around every test by setting it up with the given 'SetupFunc'.+-- a+-- This provides the port on which the application is running.+waiSpecWithSetupFunc :: SetupFunc a Application -> TestDef l Port -> TestDef l a+waiSpecWithSetupFunc setupFunc = setupAroundWith (setupFunc `connectSetupFunc` applicationSetupFunc)++-- | A 'SetupFunc' to run an application and provide its port.+applicationSetupFunc :: SetupFunc Application Port+applicationSetupFunc = SetupFunc $ \func application ->+  Warp.testWithApplication (pure application) $ \p ->+    func p++-- | Create a 'HTTP.Manager' before all tests in the given group.+managerSpec :: TestDef (HTTP.Manager ': l) a -> TestDef l a+managerSpec = beforeAll $ HTTP.newManager HTTP.defaultManagerSettings
+ sydtest-wai.cabal view
@@ -0,0 +1,59 @@+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.34.4.+--+-- see: https://github.com/sol/hpack++name:           sydtest-wai+version:        0.0.0.0+synopsis:       A wai companion library for sydtest+category:       Testing+homepage:       https://github.com/NorfairKing/sydtest#readme+bug-reports:    https://github.com/NorfairKing/sydtest/issues+author:         Tom Sydney Kerckhove+maintainer:     syd@cs-syd.eu+copyright:      Copyright (c) 2020 Tom Sydney Kerckhove+license:        OtherLicense+license-file:   LICENSE.md+build-type:     Simple++source-repository head+  type: git+  location: https://github.com/NorfairKing/sydtest++library+  exposed-modules:+      Test.Syd.Wai+  other-modules:+      Paths_sydtest_wai+  hs-source-dirs:+      src+  build-depends:+      base >=4.7 && <5+    , http-client+    , sydtest+    , wai+    , warp+  default-language: Haskell2010++test-suite sydtest-wai-test+  type: exitcode-stdio-1.0+  main-is: Spec.hs+  other-modules:+      Test.Syd.Wai.Example+      Test.Syd.WaiSpec+      Paths_sydtest_wai+  hs-source-dirs:+      test+  ghc-options: -threaded -rtsopts -with-rtsopts=-N+  build-tool-depends:+      sydtest-discover:sydtest-discover+  build-depends:+      base >=4.7 && <5+    , http-client+    , http-types+    , stm+    , sydtest+    , sydtest-wai+    , wai+  default-language: Haskell2010
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF sydtest-discover #-}
+ test/Test/Syd/Wai/Example.hs view
@@ -0,0 +1,9 @@+module Test.Syd.Wai.Example where++import Network.HTTP.Types as HTTP+import Network.Wai as Wai++exampleApplication :: Wai.Application+exampleApplication req sendResp = do+  lb <- strictRequestBody req+  sendResp $ responseLBS HTTP.ok200 (requestHeaders req) lb
+ test/Test/Syd/WaiSpec.hs view
@@ -0,0 +1,17 @@+{-# LANGUAGE OverloadedStrings #-}++module Test.Syd.WaiSpec (spec) where++import Network.HTTP.Client as HTTP+import Test.Syd+import Test.Syd.Wai+import Test.Syd.Wai.Example++spec :: Spec+spec = managerSpec $+  waiSpec exampleApplication $ do+    itWithBoth "echos this example" $ \man p -> do+      let body = "hello world"+      req <- (\r -> r {port = p, requestBody = RequestBodyLBS body}) <$> parseRequest "http://localhost"+      resp <- httpLbs req man+      responseBody resp `shouldBe` body