git.iko.soy/servant-quickcheck
on master help sign in
Add new predicates.

* notAllowedContainsAllowHeader * getsHaveCacheControlHeader * headsHaveCacheControlHeader

commit9307ea21584067e8fafb4451ca3247ec3f549270 authorJulian K. Arni <jkarni@gmail.com> dateMon 25 Apr 2016, 12:32 (11 years ago) parentb5ee965
4 files changed+94−29
src/Servant/QuickCheck/Internal/HasGenRequest.hs+4 src/Servant/QuickCheck/Internal/Predicates.hs+67−20 src/Servant/QuickCheck/Internal/QuickCheck.hs+8−2 test/Servant/QuickCheck/InternalSpec.hs+15−7
src/Servant/QuickCheck/Internal/HasGenRequest.hs+4
@@ -130,3 +130,7 @@ instance (HasGenRequest a) => HasGenRequest (Vault :> a) where
130130
131131instance (HasGenRequest a) => HasGenRequest (WithNamedContext x y a) where
132132 genRequest _ = genRequest (Proxy :: Proxy a)
133+
134+-- TODO: Try logging in
135+instance (HasGenRequest a) => HasGenRequest (BasicAuth x y :> a) where
136+ genRequest _ = genRequest (Proxy :: Proxy a)
src/Servant/QuickCheck/Internal/Predicates.hs+67−20
@@ -87,15 +87,26 @@ getsHaveLastModifiedHeader
8787-- __References__:
8888--
8989-- * @Allow@ header: <https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html RFC 2616 Section 14.7>
90−notAllowedContainsAllowHeader :: ResponsePredicate Text Bool
90+-- * Status 405: <https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html RFC 2616 Section 10.4.6>
91+notAllowedContainsAllowHeader :: RequestPredicate Text Bool
9192notAllowedContainsAllowHeader
92− = ResponsePredicate "notAllowedContainsAllowHeader" (\resp ->
93− if responseStatus resp == status405
94− then hasValidHeader "Allow" go resp
95− else True)
96− where
97− go x = all (\y -> isRight $ parseMethod $ SBSC.pack y)
98− $ wordsBy (`elem` (", " :: [Char])) (SBSC.unpack x)
93+ = RequestPredicate
94+ { reqPredName = name
95+ , reqResps = \req mgr -> mapM (flip httpLbs mgr)
96+ [ req { method = renderStdMethod m }
97+ | m <- [minBound .. maxBound ]
98+ , renderStdMethod m /= method req ]
99+ , reqPred = pred'
100+ }
101+ where
102+ name = "notAllowedContainsAllowHeader"
103+ pred' = ResponsePredicate name (\resp ->
104+ if responseStatus resp == status405
105+ then hasValidHeader "Allow" go resp
106+ else True)
107+ where
108+ go x = all (\y -> isRight $ parseMethod $ SBSC.pack y)
109+ $ wordsBy (`elem` (", " :: [Char])) (SBSC.unpack x)
99110
100111 {-
101112-- | When a request contains an @Accept@ header, the server must either return
@@ -104,28 +115,64 @@ notAllowedContainsAllowHeader
104115--
105116-- This function checks that every *successful* response has a @Content-Type@
106117-- header that matches the @Accept@ header.
107−honoursAcceptHeader :: Predicate b Bool
118+--
119+-- __References__:
120+--
121+-- * @Accept@ header: <https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html RFC 2616 Section 14.1>
122+honoursAcceptHeader :: RequestPredicate b Bool
108123honoursAcceptHeader
109− = RequestPredicate "honoursAcceptHeader" _
124+ = RequestPredicate name (ResponsePredicate name $ \req mgr -> do
125+
126+ resp <- httpLbs req mgr
127+ let scode = responseStatus resp
128+ sctype = maybeToList $ lookup "Content-Type" $ responseHeaders resp
129+ sacc = fromMaybe "*/*" $ lookup "Accept" $ requestHeaders req
130+ if 100 < scode && scode < 300
131+ then isJust matchAccept sacc sctype
132+ else True)
133+ where name = "honoursAcceptHeader"
110134
111−-- | Whether or not a representation should be cached, it is good practice to
135+-}
136+-- | [__Best Practice__]
137+--
138+-- Whether or not a representation should be cached, it is good practice to
112139-- have a @Cache-Control@ header for @GET@ requests. If the representation
113140-- should not be cached, used @Cache-Control: no-cache@.
114141--
115−-- This function checks that @GET@ responses have a valid @Cache-Control@
116−-- header.
142+-- This function checks that @GET@ responses have @Cache-Control@ header.
143+-- It does NOT currently check that the header is valid.
144+--
145+-- __References__:
117146--
118−-- References: RFC 7234 Section 5.2
119−-- https://tools.ietf.org/html/rfc7234#section-5.2
120−getsHaveCacheControlHeader :: Predicate b Bool
147+-- * @Cache-Control@ header: <https://tools.ietf.org/html/rfc7234#section-5.2 RFC 7234 Section 5.2>
148+getsHaveCacheControlHeader :: RequestPredicate Text Bool
121149getsHaveCacheControlHeader
122− = ResponsePredicate "getsHaveCacheControlHeader" _
150+ = RequestPredicate
151+ { reqPredName = name
152+ , reqResps = \req mgr -> if method req == methodGet
153+ then return <$> httpLbs req mgr
154+ else return []
155+ , reqPred = ResponsePredicate name $ \resp ->
156+ isJust $ lookup "Cache-Control" $ responseHeaders resp
157+ }
158+ where name = "getsHaveCacheControlHeader"
123159
124−-- | Like 'getsHaveCacheControlHeader', but for @HEAD@ requests.
125−headsHaveCacheControlHeader :: Predicate b Bool
160+-- | [__Best Practice__]
161+--
162+-- Like 'getsHaveCacheControlHeader', but for @HEAD@ requests.
163+headsHaveCacheControlHeader :: RequestPredicate Text Bool
126164headsHaveCacheControlHeader
127− = ResponsePredicate "headsHaveCacheControlHeader" _
165+ = RequestPredicate
166+ { reqPredName = name
167+ , reqResps = \req mgr -> if method req == methodHead
168+ then return <$> httpLbs req mgr
169+ else return []
170+ , reqPred = ResponsePredicate name $ \resp ->
171+ isJust $ lookup "Cache-Control" $ responseHeaders resp
172+ }
173+ where name = "headsHaveCacheControlHeader"
128174
175+{-
129176-- |
130177--
131178-- If the original request modifies the resource, this function makes two
src/Servant/QuickCheck/Internal/QuickCheck.hs+8−2
@@ -25,8 +25,14 @@ import Servant.QuickCheck.Internal.Equality
2525-- then stop the application.
2626withServantServer :: HasServer a '[] => Proxy a -> IO (Server a)
2727 -> (BaseUrl -> IO r) -> IO r
28−withServantServer api server t
29− = withApplication (return . serve api =<< server) $ \port ->
28+withServantServer api = withServantServerAndContext api EmptyContext
29+
30+-- | Like 'withServantServer', but allows passing in a 'Context' to the
31+-- application.
32+withServantServerAndContext :: HasServer a ctx
33+ => Proxy a -> Context ctx -> IO (Server a) -> (BaseUrl -> IO r) -> IO r
34+withServantServerAndContext api ctx server t
35+ = withApplication (return . serveWithContext api ctx =<< server) $ \port ->
3036 t (BaseUrl Http "localhost" port "")
3137
3238-- | Check that the two servers running under the provided @BaseUrl@s behave
test/Servant/QuickCheck/InternalSpec.hs+15−7
@@ -24,8 +24,8 @@ serversEqualSpec :: Spec
2424serversEqualSpec = describe "serversEqual" $ do
2525
2626 it "considers equal servers equal" $ do
27− withServantServer api server $ \burl1 ->
28− withServantServer api server $ \burl2 -> do
27+ withServantServerAndContext api ctx server $ \burl1 ->
28+ withServantServerAndContext api ctx server $ \burl2 -> do
2929 serversEqual api burl1 burl2 args bodyEquality
3030
3131
@@ -33,12 +33,17 @@ serverSatisfiesSpec :: Spec
3333serverSatisfiesSpec = describe "serverSatisfies" $ do
3434
3535 it "succeeds for true predicates" $ do
36− withServantServer api server $ \burl ->
37− serverSatisfies api burl args (not500 <%> mempty)
36+ withServantServerAndContext api ctx server $ \burl ->
37+ serverSatisfies api burl args (unauthorizedContainsWWWAuthenticate
38+ <%> not500
39+ <%> mempty)
3840
3941 it "fails for false predicates" $ do
40− withServantServer api server $ \burl ->
41− serverSatisfies api burl args (onlyJsonObjects <%> mempty)
42+ withServantServerAndContext api ctx server $ \burl -> do
43+ -- Since this is the negation, and we want to check that all of the
44+ -- predicates fail rather than one or more, we need to separate them out
45+ serverSatisfies api burl args ((not <$> onlyJsonObjects) <%> mempty)
46+ serverSatisfies api burl args ((not <$> getsHaveCacheControlHeader) <%> mempty)
4247
4348isComprehensiveSpec :: Spec
4449isComprehensiveSpec = describe "HasGenRequest" $ do
@@ -54,6 +59,7 @@ isComprehensiveSpec = describe "HasGenRequest" $ do
5459
5560type API = ReqBody '[JSON] String :> Post '[JSON] String
5661 :<|> Get '[JSON] Int
62+ :<|> BasicAuth "some-realm" () :> Get '[JSON] ()
5763
5864api :: Proxy API
5965api = Proxy
@@ -63,8 +69,10 @@ server = do
6369 mvar <- newMVar ""
6470 return $ (\x -> liftIO $ swapMVar mvar x)
6571 :<|> (liftIO $ readMVar mvar >>= return . length)
72+ :<|> (const $ return ())
6673
67−
74+ctx :: Context '[BasicAuthCheck ()]
75+ctx = BasicAuthCheck (const . return $ NoSuchUser) :. EmptyContext
6876------------------------------------------------------------------------------
6977-- Utils
7078------------------------------------------------------------------------------