@@ -1,19 +1,20 @@
11-- | This module contains wrappers around lower-level functionality.
22module Servant.QuickCheck.Internal.QuickCheck where
33
4+import qualified Data.ByteString.Lazy as LBS
45import Data.Proxy (Proxy)
5−import Network.HTTP.Client (Manager, defaultManagerSettings,
6− newManager, httpLbs, checkStatus, Request)
6+import Data.Text (Text)
7+import Network.HTTP.Client (Manager, Request, checkStatus,
8+ defaultManagerSettings, httpLbs,
9+ newManager)
710import Network.Wai.Handler.Warp (withApplication)
811import Servant (HasServer, Server, serve)
9−import Servant.Client (BaseUrl (..), Scheme (..) )
12+import Servant.Client (BaseUrl (..), Scheme (..))
13+import System.IO.Unsafe (unsafePerformIO)
1014import Test.Hspec (Expectation, expectationFailure)
1115import Test.QuickCheck (Args (..), Result (..),
1216 quickCheckWithResult)
13−import System.IO.Unsafe (unsafePerformIO)
14−import Test.QuickCheck.Monadic
15−import qualified Data.ByteString.Lazy as LBS
16−import Data.Text (Text)
17+import Test.QuickCheck.Monadic
1718
1819import Servant.QuickCheck.Internal.HasGenRequest
1920import Servant.QuickCheck.Internal.Predicates
@@ -54,13 +55,29 @@ serversEqual api burl1 burl2 args req = do
5455 NoExpectedFailure {} -> expectationFailure $ "No expected failure"
5556 InsufficientCoverage {} -> expectationFailure $ "Insufficient coverage"
5657
58+-- | Check that a server satisfies the set of properties specified.
59+--
60+-- Note that, rather than having separate tests for each property you'd like to
61+-- test, you should generally prefer to combine all properties into a single
62+-- test. This enables a more parsimonious generation of requests and responses
63+-- with the same testing depth.
64+--
65+-- Example usage:
66+--
67+-- > goodAPISpec = describe "my server" $ do
68+-- >
69+-- > it "follows best practices" $ do
70+-- > withServantServer api server $ \burl ->
71+-- > serverSatisfies api burl stdArgs (not500
72+-- > <%> onlyJsonObjects
73+-- > <%> notAllowedContainsAllowHeader
74+-- > <%> mempty)
5775serverSatisfies :: (HasGenRequest a) =>
5876 Proxy a -> BaseUrl -> Args -> Predicates [Text] [Text] -> Expectation
5977serverSatisfies api burl args preds = do
6078 let reqs = ($ burl) <$> genRequest api
6179 r <- quickCheckWithResult args $ monadicIO $ forAllM reqs $ \req -> do
6280 v <- run $ finishPredicates preds (noCheckStatus req) defManager
63− {-run $ print v-}
6481 assert $ null v
6582 case r of
6683 Success {} -> return ()