hspec-wai icon indicating copy to clipboard operation
hspec-wai copied to clipboard

Feature request: partial body matcher

Open ryantm opened this issue 10 years ago • 3 comments

It would be nice if there were a way to match part of the body of a response. Sometimes responses contain random bits that you don't care about testing.

ryantm avatar Sep 03 '15 14:09 ryantm

+1 on this being useful

cgag avatar Jan 07 '16 07:01 cgag

You can write your own custom matchers to do whatever you like using ResponseMatcher. For instance:

partialMatcher :: String -> ResponseMatcher
partialMatcher expected = ResponseMatcher {
    matchStatus=200
  , matchHeaders=[]
  , matchBody=MatchBody (\_ actual -> do 
      if encode expected `isInfixOf` actual
        then Nothing
        else Just "expected string wasn't a partial match of actual!"
    )
  }

You can then use this in your tests like so:

let expected = "expected partial response"
request methodPost someTestPath [] body `shouldRespondWith` partialMatcher expected

adlaika avatar May 13 '22 19:05 adlaika

...and I just realized this issue was last updated in 2016 😅

adlaika avatar May 13 '22 19:05 adlaika