@@ -2,34 +2,32 @@
2module Servant.QuickCheck.InternalSpec (spec) where
2module Servant.QuickCheck.InternalSpec (spec) where
5−import Control.Concurrent.MVar (newMVar, readMVar, swapMVar)
5+import Control.Concurrent.MVar (newMVar, readMVar, swapMVar)
6−import Control.Exception (SomeException)
6+import Control.Exception (SomeException)
7−import Control.Monad (replicateM)
7+import Control.Monad (replicateM)
8−import Control.Monad.IO.Class (liftIO)
8+import Control.Monad.IO.Class (liftIO)
9−import qualified Data.ByteString as BS
9+import qualified Data.ByteString as BS
10−import qualified Data.ByteString.Char8 as C
10+import qualified Data.ByteString.Char8 as C
11−import Data.Maybe (fromJust)
11+import Data.Maybe (fromJust)
12+import Network.HTTP.Client (path, queryString)
12import Prelude.Compat
13import Prelude.Compat
13import Servant
14import Servant
15+import Servant.HTML.Blaze (HTML)
16+import qualified Text.Blaze.Html as Blaze
17+import qualified Text.Blaze.Html5 as Blaze5
14−import Test.Hspec (Spec, context, describe, it, shouldBe,
18+import Test.Hspec (Spec, context, describe, it, shouldBe,
15− shouldContain)
19+ shouldContain)
16−import Test.Hspec.Core.Spec (Arg, Example, Result (..),
20+import Test.Hspec.Core.Spec (Arg, Example, Result (..), ResultStatus (..),
17− defaultParams, safeEvaluateExample)
21+ defaultParams, safeEvaluateExample)
18−import Test.QuickCheck.Gen (unGen, generate)
22+import Test.QuickCheck.Gen (generate, unGen)
19−import Test.QuickCheck.Random (mkQCGen)
23+import Test.QuickCheck.Random (mkQCGen)
20−import Network.HTTP.Client (queryString, path)
23−#if MIN_VERSION_servant(0,8,0)
24−import Servant.API.Internal.Test.ComprehensiveAPI (comprehensiveAPIWithoutRaw)
26+import Servant.Test.ComprehensiveAPI (comprehensiveAPIWithoutStreamingOrRaw)
26−import Servant.API.Internal.Test.ComprehensiveAPI (ComprehensiveAPI,
30import Servant.QuickCheck
28import Servant.QuickCheck
31−import Servant.QuickCheck.Internal (genRequest, runGenRequest, serverDoesntSatisfy)
29+import Servant.QuickCheck.Internal (genRequest, runGenRequest,
32−
30+ serverDoesntSatisfy)
34spec :: Spec
32spec :: Spec
@@ -41,6 +39,7 @@ spec = do
41 queryParamsSpec
39 queryParamsSpec
42 queryFlagsSpec
40 queryFlagsSpec
43 deepPathSpec
41 deepPathSpec
44 unbiasedGenerationSpec
43 unbiasedGenerationSpec
46serversEqualSpec :: Spec
45serversEqualSpec :: Spec
@@ -53,14 +52,42 @@ serversEqualSpec = describe "serversEqual" $ do
54 context "when servers are not equal" $ do
53 context "when servers are not equal" $ do
55 it "provides the failing responses in the error message" $ do
54 it "provides the failing responses in the error message" $ do
56− Right (Failure _ err) <- withServantServer api2 server2 $ \burl1 ->
55+ FailedWith err <- withServantServer api2 server2 $ \burl1 ->
57 withServantServer api2 server3 $ \burl2 -> do
56 withServantServer api2 server3 $ \burl2 -> do
58− safeEvalExample $ serversEqual api2 burl1 burl2 args bodyEquality
57+ evalExample $ serversEqual api2 burl1 burl2 args bodyEquality
59 show err `shouldContain` "Server equality failed"
58 show err `shouldContain` "Server equality failed"
60 show err `shouldContain` "Body: 1"
59 show err `shouldContain` "Body: 1"
61 show err `shouldContain` "Body: 2"
60 show err `shouldContain` "Body: 2"
62 show err `shouldContain` "Path: /failplz"
61 show err `shouldContain` "Path: /failplz"
63+ context "when JSON is equal but looks a bit different as a ByteString" $ do
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"
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
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"
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"
64serverSatisfiesSpec :: Spec
91serverSatisfiesSpec :: Spec
65serverSatisfiesSpec = describe "serverSatisfies" $ do
92serverSatisfiesSpec = describe "serverSatisfies" $ do
@@ -81,8 +108,8 @@ serverSatisfiesSpec = describe "serverSatisfies" $ do
81 context "when predicates are false" $ do
108 context "when predicates are false" $ do
83 it "fails with informative error messages" $ do
110 it "fails with informative error messages" $ do
84− Right (Failure _ err) <- withServantServerAndContext api ctx server $ \burl -> do
111+ FailedWith err <- withServantServerAndContext api ctx server $ \burl -> do
85− safeEvalExample $ serverSatisfies api burl args (notAllowedContainsAllowHeader <%> mempty)
112+ evalExample $ serverSatisfies api burl args (notAllowedContainsAllowHeader <%> mempty)
86 show err `shouldContain` "notAllowedContainsAllowHeader"
113 show err `shouldContain` "notAllowedContainsAllowHeader"
87 show err `shouldContain` "Headers"
114 show err `shouldContain` "Headers"
88 show err `shouldContain` "Body"
115 show err `shouldContain` "Body"
@@ -92,8 +119,8 @@ onlyJsonObjectSpec :: Spec
92onlyJsonObjectSpec = describe "onlyJsonObjects" $ do
119onlyJsonObjectSpec = describe "onlyJsonObjects" $ do
94 it "fails correctly" $ do
121 it "fails correctly" $ do
95− Right (Failure _ err) <- withServantServerAndContext api ctx server $ \burl -> do
122+ FailedWith err <- withServantServerAndContext api ctx server $ \burl -> do
96− safeEvalExample $ serverSatisfies (Proxy :: Proxy (Get '[JSON] Int)) burl args
123+ evalExample $ serverSatisfies (Proxy :: Proxy (Get '[JSON] Int)) burl args
97 (onlyJsonObjects <%> mempty)
124 (onlyJsonObjects <%> mempty)
98 show err `shouldContain` "onlyJsonObjects"
125 show err `shouldContain` "onlyJsonObjects"
@@ -101,12 +128,16 @@ onlyJsonObjectSpec = describe "onlyJsonObjects" $ do
101 withServantServerAndContext octetAPI ctx serverOctetAPI $ \burl ->
128 withServantServerAndContext octetAPI ctx serverOctetAPI $ \burl ->
102 serverSatisfies octetAPI burl args (onlyJsonObjects <%> mempty)
129 serverSatisfies octetAPI burl args (onlyJsonObjects <%> mempty)
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)
104notLongerThanSpec :: Spec
135notLongerThanSpec :: Spec
105notLongerThanSpec = describe "notLongerThan" $ do
136notLongerThanSpec = describe "notLongerThan" $ do
107 it "fails correctly" $ do
138 it "fails correctly" $ do
108− Right (Failure _ err) <- withServantServerAndContext api ctx server $ \burl -> do
139+ FailedWith err <- withServantServerAndContext api ctx server $ \burl -> do
109− safeEvalExample $ serverSatisfies (Proxy :: Proxy (Get '[JSON] Int)) burl args
140+ evalExample $ serverSatisfies (Proxy :: Proxy (Get '[JSON] Int)) burl args
110 (notLongerThan 1 <%> mempty)
141 (notLongerThan 1 <%> mempty)
111 show err `shouldContain` "notLongerThan"
142 show err `shouldContain` "notLongerThan"
@@ -118,7 +149,7 @@ isComprehensiveSpec :: Spec
118isComprehensiveSpec = describe "HasGenRequest" $ do
149isComprehensiveSpec = describe "HasGenRequest" $ do
120 it "has instances for all 'servant' combinators" $ do
151 it "has instances for all 'servant' combinators" $ do
121− let _g = genRequest comprehensiveAPIWithoutRaw
152+ let _g = genRequest comprehensiveAPIWithoutStreamingOrRaw
122 True `shouldBe` True -- This is a type-level check
153 True `shouldBe` True -- This is a type-level check
124deepPathSpec :: Spec
155deepPathSpec :: Spec
@@ -154,6 +185,25 @@ queryFlagsSpec = describe "QueryFlags" $ do
154 qs = C.unpack $ queryString req
185 qs = C.unpack $ queryString req
155 qs `shouldBe` "one&two"
186 qs `shouldBe` "one&two"
188+htmlDocTypesSpec :: Spec
189+htmlDocTypesSpec = describe "HtmlDocTypes" $ do
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"
197+ it "passes HTML with a doctype at start" $ do
198+ withServantServerAndContext docTypeApi ctx docTypeServer $ \burl ->
199+ serverSatisfies docTypeApi burl args (htmlIncludesDoctype <%> mempty)
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)
157makeRandomRequest :: Proxy LargeAPI -> BaseUrl -> IO Integer
207makeRandomRequest :: Proxy LargeAPI -> BaseUrl -> IO Integer
158makeRandomRequest large burl = do
208makeRandomRequest large burl = do
159 req <- generate $ runGenRequest large
209 req <- generate $ runGenRequest large
@@ -223,7 +273,23 @@ server2 = return $ return 1
223server3 :: IO (Server API2)
273server3 :: IO (Server API2)
224server3 = return $ return 2
274server3 = return $ return 2
276+serverFailing :: IO (Server API2)
277+serverFailing = return . throwError $ err405
280+type HtmlDoctype = Get '[HTML] Blaze.Html
282+docTypeApi :: Proxy HtmlDoctype
285+docTypeServer :: IO (Server HtmlDoctype)
286+docTypeServer = pure $ pure $ Blaze5.docTypeHtml $ Blaze5.span "Hello Test!"
288+noDocTypeServer :: IO (Server HtmlDoctype)
289+noDocTypeServer = pure $ pure $ Blaze.text "Hello Test!"
292+-- Api for unbiased generation of requests tests
227largeApi :: Proxy LargeAPI
293largeApi :: Proxy LargeAPI
228largeApi = Proxy
294largeApi = Proxy
@@ -254,16 +320,45 @@ octetAPI = Proxy
254serverOctetAPI :: IO (Server OctetAPI)
320serverOctetAPI :: IO (Server OctetAPI)
255serverOctetAPI = return $ return "blah"
321serverOctetAPI = return $ return "blah"
323+type JsonApi = "jsonComparison" :> Get '[OctetStream] BS.ByteString
325+jsonApi :: Proxy JsonApi
328+jsonServer1 :: IO (Server JsonApi)
329+jsonServer1 = return $ return "{ \"b\": [\"b\"], \"a\": 1 }" -- whitespace, ordering different
331+jsonServer2 :: IO (Server JsonApi)
332+jsonServer2 = return $ return "{\"a\": 1,\"b\":[\"b\"]}"
334+jsonServer3 :: IO (Server JsonApi)
335+jsonServer3 = return $ return "{\"a\": 2, \"b\": [\"b\"]}"
337+jsonServer4 :: IO (Server JsonApi)
338+jsonServer4 = return $ return "{\"c\": 1, \"d\": [\"b\"]}"
257ctx :: Context '[BasicAuthCheck ()]
341ctx :: Context '[BasicAuthCheck ()]
258ctx = BasicAuthCheck (const . return $ NoSuchUser) :. EmptyContext
342ctx = BasicAuthCheck (const . return $ NoSuchUser) :. EmptyContext
259------------------------------------------------------------------------------
343------------------------------------------------------------------------------
261------------------------------------------------------------------------------
345------------------------------------------------------------------------------
262−safeEvalExample :: (Example e, Arg e ~ ()) => e -> IO (Either SomeException Result)
346+evalExample :: (Example e, Arg e ~ ()) => e -> IO EvalResult
263−safeEvalExample e = safeEvaluateExample e defaultParams ($ ()) progCallback
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"
265 progCallback _ = return ()
354 progCallback _ = return ()
357+ = AnException SomeException
268args :: Args
363args :: Args
269args = defaultArgs { maxSuccess = noOfTestCases }
364args = defaultArgs { maxSuccess = noOfTestCases }
@@ -274,8 +369,3 @@ noOfTestCases = 20000
275noOfTestCases = 1000
370noOfTestCases = 1000
278−#if !MIN_VERSION_servant(0,8,0)
279−comprehensiveAPIWithoutRaw :: Proxy ComprehensiveAPI
280−comprehensiveAPIWithoutRaw = comprehensiveAPI