@@ -1,33 +1,30 @@
11{-# LANGUAGE CPP #-}
22module Servant.QuickCheck.InternalSpec (spec) where
33
4−
54import Control.Concurrent.MVar (newMVar, readMVar, swapMVar)
6−import Control.Exception (SomeException)
7−import Control.Monad (replicateM)
5+import Control.Exception
86import Control.Monad.IO.Class (liftIO)
97import qualified Data.ByteString as BS
108import qualified Data.ByteString.Char8 as C
11−import Data.Maybe (fromJust)
12−import Network.HTTP.Client (path, queryString)
139import Prelude.Compat
1410import Servant
15−import Servant.HTML.Blaze (HTML)
16−import qualified Text.Blaze.Html as Blaze
17−import qualified Text.Blaze.Html5 as Blaze5
1811import Test.Hspec (Spec, context, describe, it, shouldBe,
1912 shouldContain)
20−import Test.Hspec.Core.Spec (Arg, Example, Result (..), ResultStatus (..),
21− defaultParams, safeEvaluateExample)
22−import Test.QuickCheck.Gen (generate, unGen)
13+import Test.Hspec.Core.Spec (Arg, Example, Result (..), FailureReason (..),
14+ defaultParams, evaluateExample, safeEvaluateExample)
15+import Test.QuickCheck.Gen (unGen)
2316import Test.QuickCheck.Random (mkQCGen)
17+import Network.HTTP.Client (queryString, path)
2418
25−
26−import Servant.Test.ComprehensiveAPI (comprehensiveAPIWithoutStreamingOrRaw)
19+#if MIN_VERSION_servant(0,8,0)
20+import Servant.API.Internal.Test.ComprehensiveAPI (comprehensiveAPIWithoutRaw)
21+#else
22+import Servant.API.Internal.Test.ComprehensiveAPI (ComprehensiveAPI,
23+ comprehensiveAPI)
24+#endif
2725
2826import Servant.QuickCheck
29−import Servant.QuickCheck.Internal (genRequest, runGenRequest,
30− serverDoesntSatisfy)
27+import Servant.QuickCheck.Internal (genRequest, serverDoesntSatisfy)
3128
3229spec :: Spec
3330spec = do
@@ -39,8 +36,6 @@ spec = do
3936 queryParamsSpec
4037 queryFlagsSpec
4138 deepPathSpec
42− htmlDocTypesSpec
43− unbiasedGenerationSpec
4439
4540serversEqualSpec :: Spec
4641serversEqualSpec = describe "serversEqual" $ do
@@ -51,43 +46,16 @@ serversEqualSpec = describe "serversEqual" $ do
5146 serversEqual api burl1 burl2 args bodyEquality
5247
5348 context "when servers are not equal" $ do
49+
5450 it "provides the failing responses in the error message" $ do
55− FailedWith err <- withServantServer api2 server2 $ \burl1 ->
51+ Right (Failure _ err) <- withServantServer api2 server2 $ \burl1 ->
5652 withServantServer api2 server3 $ \burl2 -> do
57− evalExample $ serversEqual api2 burl1 burl2 args bodyEquality
53+ safeEvalExample $ serversEqual api2 burl1 burl2 args bodyEquality
5854 show err `shouldContain` "Server equality failed"
5955 show err `shouldContain` "Body: 1"
6056 show err `shouldContain` "Body: 2"
6157 show err `shouldContain` "Path: /failplz"
6258
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−
9159serverSatisfiesSpec :: Spec
9260serverSatisfiesSpec = describe "serverSatisfies" $ do
9361
@@ -108,19 +76,18 @@ serverSatisfiesSpec = describe "serverSatisfies" $ do
10876 context "when predicates are false" $ do
10977
11078 it "fails with informative error messages" $ do
111− FailedWith err <- withServantServerAndContext api ctx server $ \burl -> do
112− evalExample $ serverSatisfies api burl args (notAllowedContainsAllowHeader <%> mempty)
113− show err `shouldContain` "notAllowedContainsAllowHeader"
79+ Right (Failure _ err) <- withServantServerAndContext api ctx server $ \burl -> do
80+ safeEvalExample $ serverSatisfies api burl args (getsHaveCacheControlHeader <%> mempty)
81+ show err `shouldContain` "getsHaveCacheControlHeader"
11482 show err `shouldContain` "Headers"
11583 show err `shouldContain` "Body"
11684
117−
11885onlyJsonObjectSpec :: Spec
11986onlyJsonObjectSpec = describe "onlyJsonObjects" $ do
12087
12188 it "fails correctly" $ do
122− FailedWith err <- withServantServerAndContext api ctx server $ \burl -> do
123− evalExample $ serverSatisfies (Proxy :: Proxy (Get '[JSON] Int)) burl args
89+ Right (Failure _ err) <- withServantServerAndContext api ctx server $ \burl -> do
90+ safeEvalExample $ serverSatisfies (Proxy :: Proxy (Get '[JSON] Int)) burl args
12491 (onlyJsonObjects <%> mempty)
12592 show err `shouldContain` "onlyJsonObjects"
12693
@@ -128,16 +95,12 @@ onlyJsonObjectSpec = describe "onlyJsonObjects" $ do
12895 withServantServerAndContext octetAPI ctx serverOctetAPI $ \burl ->
12996 serverSatisfies octetAPI burl args (onlyJsonObjects <%> mempty)
13097
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−
13598notLongerThanSpec :: Spec
13699notLongerThanSpec = describe "notLongerThan" $ do
137100
138101 it "fails correctly" $ do
139− FailedWith err <- withServantServerAndContext api ctx server $ \burl -> do
140− evalExample $ serverSatisfies (Proxy :: Proxy (Get '[JSON] Int)) burl args
102+ Right (Failure _ err) <- withServantServerAndContext api ctx server $ \burl -> do
103+ safeEvalExample $ serverSatisfies (Proxy :: Proxy (Get '[JSON] Int)) burl args
141104 (notLongerThan 1 <%> mempty)
142105 show err `shouldContain` "notLongerThan"
143106
@@ -149,7 +112,7 @@ isComprehensiveSpec :: Spec
149112isComprehensiveSpec = describe "HasGenRequest" $ do
150113
151114 it "has instances for all 'servant' combinators" $ do
152− let _g = genRequest comprehensiveAPIWithoutStreamingOrRaw
115+ let _g = genRequest comprehensiveAPIWithoutRaw
153116 True `shouldBe` True -- This is a type-level check
154117
155118deepPathSpec :: Spec
@@ -158,7 +121,7 @@ deepPathSpec = describe "Path components" $ do
158121 it "are separated by slashes, without a trailing slash" $ do
159122 let rng = mkQCGen 0
160123 burl = BaseUrl Http "localhost" 80 ""
161− gen = runGenRequest deepAPI
124+ gen = genRequest deepAPI
162125 req = (unGen gen rng 0) burl
163126 path req `shouldBe` ("/one/two/three")
164127
@@ -169,7 +132,7 @@ queryParamsSpec = describe "QueryParams" $ do
169132 it "reduce to an HTTP query string correctly" $ do
170133 let rng = mkQCGen 0
171134 burl = BaseUrl Http "localhost" 80 ""
172− gen = runGenRequest paramsAPI
135+ gen = genRequest paramsAPI
173136 req = (unGen gen rng 0) burl
174137 qs = C.unpack $ queryString req
175138 qs `shouldBe` "one=_&two=_"
@@ -180,52 +143,11 @@ queryFlagsSpec = describe "QueryFlags" $ do
180143 it "reduce to an HTTP query string correctly" $ do
181144 let rng = mkQCGen 0
182145 burl = BaseUrl Http "localhost" 80 ""
183− gen = runGenRequest flagsAPI
146+ gen = genRequest flagsAPI
184147 req = (unGen gen rng 0) burl
185148 qs = C.unpack $ queryString req
186149 qs `shouldBe` "one&two"
187150
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−
207−makeRandomRequest :: Proxy LargeAPI -> BaseUrl -> IO Integer
208−makeRandomRequest large burl = do
209− req <- generate $ runGenRequest large
210− pure $ fst . fromJust . C.readInteger . C.drop 1 . path $ req burl
211−
212−
213−unbiasedGenerationSpec :: Spec
214−unbiasedGenerationSpec = describe "Unbiased Generation of requests" $
215−
216− it "frequency paired with generated endpoint should be more randomly distributed" $ do
217− let burl = BaseUrl Http "localhost" 80 ""
218− let runs = 10000 :: Double
219− someRequests <- replicateM 10000 (makeRandomRequest largeApi burl)
220− let mean = (sum $ map fromIntegral someRequests) / runs
221− let variancer x = let ix = fromIntegral x in (ix - mean) * (ix - mean)
222− 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
225− mean < 9 `shouldBe` True
226− -- Std dev is likely around 4. Variance is probably greater than 20.
227− variance > 19.5 `shouldBe` True
228−
229151------------------------------------------------------------------------------
230152-- APIs
231153------------------------------------------------------------------------------
@@ -273,45 +195,6 @@ server2 = return $ return 1
273195server3 :: IO (Server API2)
274196server3 = return $ return 2
275197
276−serverFailing :: IO (Server API2)
277−serverFailing = return . throwError $ err405
278−
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
293−largeApi :: Proxy LargeAPI
294−largeApi = Proxy
295−
296−type LargeAPI
297− = "1" :> Get '[JSON] Int
298− :<|> "2" :> Get '[JSON] Int
299− :<|> "3" :> Get '[JSON] Int
300− :<|> "4" :> Get '[JSON] Int
301− :<|> "5" :> Get '[JSON] Int
302− :<|> "6" :> Get '[JSON] Int
303− :<|> "7" :> Get '[JSON] Int
304− :<|> "8" :> Get '[JSON] Int
305− :<|> "9" :> Get '[JSON] Int
306− :<|> "10" :> Get '[JSON] Int
307− :<|> "11" :> Get '[JSON] Int
308− :<|> "12" :> Get '[JSON] Int
309− :<|> "13" :> Get '[JSON] Int
310− :<|> "14" :> Get '[JSON] Int
311− :<|> "15" :> Get '[JSON] Int
312− :<|> "16" :> Get '[JSON] Int
313−
314−
315198type OctetAPI = Get '[OctetStream] BS.ByteString
316199
317200octetAPI :: Proxy OctetAPI
@@ -320,44 +203,21 @@ octetAPI = Proxy
320203serverOctetAPI :: IO (Server OctetAPI)
321204serverOctetAPI = return $ return "blah"
322205
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−
341206ctx :: Context '[BasicAuthCheck ()]
342207ctx = BasicAuthCheck (const . return $ NoSuchUser) :. EmptyContext
343208------------------------------------------------------------------------------
344209-- Utils
345210------------------------------------------------------------------------------
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"
211+
212+evalExample :: (Example e, Arg e ~ ()) => e -> IO Result
213+evalExample e = evaluateExample e defaultParams ($ ()) progCallback
353214 where
354215 progCallback _ = return ()
355216
356−data EvalResult
357− = AnException SomeException
358− | AllGood
359− | FailedWith String
360− deriving (Show)
217+safeEvalExample :: (Example e, Arg e ~ ()) => e -> IO (Either SomeException Result)
218+safeEvalExample e = safeEvaluateExample e defaultParams ($ ()) progCallback
219+ where
220+ progCallback _ = return ()
361221
362222
363223args :: Args
@@ -369,3 +229,8 @@ noOfTestCases = 20000
369229#else
370230noOfTestCases = 1000
371231#endif
232+
233+#if !MIN_VERSION_servant(0,8,0)
234+comprehensiveAPIWithoutRaw :: Proxy ComprehensiveAPI
235+comprehensiveAPIWithoutRaw = comprehensiveAPI
236+#endif