tasty-wai 0.1.1.1 → 0.1.2.0
raw patch · 4 files changed
+50/−12 lines, 4 filesdep ~basedep ~bytestringdep ~tastyPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base, bytestring, tasty
API changes (from Hackage documentation)
+ Test.Tasty.Wai: head :: ByteString -> Session SResponse
+ Test.Tasty.Wai: postWithHeaders :: ByteString -> ByteString -> RequestHeaders -> Session SResponse
Files
- CHANGELOG.md +4/−0
- src/Test/Tasty/Wai.hs +14/−2
- tasty-wai.cabal +6/−5
- test/Test.hs +26/−5
CHANGELOG.md view
@@ -1,5 +1,9 @@ # Revision history for tasty-wai +## 0.1.2.0 -- 2022-07-27++* Add `head` and `postWithHeaders` functions+ ## 0.1.1.1 -- 2020-09-24 * Support wai-extra 3.1
src/Test/Tasty/Wai.hs view
@@ -10,7 +10,9 @@ -- * Helpers , get+ , head , post+ , postWithHeaders , put , assertStatus' @@ -23,6 +25,7 @@ ) where import qualified Control.Exception as E+import Prelude hiding (head) import qualified Data.ByteString as BS import qualified Data.ByteString.Lazy as LBS@@ -31,7 +34,8 @@ import Network.HTTP.Types (RequestHeaders, StdMethod) import qualified Network.HTTP.Types as HTTP -import Test.HUnit.Lang (HUnitFailure (HUnitFailure), formatFailureReason)+import Test.HUnit.Lang (HUnitFailure (HUnitFailure),+ formatFailureReason) import Test.Tasty.Providers (IsTest (..), Progress (..), TestName, TestTree, singleTest, testFailed,@@ -43,7 +47,8 @@ import Network.Wai.Test -- | Data structure for carrying around the info needed to build and run a test.-data Sess = S Application TestName (Session ())+data Sess+ = S Application TestName (Session ()) instance IsTest Sess where -- No options yet@@ -110,6 +115,10 @@ testWai :: Application -> TestName -> Session () -> TestTree testWai a tn = singleTest tn . S a tn +-- | Submit a 'HTTP.HEAD' request to the provided endpoint.+head :: BS.ByteString -> Session SResponse+head = request . buildRequest HTTP.HEAD+ -- | Submit a 'HTTP.GET' request to the provided endpoint. get :: BS.ByteString -> Session SResponse get = request . buildRequest HTTP.GET@@ -118,6 +127,9 @@ -- 'LBS.ByteString' as the body content. post :: BS.ByteString -> LBS.ByteString -> Session SResponse post r = srequest . buildRequestWithBody HTTP.POST r++postWithHeaders :: BS.ByteString -> LBS.ByteString -> RequestHeaders -> Session SResponse+postWithHeaders path body headers = srequest $ buildRequestWithHeaders HTTP.POST path body headers -- | Submit a 'HTTP.PUT' request to the given endpoint with the provided -- 'LBS.ByteString' as the body content.
tasty-wai.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: tasty-wai-version: 0.1.1.1+version: 0.1.2.0 synopsis: Test 'wai' endpoints via Test.Tasty description: Helper functions and runners for testing wai endpoints using the Tasty testing infrastructure. license: BSD3@@ -27,6 +27,7 @@ , GHC == 8.6.5 , GHC == 8.8.3 , GHC == 8.10.1+ , GHC == 9.0.1 source-repository head type: git@@ -38,8 +39,8 @@ -- other-modules: -- other-extensions: - build-depends: base >= 4.8 && < 4.15- , tasty >= 0.8 && < 1.4+ build-depends: base >= 4.8 && < 4.17+ , tasty >= 0.8 && < 1.5 , bytestring >= 0.10 && < 0.12 , wai == 3.2.* , wai-extra >= 3 && < 3.2@@ -60,8 +61,8 @@ hs-source-dirs: test main-is: Test.hs - build-depends: base >= 4.8 && < 4.15- , tasty >= 0.8 && < 1.4+ build-depends: base >= 4.8 && < 4.17+ , tasty >= 0.8 && < 1.5 , wai == 3.2.* , http-types >= 0.9 && < 0.13 , tasty-wai
test/Test.hs view
@@ -1,6 +1,8 @@ {-# LANGUAGE OverloadedStrings #-} module Main where +import Prelude hiding (head)+ import Network.Wai (Application) import qualified Network.Wai as W @@ -8,7 +10,7 @@ import Test.Tasty (defaultMain, testGroup) import Test.Tasty.Wai (assertBody, assertStatus, assertStatus',- get, post, testWai)+ get, head, post, postWithHeaders, testWai) testApp :: Application testApp rq cb = do@@ -16,17 +18,25 @@ mkresp s = W.responseLBS s [] resp404 = mkresp H.status404 resp200 = mkresp H.status200+ resp204 = mkresp H.status204 - resp <- case (W.requestMethod rq, W.pathInfo rq) of+ resp <- case (W.requestMethod rq, W.pathInfo rq, W.requestHeaders rq) of + --+ ("HEAD", ["hello"], _) -> pure $ resp204 ""+ -- Ye olde...- ("GET", ["hello"]) -> pure $ resp200 "world!"+ ("GET", ["hello"], _) -> pure $ resp200 "world!" -- Echo me this!- ("POST", ["echo"]) -> resp200 <$> W.strictRequestBody rq+ ("POST", ["echo"], []) -> resp200 <$> W.strictRequestBody rq + -- Echo me this fine JSON!+ ("POST", ["echo"], [("content-type", "application/json")]) ->+ resp200 . ("{'field':'" <>) . (<> "'}") <$> W.strictRequestBody rq+ -- Well, then...- _ -> pure $ resp404 "no route"+ _ -> pure $ resp404 "no route" cb resp @@ -43,8 +53,19 @@ assertStatus 200 res -- Use raw ints assertBody "thus" res + , testWai testApp "Echo to thee" $ do+ res <- postWithHeaders "echo" "thus" [("content-type", "application/json")]+ assertStatus' H.status200 res -- Use functions from Network.HTTP.Types+ assertStatus 200 res -- Use raw ints+ assertBody ("{'field':'thus'}") res+ , testWai testApp "Will die!" $ do res <- get "not-a-thing" assertStatus' H.status404 res assertBody "no route" res++ , testWai testApp "Hello to World" $ do+ res <- head "hello"+ assertStatus' H.status204 res+ assertBody "" res ]