@@ -1,6 +1,8 @@
11module Servant.QuickCheck.Internal.Predicates where
22
3−import Control.Monad (liftM2)
3+import Control.Exception (catch, SomeException, throw)
4+import Control.Monad (liftM2, guard, ap)
5+import Control.Monad.Reader
46import Data.Aeson (Object, decode)
57import Data.Bifunctor (Bifunctor (..))
68import Prelude.Compat
@@ -24,6 +26,9 @@ import Network.HTTP.Types (methodGet, methodHead, parseMethod,
2426 status300, status401, status405,
2527 status500, status100)
2628
29+import Servant.QuickCheck.Internal.ErrorTypes
30+
31+
2732-- | [__Best Practice__]
2833--
2934-- @500 Internal Server Error@ should be avoided - it may represent some
@@ -33,8 +38,9 @@ import Network.HTTP.Types (methodGet, methodHead, parseMethod,
3338-- This function checks that the response code is not 500.
3439--
3540-- /Since 0.0.0.0/
36−not500 :: ResponsePredicate Text Bool
37−not500 = ResponsePredicate "not500" (\resp -> not $ responseStatus resp == status500)
41+not500 :: ResponsePredicate
42+not500 = ResponsePredicate $ \resp ->
43+ when (responseStatus resp == status500) $ fail "not500"
3844
3945-- | [__Best Practice__]
4046--
@@ -57,11 +63,11 @@ not500 = ResponsePredicate "not500" (\resp -> not $ responseStatus resp == statu
5763-- * JSON Grammar: <https://tools.ietf.org/html/rfc4627#section-2 RFC 4627 Section 2>
5864--
5965-- /Since 0.0.0.0/
60−onlyJsonObjects :: ResponsePredicate Text Bool
66+onlyJsonObjects :: ResponsePredicate
6167onlyJsonObjects
62− = ResponsePredicate "onlyJsonObjects" (\resp -> case decode (responseBody resp) of
63− Nothing -> False
64− Just (_ :: Object) -> True)
68+ = ResponsePredicate (\resp -> case decode (responseBody resp) of
69+ Nothing -> throw $ PredicateFailure "onlyJsonObjects" Nothing resp
70+ Just (_ :: Object) -> return ())
6571
6672-- | __Optional__
6773--
@@ -82,25 +88,24 @@ onlyJsonObjects
8288-- * Location header: <https://tools.ietf.org/html/rfc7231#section-7.1.2 RFC 7231 Section 7.1.2>
8389--
8490-- /Since 0.0.0.0/
85−createContainsValidLocation :: RequestPredicate Text Bool
91+createContainsValidLocation :: RequestPredicate
8692createContainsValidLocation
87− = RequestPredicate
88− { reqPredName = "createContainsValidLocation"
89− , reqResps = \req mgr -> do
90− resp <- httpLbs req mgr
91− if responseStatus resp == status201
92− then case lookup "Location" $ responseHeaders resp of
93− Nothing -> return (False, [resp])
94− Just l -> case parseUrl $ SBSC.unpack l of
95− Nothing -> return (False, [resp])
96− Just x -> do
97− resp2 <- httpLbs x mgr
98− return (status2XX resp2, [resp, resp2])
99− else return (True, [resp])
100− }
93+ = RequestPredicate $ \req mgr -> do
94+ let n = "createContainsValidLocation"
95+ resp <- httpLbs req mgr
96+ if responseStatus resp == status201
97+ then case lookup "Location" $ responseHeaders resp of
98+ Nothing -> fail n
99+ Just l -> case parseUrl $ SBSC.unpack l of
100+ Nothing -> fail n
101+ Just x -> do
102+ resp2 <- httpLbs x mgr
103+ status2XX resp2 n
104+ return [resp, resp2]
105+ else return [resp]
101106
102107{-
103−getsHaveLastModifiedHeader :: ResponsePredicate Text Bool
108+getsHaveLastModifiedHeader :: ResponsePredicate
104109getsHaveLastModifiedHeader
105110 = ResponsePredicate "getsHaveLastModifiedHeader" (\resp ->
106111
@@ -122,18 +127,17 @@ getsHaveLastModifiedHeader
122127-- * Status 405: <https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html RFC 2616 Section 10.4.6>
123128--
124129-- /Since 0.0.0.0/
125−notAllowedContainsAllowHeader :: RequestPredicate Text Bool
130+notAllowedContainsAllowHeader :: RequestPredicate
126131notAllowedContainsAllowHeader
127− = RequestPredicate
128− { reqPredName = "notAllowedContainsAllowHeader"
129− , reqResps = \req mgr -> do
130− resp <- mapM (flip httpLbs mgr) $ [ req { method = renderStdMethod m }
131− | m <- [minBound .. maxBound ]
132− , renderStdMethod m /= method req ]
133− return (all pred' resp, resp)
134− }
132+ = RequestPredicate $ \req mgr -> do
133+ resp <- mapM (flip httpLbs mgr) $ [ req { method = renderStdMethod m }
134+ | m <- [minBound .. maxBound ]
135+ , renderStdMethod m /= method req ]
136+ case filter pred' resp of
137+ (x:xs) -> throw $ PredicateFailure "notAllowedContainsAllowHeader" (Just req) x
138+ [] -> return resp
135139 where
136− pred' resp = responseStatus resp /= status405 || hasValidHeader "Allow" go resp
140+ pred' resp = responseStatus resp == status405 && not (hasValidHeader "Allow" go resp)
137141 where
138142 go x = all (\y -> isRight $ parseMethod $ SBSC.pack y)
139143 $ wordsBy (`elem` (", " :: [Char])) (SBSC.unpack x)
@@ -154,19 +158,19 @@ notAllowedContainsAllowHeader
154158-- * @Accept@ header: <https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html RFC 2616 Section 14.1>
155159--
156160-- /Since 0.0.0.0/
157−honoursAcceptHeader :: RequestPredicate Text Bool
161+honoursAcceptHeader :: RequestPredicate
158162honoursAcceptHeader
159− = RequestPredicate
160− { reqPredName = "honoursAcceptHeader"
161− , reqResps = \req mgr -> do
162− resp <- httpLbs req mgr
163− let scode = responseStatus resp
164− sctype = lookup "Content-Type" $ responseHeaders resp
165− sacc = fromMaybe "*/*" $ lookup "Accept" (requestHeaders req)
166− if status100 < scode && scode < status300
167− then return (isJust $ sctype >>= \x -> matchAccept [x] sacc, [resp])
168− else return (True, [resp])
169− }
163+ = RequestPredicate $ \req mgr -> do
164+ resp <- httpLbs req mgr
165+ let scode = responseStatus resp
166+ sctype = lookup "Content-Type" $ responseHeaders resp
167+ sacc = fromMaybe "*/*" $ lookup "Accept" (requestHeaders req)
168+ if status100 < scode && scode < status300
169+ then if isJust $ sctype >>= \x -> matchAccept [x] sacc
170+ then fail "honoursAcceptHeader"
171+ else return [resp]
172+ else return [resp]
173+
170174
171175-- | [__Best Practice__]
172176--
@@ -182,34 +186,32 @@ honoursAcceptHeader
182186-- * @Cache-Control@ header: <https://tools.ietf.org/html/rfc7234#section-5.2 RFC 7234 Section 5.2>
183187--
184188-- /Since 0.0.0.0/
185−getsHaveCacheControlHeader :: RequestPredicate Text Bool
189+getsHaveCacheControlHeader :: RequestPredicate
186190getsHaveCacheControlHeader
187− = RequestPredicate
188− { reqPredName = "getsHaveCacheControlHeader"
189− , reqResps = \req mgr -> if method req == methodGet
190− then do
191− resp <- httpLbs req mgr
192− let good = isJust $ lookup "Cache-Control" $ responseHeaders resp
193− return (good, [resp])
194− else return (True, [])
195− }
191+ = RequestPredicate $ \req mgr ->
192+ if (method req == methodGet)
193+ then do
194+ resp <- httpLbs req mgr
195+ unless (hasValidHeader "Cache-Control" (const True) resp) $ do
196+ throw $ PredicateFailure "getsHaveCacheControlHeader" (Just req) resp
197+ return [resp]
198+ else return []
196199
197200-- | [__Best Practice__]
198201--
199202-- Like 'getsHaveCacheControlHeader', but for @HEAD@ requests.
200203--
201204-- /Since 0.0.0.0/
202−headsHaveCacheControlHeader :: RequestPredicate Text Bool
205+headsHaveCacheControlHeader :: RequestPredicate
203206headsHaveCacheControlHeader
204− = RequestPredicate
205− { reqPredName = "headsHaveCacheControlHeader"
206− , reqResps = \req mgr -> if method req == methodHead
207− then do
208− resp <- httpLbs req mgr
209− let good = hasValidHeader "Cache-Control" (const True) resp
210− return (good, [resp])
211− else return (True, [])
212− }
207+ = RequestPredicate $ \req mgr ->
208+ if (method req == methodHead)
209+ then do
210+ resp <- httpLbs req mgr
211+ unless (hasValidHeader "Cache-Control" (const True) resp) $
212+ throw $ PredicateFailure "headsHaveCacheControlHeader" (Just req) resp
213+ return [resp]
214+ else return []
213215{-
214216-- |
215217--
@@ -271,12 +273,13 @@ linkHeadersAreValid
271273-- * @WWW-Authenticate@ header: <https://tools.ietf.org/html/rfc7235#section-4.1 RFC 7235 Section 4.1>
272274--
273275-- /Since 0.0.0.0/
274−unauthorizedContainsWWWAuthenticate :: ResponsePredicate Text Bool
276+unauthorizedContainsWWWAuthenticate :: ResponsePredicate
275277unauthorizedContainsWWWAuthenticate
276− = ResponsePredicate "unauthorizedContainsWWWAuthenticate" (\resp ->
278+ = ResponsePredicate $ \resp ->
277279 if responseStatus resp == status401
278− then hasValidHeader "WWW-Authenticate" (const True) resp
279− else True)
280+ then unless (hasValidHeader "WWW-Authenticate" (const True) resp) $
281+ fail "unauthorizedContainsWWWAuthenticate"
282+ else return ()
280283
281284-- * Predicate logic
282285
@@ -289,67 +292,46 @@ unauthorizedContainsWWWAuthenticate
289292-- | A predicate that depends only on the response.
290293--
291294-- /Since 0.0.0.0/
292−data ResponsePredicate n r = ResponsePredicate
293− { respPredName :: n
294− , respPred :: Response LBS.ByteString -> r
295− } deriving (Functor, Generic)
296−
297−instance Bifunctor ResponsePredicate where
298− first f (ResponsePredicate a b) = ResponsePredicate (f a) b
299− second = fmap
300−
301−instance (Monoid n, Monoid r) => Monoid (ResponsePredicate n r) where
302− mempty = ResponsePredicate mempty mempty
303− a `mappend` b = ResponsePredicate
304− { respPredName = respPredName a <> respPredName b
305− , respPred = respPred a <> respPred b
306− }
295+data ResponsePredicate = ResponsePredicate
296+ { getResponsePredicate :: Response LBS.ByteString -> IO ()
297+ } deriving (Generic)
298+
299+instance Monoid ResponsePredicate where
300+ mempty = ResponsePredicate $ const $ return ()
301+ ResponsePredicate a `mappend` ResponsePredicate b = ResponsePredicate $ \x -> a x >> b x
307302
308303-- | A predicate that depends on both the request and the response.
309304--
310305-- /Since 0.0.0.0/
311−data RequestPredicate n r = RequestPredicate
312− { reqPredName :: n
313− , reqResps :: Request -> Manager -> IO (r, [Response LBS.ByteString])
314− } deriving (Generic, Functor)
315−
316−instance Bifunctor RequestPredicate where
317− first f (RequestPredicate a b) = RequestPredicate (f a) b
318− second = fmap
306+data RequestPredicate = RequestPredicate
307+ { getRequestPredicate :: Request -> Manager -> IO [Response LBS.ByteString]
308+ } deriving (Generic)
319309
320310-- TODO: This isn't actually a monoid
321−instance (Monoid n, Monoid r) => Monoid (RequestPredicate n r) where
322− mempty = RequestPredicate mempty (\r m -> httpLbs r m >>= \x -> return (mempty, [x]))
323− a `mappend` b = RequestPredicate
324− { reqPredName = reqPredName a <> reqPredName b
325− , reqResps = \x m -> liftM2 (<>) (reqResps a x m) (reqResps b x m)
326− }
311+instance Monoid RequestPredicate where
312+ mempty = RequestPredicate (\r m -> httpLbs r m >>= \x -> return ([x]))
313+ RequestPredicate a `mappend` RequestPredicate b = RequestPredicate $ \r mgr ->
314+ liftM2 (<>) (a r mgr) (b r mgr)
327315
328316-- | A set of predicates. Construct one with 'mempty' and '<%>'.
329−data Predicates n r = Predicates
330− { reqPreds :: RequestPredicate n r
331− , respPreds :: ResponsePredicate n r
332− } deriving (Generic, Functor)
317+data Predicates = Predicates
318+ { requestPredicates :: RequestPredicate
319+ , responsePredicates :: ResponsePredicate
320+ } deriving (Generic)
333321
334−instance (Monoid n, Monoid r) => Monoid (Predicates n r) where
322+instance Monoid Predicates where
335323 mempty = Predicates mempty mempty
336− a `mappend` b = Predicates (reqPreds a <> reqPreds b) (respPreds a <> respPreds b)
337−
338−
324+ a `mappend` b = Predicates (requestPredicates a <> requestPredicates b)
325+ (responsePredicates a <> responsePredicates b)
339326
340327class JoinPreds a where
341− joinPreds :: a -> Predicates [Text] [Text] -> Predicates [Text] [Text]
342−
343−instance JoinPreds (RequestPredicate Text Bool) where
344− joinPreds p (Predicates x y) = Predicates (go <> x) y
345− where go = let p' = first return p
346− in fmap (\z -> if z then [] else reqPredName p') p'
328+ joinPreds :: a -> Predicates -> Predicates
347329
348−instance JoinPreds (ResponsePredicate Text Bool) where
349− joinPreds p (Predicates x y) = Predicates x (go <> y)
350− where go = let p' = first return p
351− in fmap (\z -> if z then [] else respPredName p') p'
330+instance JoinPreds (RequestPredicate ) where
331+ joinPreds p (Predicates x y) = Predicates (p <> x) y
352332
333+instance JoinPreds (ResponsePredicate ) where
334+ joinPreds p (Predicates x y) = Predicates x (p <> y)
353335
354336-- | Adds a new predicate (either `ResponsePredicate` or `RequestPredicate`) to
355337-- the existing predicates.
@@ -357,14 +339,17 @@ instance JoinPreds (ResponsePredicate Text Bool) where
357339-- > not500 <%> onlyJsonObjects <%> empty
358340--
359341-- /Since 0.0.0.0/
360−(<%>) :: JoinPreds a => a -> Predicates [Text] [Text] -> Predicates [Text] [Text]
342+(<%>) :: JoinPreds a => a -> Predicates -> Predicates
361343(<%>) = joinPreds
362344infixr 6 <%>
363345
364−finishPredicates :: Predicates [Text] [Text] -> Request -> Manager -> IO [Text]
365−finishPredicates p req mgr = do
366− (soFar, resps) <- reqResps (reqPreds p) req mgr
367− return $ soFar <> mconcat [respPred (respPreds p) r | r <- resps]
346+finishPredicates :: Predicates -> Request -> Manager -> IO (Maybe PredicateFailure)
347+finishPredicates p req mgr = go `catch` \(e :: PredicateFailure) -> return $ Just e
348+ where
349+ go = do
350+ resps <- getRequestPredicate (requestPredicates p) req mgr
351+ mapM_ (getResponsePredicate $ responsePredicates p) resps
352+ return Nothing
368353
369354-- * helpers
370355
@@ -373,5 +358,8 @@ hasValidHeader hdr p r = case lookup (mk hdr) (responseHeaders r) of
373358 Nothing -> False
374359 Just v -> p v
375360
376−status2XX :: Response b -> Bool
377−status2XX r = status200 <= responseStatus r && responseStatus r < status300
361+status2XX :: Monad m => Response b -> String -> m ()
362+status2XX r t
363+ | status200 <= responseStatus r && responseStatus r < status300
364+ = return ()
365+ | otherwise = fail t