Rework ResponsePredicate to include request and PredicateFailure without Maybe request
This commit is contained in:
parent
54a05a53a9
commit
8673a776dd
@ -12,7 +12,7 @@ import Prelude.Compat
|
|||||||
import Text.PrettyPrint
|
import Text.PrettyPrint
|
||||||
|
|
||||||
data PredicateFailure
|
data PredicateFailure
|
||||||
= PredicateFailure T.Text (Maybe C.Request) (C.Response LBS.ByteString)
|
= PredicateFailure T.Text (C.Request) (C.Response LBS.ByteString)
|
||||||
deriving (Typeable, Generic)
|
deriving (Typeable, Generic)
|
||||||
|
|
||||||
instance Exception ServerEqualityFailure where
|
instance Exception ServerEqualityFailure where
|
||||||
@ -71,10 +71,5 @@ prettyPredicateFailure :: PredicateFailure -> Doc
|
|||||||
prettyPredicateFailure (PredicateFailure predicate req resp) =
|
prettyPredicateFailure (PredicateFailure predicate req resp) =
|
||||||
text "Predicate failed" $$ (nest 5 $
|
text "Predicate failed" $$ (nest 5 $
|
||||||
text "Predicate:" <+> (text $ T.unpack predicate)
|
text "Predicate:" <+> (text $ T.unpack predicate)
|
||||||
$$ r
|
$$ prettyReq req
|
||||||
$$ prettyResp resp)
|
$$ prettyResp resp)
|
||||||
where
|
|
||||||
r = case req of
|
|
||||||
Nothing -> text ""
|
|
||||||
Just v -> prettyReq v
|
|
||||||
|
|
||||||
|
|||||||
@ -41,8 +41,9 @@ import Servant.QuickCheck.Internal.ErrorTypes
|
|||||||
--
|
--
|
||||||
-- /Since 0.0.0.0/
|
-- /Since 0.0.0.0/
|
||||||
not500 :: ResponsePredicate
|
not500 :: ResponsePredicate
|
||||||
not500 = ResponsePredicate $ \resp ->
|
not500 = ResponsePredicate $ \req resp ->
|
||||||
when (responseStatus resp == status500) $ fail "not500"
|
when (responseStatus resp == status500) $
|
||||||
|
throw $ PredicateFailure "not500" req resp
|
||||||
|
|
||||||
-- | [__Optional__]
|
-- | [__Optional__]
|
||||||
--
|
--
|
||||||
@ -57,7 +58,7 @@ notLongerThan maxAllowed
|
|||||||
resp <- httpLbs req mgr
|
resp <- httpLbs req mgr
|
||||||
end <- getTime Monotonic
|
end <- getTime Monotonic
|
||||||
when (toNanoSecs (end `diffTimeSpec` start) > maxAllowed) $
|
when (toNanoSecs (end `diffTimeSpec` start) > maxAllowed) $
|
||||||
throw $ PredicateFailure "notLongerThan" (Just req) resp
|
throw $ PredicateFailure "notLongerThan" req resp
|
||||||
return []
|
return []
|
||||||
|
|
||||||
-- | [__Best Practice__]
|
-- | [__Best Practice__]
|
||||||
@ -83,8 +84,8 @@ notLongerThan maxAllowed
|
|||||||
-- /Since 0.0.0.0/
|
-- /Since 0.0.0.0/
|
||||||
onlyJsonObjects :: ResponsePredicate
|
onlyJsonObjects :: ResponsePredicate
|
||||||
onlyJsonObjects
|
onlyJsonObjects
|
||||||
= ResponsePredicate (\resp -> case go resp of
|
= ResponsePredicate (\req resp -> case go resp of
|
||||||
Nothing -> throw $ PredicateFailure "onlyJsonObjects" Nothing resp
|
Nothing -> throw $ PredicateFailure "onlyJsonObjects" req resp
|
||||||
Just () -> return ())
|
Just () -> return ())
|
||||||
where
|
where
|
||||||
go r = do
|
go r = do
|
||||||
@ -119,9 +120,9 @@ createContainsValidLocation
|
|||||||
resp <- httpLbs req mgr
|
resp <- httpLbs req mgr
|
||||||
if responseStatus resp == status201
|
if responseStatus resp == status201
|
||||||
then case lookup "Location" $ responseHeaders resp of
|
then case lookup "Location" $ responseHeaders resp of
|
||||||
Nothing -> fail n
|
Nothing -> throw $ PredicateFailure "createContainsValidLocation" req resp
|
||||||
Just l -> case parseRequest $ SBSC.unpack l of
|
Just l -> case parseRequest $ SBSC.unpack l of
|
||||||
Nothing -> fail n
|
Nothing -> throw $ PredicateFailure "createContainsValidLocation" req resp
|
||||||
Just x -> do
|
Just x -> do
|
||||||
resp2 <- httpLbs x mgr
|
resp2 <- httpLbs x mgr
|
||||||
status2XX resp2 n
|
status2XX resp2 n
|
||||||
@ -159,8 +160,8 @@ getsHaveLastModifiedHeader
|
|||||||
if (method req == methodGet)
|
if (method req == methodGet)
|
||||||
then do
|
then do
|
||||||
resp <- httpLbs req mgr
|
resp <- httpLbs req mgr
|
||||||
unless (hasValidHeader "Last-Modified" isRFC822Date resp) $ do
|
unless (hasValidHeader "Last-Modified" isRFC822Date resp) $
|
||||||
throw $ PredicateFailure "getsHaveLastModifiedHeader" (Just req) resp
|
throw $ PredicateFailure "getsHaveLastModifiedHeader" req resp
|
||||||
return [resp]
|
return [resp]
|
||||||
else return []
|
else return []
|
||||||
|
|
||||||
@ -192,7 +193,7 @@ notAllowedContainsAllowHeader
|
|||||||
| m <- [minBound .. maxBound ]
|
| m <- [minBound .. maxBound ]
|
||||||
, renderStdMethod m /= method req ]
|
, renderStdMethod m /= method req ]
|
||||||
case filter pred' resp of
|
case filter pred' resp of
|
||||||
(x:_) -> throw $ PredicateFailure "notAllowedContainsAllowHeader" (Just req) x
|
(x:_) -> throw $ PredicateFailure "notAllowedContainsAllowHeader" req x
|
||||||
[] -> return resp
|
[] -> return resp
|
||||||
where
|
where
|
||||||
pred' resp = responseStatus resp == status405 && not (hasValidHeader "Allow" go resp)
|
pred' resp = responseStatus resp == status405 && not (hasValidHeader "Allow" go resp)
|
||||||
@ -250,8 +251,8 @@ getsHaveCacheControlHeader
|
|||||||
if (method req == methodGet)
|
if (method req == methodGet)
|
||||||
then do
|
then do
|
||||||
resp <- httpLbs req mgr
|
resp <- httpLbs req mgr
|
||||||
unless (hasValidHeader "Cache-Control" (const True) resp) $ do
|
unless (hasValidHeader "Cache-Control" (const True) resp) $
|
||||||
throw $ PredicateFailure "getsHaveCacheControlHeader" (Just req) resp
|
throw $ PredicateFailure "getsHaveCacheControlHeader" req resp
|
||||||
return [resp]
|
return [resp]
|
||||||
else return []
|
else return []
|
||||||
|
|
||||||
@ -267,7 +268,7 @@ headsHaveCacheControlHeader
|
|||||||
then do
|
then do
|
||||||
resp <- httpLbs req mgr
|
resp <- httpLbs req mgr
|
||||||
unless (hasValidHeader "Cache-Control" (const True) resp) $
|
unless (hasValidHeader "Cache-Control" (const True) resp) $
|
||||||
throw $ PredicateFailure "headsHaveCacheControlHeader" (Just req) resp
|
throw $ PredicateFailure "headsHaveCacheControlHeader" req resp
|
||||||
return [resp]
|
return [resp]
|
||||||
else return []
|
else return []
|
||||||
{-
|
{-
|
||||||
@ -333,10 +334,10 @@ linkHeadersAreValid
|
|||||||
-- /Since 0.0.0.0/
|
-- /Since 0.0.0.0/
|
||||||
unauthorizedContainsWWWAuthenticate :: ResponsePredicate
|
unauthorizedContainsWWWAuthenticate :: ResponsePredicate
|
||||||
unauthorizedContainsWWWAuthenticate
|
unauthorizedContainsWWWAuthenticate
|
||||||
= ResponsePredicate $ \resp ->
|
= ResponsePredicate $ \req resp ->
|
||||||
if responseStatus resp == status401
|
if responseStatus resp == status401
|
||||||
then unless (hasValidHeader "WWW-Authenticate" (const True) resp) $
|
then unless (hasValidHeader "WWW-Authenticate" (const True) resp) $
|
||||||
fail "unauthorizedContainsWWWAuthenticate"
|
throw $ PredicateFailure "unauthorizedContainsWWWAuthenticate" req resp
|
||||||
else return ()
|
else return ()
|
||||||
|
|
||||||
-- * Predicate logic
|
-- * Predicate logic
|
||||||
@ -351,12 +352,12 @@ unauthorizedContainsWWWAuthenticate
|
|||||||
--
|
--
|
||||||
-- /Since 0.0.0.0/
|
-- /Since 0.0.0.0/
|
||||||
newtype ResponsePredicate = ResponsePredicate
|
newtype ResponsePredicate = ResponsePredicate
|
||||||
{ getResponsePredicate :: Response LBS.ByteString -> IO ()
|
{ getResponsePredicate :: Request -> Response LBS.ByteString -> IO ()
|
||||||
} deriving (Generic)
|
} deriving (Generic)
|
||||||
|
|
||||||
instance Monoid ResponsePredicate where
|
instance Monoid ResponsePredicate where
|
||||||
mempty = ResponsePredicate $ const $ return ()
|
mempty = ResponsePredicate (\req resp -> return ())
|
||||||
ResponsePredicate a `mappend` ResponsePredicate b = ResponsePredicate $ \x -> a x >> b x
|
ResponsePredicate a `mappend` ResponsePredicate b = ResponsePredicate $ \x y -> a x y >> b x y
|
||||||
|
|
||||||
-- | A predicate that depends on both the request and the response.
|
-- | A predicate that depends on both the request and the response.
|
||||||
--
|
--
|
||||||
@ -406,7 +407,8 @@ finishPredicates p req mgr = go `catch` \(e :: PredicateFailure) -> return $ Jus
|
|||||||
where
|
where
|
||||||
go = do
|
go = do
|
||||||
resps <- getRequestPredicate (requestPredicates p) req mgr
|
resps <- getRequestPredicate (requestPredicates p) req mgr
|
||||||
mapM_ (getResponsePredicate $ responsePredicates p) resps
|
let responder = getResponsePredicate (responsePredicates p) req
|
||||||
|
mapM_ responder resps
|
||||||
return Nothing
|
return Nothing
|
||||||
|
|
||||||
-- * helpers
|
-- * helpers
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user