Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match partial body. #41

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/Test/Hspec/Wai/Matcher.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Test.Hspec.Wai.Matcher (
, Body
, (<:>)
, bodyEquals
, bodyContains
, match
) where

Expand All @@ -17,7 +18,7 @@ import Data.Maybe
import Data.String
import Data.Text.Lazy.Encoding (encodeUtf8)
import qualified Data.Text.Lazy as T
import Data.ByteString (ByteString)
import Data.ByteString (ByteString, isInfixOf)
import qualified Data.ByteString.Lazy as LB
import Network.HTTP.Types
import Network.Wai.Test
Expand All @@ -37,14 +38,20 @@ data MatchHeader = MatchHeader ([Header] -> Body -> Maybe String)
data MatchBody = MatchBody ([Header] -> Body -> Maybe String)

bodyEquals :: Body -> MatchBody
bodyEquals body = MatchBody (\_ actual -> bodyMatcher actual body)
bodyEquals body = MatchBody $ \_ actual -> bodyMismatch "body mismatch:" actual body
<$ guard (toStrict actual /= toStrict body)

bodyContains :: Body -> MatchBody
bodyContains sub = MatchBody $ \_ full -> bodyMismatch "body substring search failed:" full sub
<$ guard (not $ toStrict sub `isInfixOf` toStrict full)

bodyMismatch :: String -> Body -> Body -> String
bodyMismatch msg actual expected = actualExpected msg actual_ expected_
where
bodyMatcher :: Body -> Body -> Maybe String
bodyMatcher (toStrict -> actual) (toStrict -> expected) = actualExpected "body mismatch:" actual_ expected_ <$ guard (actual /= expected)
where
(actual_, expected_) = case (safeToString actual, safeToString expected) of
(Just x, Just y) -> (x, y)
_ -> (show actual, show expected)
f = safeToString . toStrict
(actual_, expected_) = case (f actual, f expected) of
(Just x, Just y) -> (x, y)
_ -> (show actual, show expected)

matchAny :: MatchBody
matchAny = MatchBody (\_ _ -> Nothing)
Expand Down
14 changes: 14 additions & 0 deletions test/Test/Hspec/Wai/MatcherSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,17 @@ spec = do
, "the actual headers were:"
, " Content-Length: 23"
]

context "when body has a partial match" $ do
it "returns Nothing" $ do
SResponse status200 [] "@=.#" `match` 200 { matchBody = bodyContains "=." }
`shouldBe` Nothing

context "when body has NO partial match" $ do
it "returns an error message" $ do
SResponse status200 [] "@.#" `match` 200 { matchBody = bodyContains "=." }
`shouldBe` (Just . unlines) [
"body substring search failed:"
, " expected: =."
, " but got: @.#"
]