@@ -1,33 +1,32 @@
11{-# LANGUAGE CPP #-}
22module Servant.QuickCheck.InternalSpec (spec) where
33
4−
5−import Control.Concurrent.MVar (newMVar, readMVar, swapMVar)
6−import Control.Exception (SomeException)
7−import Control.Monad (replicateM)
8−import Control.Monad.IO.Class (liftIO)
9−import qualified Data.ByteString as BS
10−import qualified Data.ByteString.Char8 as C
11−import Data.Maybe (fromJust)
12−import Network.HTTP.Client (path, queryString)
4+import Control.Concurrent.MVar (newMVar, readMVar, swapMVar)
5+import Control.Monad (replicateM)
6+import Control.Monad.IO.Class (liftIO)
7+import qualified Data.ByteString as BS
8+import qualified Data.ByteString.Char8 as C
9+import Data.Maybe (fromJust)
1310import Prelude.Compat
1411import Servant
15−import Servant.HTML.Blaze (HTML)
16−import qualified Text.Blaze.Html as Blaze
17−import qualified Text.Blaze.Html5 as Blaze5
18−import Test.Hspec (Spec, context, describe, it, shouldBe,
19− shouldContain)
20−import Test.Hspec.Core.Spec (Arg, Example, Result (..), ResultStatus (..),
21− defaultParams, safeEvaluateExample)
22−import Test.QuickCheck.Gen (generate, unGen)
23−import Test.QuickCheck.Random (mkQCGen)
24−
25−
26−import Servant.Test.ComprehensiveAPI (comprehensiveAPIWithoutStreamingOrRaw)
12+import Test.Hspec (Spec, context, describe, it, shouldBe,
13+ shouldContain)
14+import Test.Hspec.Core.Spec (Arg, Example, Result (..),
15+ defaultParams, evaluateExample)
16+import Test.QuickCheck.Gen (unGen, generate)
17+import Test.QuickCheck.Random (mkQCGen)
18+import Network.HTTP.Client (queryString, path)
19+
20+#if MIN_VERSION_servant(0,8,0)
21+import Servant.API.Internal.Test.ComprehensiveAPI (comprehensiveAPIWithoutRaw)
22+#else
23+import Servant.API.Internal.Test.ComprehensiveAPI (ComprehensiveAPI,
24+ comprehensiveAPI)
25+#endif
2726
2827import Servant.QuickCheck
29−import Servant.QuickCheck.Internal (genRequest, runGenRequest,
30− serverDoesntSatisfy)
28+import Servant.QuickCheck.Internal (genRequest, runGenRequest, serverDoesntSatisfy)
29+
3130
3231spec :: Spec
3332spec = do
@@ -39,7 +38,6 @@ spec = do
3938 queryParamsSpec
4039 queryFlagsSpec
4140 deepPathSpec
42− htmlDocTypesSpec
4341 unbiasedGenerationSpec
4442
4543serversEqualSpec :: Spec
@@ -52,42 +50,13 @@ serversEqualSpec = describe "serversEqual" $ do
5250
5351 context "when servers are not equal" $ do
5452 it "provides the failing responses in the error message" $ do
55− FailedWith err <- withServantServer api2 server2 $ \burl1 ->
53+ Fail _ err <- withServantServer api2 server2 $ \burl1 ->
5654 withServantServer api2 server3 $ \burl2 -> do
5755 evalExample $ serversEqual api2 burl1 burl2 args bodyEquality
58− show err `shouldContain` "Server equality failed"
5956 show err `shouldContain` "Body: 1"
6057 show err `shouldContain` "Body: 2"
6158 show err `shouldContain` "Path: /failplz"
6259
63− context "when JSON is equal but looks a bit different as a ByteString" $ do
64−
65− it "sanity check: different whitespace same JSON objects bodyEquality fails" $ do
66− FailedWith err <- withServantServer jsonApi jsonServer1 $ \burl1 ->
67− withServantServer jsonApi jsonServer2 $ \burl2 -> do
68− evalExample $ serversEqual jsonApi burl1 burl2 args bodyEquality
69− show err `shouldContain` "Server equality failed"
70−
71− it "jsonEquality considers equal JSON apis equal regardless of key ordering or whitespace" $ do
72− withServantServerAndContext jsonApi ctx jsonServer1 $ \burl1 ->
73− withServantServerAndContext jsonApi ctx jsonServer2 $ \burl2 ->
74− serversEqual jsonApi burl1 burl2 args jsonEquality
75−
76− it "sees when JSON apis are not equal because any value is different" $ do
77− FailedWith err <- withServantServer jsonApi jsonServer2 $ \burl1 ->
78− withServantServer jsonApi jsonServer3 $ \burl2 -> do
79− evalExample $ serversEqual jsonApi burl1 burl2 args jsonEquality
80− show err `shouldContain` "Server equality failed"
81− show err `shouldContain` "Path: /jsonComparison"
82−
83− it "sees when JSON apis are not equal due to different keys but same values" $ do
84− FailedWith err <- withServantServer jsonApi jsonServer2 $ \burl1 ->
85− withServantServer jsonApi jsonServer4 $ \burl2 -> do
86− evalExample $ serversEqual jsonApi burl1 burl2 args jsonEquality
87− show err `shouldContain` "Server equality failed"
88− show err `shouldContain` "Path: /jsonComparison"
89−
90−
9160serverSatisfiesSpec :: Spec
9261serverSatisfiesSpec = describe "serverSatisfies" $ do
9362
@@ -108,38 +77,34 @@ serverSatisfiesSpec = describe "serverSatisfies" $ do
10877 context "when predicates are false" $ do
10978
11079 it "fails with informative error messages" $ do
111− FailedWith err <- withServantServerAndContext api ctx server $ \burl -> do
80+ Fail _ err <- withServantServerAndContext api ctx server $ \burl -> do
11281 evalExample $ serverSatisfies api burl args (notAllowedContainsAllowHeader <%> mempty)
113− show err `shouldContain` "notAllowedContainsAllowHeader"
114− show err `shouldContain` "Headers"
115− show err `shouldContain` "Body"
82+ err `shouldContain` "notAllowedContainsAllowHeader"
83+ err `shouldContain` "Headers"
84+ err `shouldContain` "Body"
11685
11786
11887onlyJsonObjectSpec :: Spec
11988onlyJsonObjectSpec = describe "onlyJsonObjects" $ do
12089
12190 it "fails correctly" $ do
122− FailedWith err <- withServantServerAndContext api ctx server $ \burl -> do
91+ Fail _ err <- withServantServerAndContext api ctx server $ \burl -> do
12392 evalExample $ serverSatisfies (Proxy :: Proxy (Get '[JSON] Int)) burl args
12493 (onlyJsonObjects <%> mempty)
125− show err `shouldContain` "onlyJsonObjects"
94+ err `shouldContain` "onlyJsonObjects"
12695
12796 it "accepts non-JSON endpoints" $ do
12897 withServantServerAndContext octetAPI ctx serverOctetAPI $ \burl ->
12998 serverSatisfies octetAPI burl args (onlyJsonObjects <%> mempty)
13099
131− it "does not fail when there is no content-type" $ do
132− withServantServerAndContext api2 ctx serverFailing $ \burl ->
133− serverSatisfies api2 burl args (onlyJsonObjects <%> mempty)
134−
135100notLongerThanSpec :: Spec
136101notLongerThanSpec = describe "notLongerThan" $ do
137102
138103 it "fails correctly" $ do
139− FailedWith err <- withServantServerAndContext api ctx server $ \burl -> do
104+ Fail _ err <- withServantServerAndContext api ctx server $ \burl -> do
140105 evalExample $ serverSatisfies (Proxy :: Proxy (Get '[JSON] Int)) burl args
141106 (notLongerThan 1 <%> mempty)
142− show err `shouldContain` "notLongerThan"
107+ err `shouldContain` "notLongerThan"
143108
144109 it "succeeds correctly" $ do
145110 withServantServerAndContext api ctx server $ \burl ->
@@ -149,7 +114,7 @@ isComprehensiveSpec :: Spec
149114isComprehensiveSpec = describe "HasGenRequest" $ do
150115
151116 it "has instances for all 'servant' combinators" $ do
152− let _g = genRequest comprehensiveAPIWithoutStreamingOrRaw
117+ let _g = genRequest comprehensiveAPIWithoutRaw
153118 True `shouldBe` True -- This is a type-level check
154119
155120deepPathSpec :: Spec
@@ -185,25 +150,6 @@ queryFlagsSpec = describe "QueryFlags" $ do
185150 qs = C.unpack $ queryString req
186151 qs `shouldBe` "one&two"
187152
188−htmlDocTypesSpec :: Spec
189−htmlDocTypesSpec = describe "HtmlDocTypes" $ do
190−
191− it "fails HTML without doctype correctly" $ do
192− err <- withServantServerAndContext docTypeApi ctx noDocTypeServer $ \burl -> do
193− evalExample $ serverSatisfies docTypeApi burl args
194− (htmlIncludesDoctype <%> mempty)
195− show err `shouldContain` "htmlIncludesDoctype"
196−
197− it "passes HTML with a doctype at start" $ do
198− withServantServerAndContext docTypeApi ctx docTypeServer $ \burl ->
199− serverSatisfies docTypeApi burl args (htmlIncludesDoctype <%> mempty)
200−
201− it "accepts json endpoints and passes over them in silence" $ do
202− withServantServerAndContext api ctx server $ \burl -> do
203− serverSatisfies (Proxy :: Proxy (Get '[JSON] Int)) burl args
204− (htmlIncludesDoctype <%> mempty)
205−
206−
207153makeRandomRequest :: Proxy LargeAPI -> BaseUrl -> IO Integer
208154makeRandomRequest large burl = do
209155 req <- generate $ runGenRequest large
@@ -220,8 +166,8 @@ unbiasedGenerationSpec = describe "Unbiased Generation of requests" $
220166 let mean = (sum $ map fromIntegral someRequests) / runs
221167 let variancer x = let ix = fromIntegral x in (ix - mean) * (ix - mean)
222168 let variance = (sum $ map variancer someRequests) / runs - 1
223− -- mean should be around 8.5. If this fails, we likely need more runs (or there's a bug!)
224− mean > 8 `shouldBe` True
169+ -- mean should be around 8
170+ mean > 7 `shouldBe` True
225171 mean < 9 `shouldBe` True
226172 -- Std dev is likely around 4. Variance is probably greater than 20.
227173 variance > 19.5 `shouldBe` True
@@ -273,23 +219,7 @@ server2 = return $ return 1
273219server3 :: IO (Server API2)
274220server3 = return $ return 2
275221
276−serverFailing :: IO (Server API2)
277−serverFailing = return . throwError $ err405
278222
279−-- With Doctypes
280−type HtmlDoctype = Get '[HTML] Blaze.Html
281−
282−docTypeApi :: Proxy HtmlDoctype
283−docTypeApi = Proxy
284−
285−docTypeServer :: IO (Server HtmlDoctype)
286−docTypeServer = pure $ pure $ Blaze5.docTypeHtml $ Blaze5.span "Hello Test!"
287−
288−noDocTypeServer :: IO (Server HtmlDoctype)
289−noDocTypeServer = pure $ pure $ Blaze.text "Hello Test!"
290−
291−
292−-- Api for unbiased generation of requests tests
293223largeApi :: Proxy LargeAPI
294224largeApi = Proxy
295225
@@ -320,46 +250,17 @@ octetAPI = Proxy
320250serverOctetAPI :: IO (Server OctetAPI)
321251serverOctetAPI = return $ return "blah"
322252
323−type JsonApi = "jsonComparison" :> Get '[OctetStream] BS.ByteString
324−
325−jsonApi :: Proxy JsonApi
326−jsonApi = Proxy
327−
328−jsonServer1 :: IO (Server JsonApi)
329−jsonServer1 = return $ return "{ \"b\": [\"b\"], \"a\": 1 }" -- whitespace, ordering different
330−
331−jsonServer2 :: IO (Server JsonApi)
332−jsonServer2 = return $ return "{\"a\": 1,\"b\":[\"b\"]}"
333−
334−jsonServer3 :: IO (Server JsonApi)
335−jsonServer3 = return $ return "{\"a\": 2, \"b\": [\"b\"]}"
336−
337−jsonServer4 :: IO (Server JsonApi)
338−jsonServer4 = return $ return "{\"c\": 1, \"d\": [\"b\"]}"
339−
340−
341253ctx :: Context '[BasicAuthCheck ()]
342254ctx = BasicAuthCheck (const . return $ NoSuchUser) :. EmptyContext
343255------------------------------------------------------------------------------
344256-- Utils
345257------------------------------------------------------------------------------
346−evalExample :: (Example e, Arg e ~ ()) => e -> IO EvalResult
347−evalExample e = do
348− r <- safeEvaluateExample e defaultParams ($ ()) progCallback
349− case resultStatus r of
350− Success -> return $ AllGood
351− Failure _ reason -> return $ FailedWith $ show reason
352− Pending {} -> error "should not happen"
258+
259+evalExample :: (Example e, Arg e ~ ()) => e -> IO Result
260+evalExample e = evaluateExample e defaultParams ($ ()) progCallback
353261 where
354262 progCallback _ = return ()
355263
356−data EvalResult
357− = AnException SomeException
358− | AllGood
359− | FailedWith String
360− deriving (Show)
361−
362−
363264args :: Args
364265args = defaultArgs { maxSuccess = noOfTestCases }
365266
@@ -369,3 +270,8 @@ noOfTestCases = 20000
369270#else
370271noOfTestCases = 1000
371272#endif
273+
274+#if !MIN_VERSION_servant(0,8,0)
275+comprehensiveAPIWithoutRaw :: Proxy ComprehensiveAPI
276+comprehensiveAPIWithoutRaw = comprehensiveAPI
277+#endif