git.iko.soy/typeable/servant
on master help sign in
test suite: quickcheck property for a more complex client function
commitfd636ead19383c4ad856095332e63526363e9cba authorSönke Hahn <soenkehahn@gmail.com> dateFri 31 Oct 2014, 21:50 (12 years ago) parent10fa62e
2 files changed+32−3
servant.cabal+1 test/Servant/ClientSpec.hs+31−3
servant.cabal+1
@@ -90,6 +90,7 @@ test-suite spec
9090 , http-types
9191 , network >= 2.6
9292 , network-uri >= 2.6
93+ , QuickCheck
9394 , servant
9495 , string-conversions
9596 , text
test/Servant/ClientSpec.hs+31−3
@@ -8,15 +8,17 @@ module Servant.ClientSpec where
88import Control.Concurrent
99import Control.Exception
1010import Control.Monad.Trans.Either
11−import Data.Foldable
11+import Data.Char
12+import Data.Foldable (forM_)
1213import Data.Proxy
1314import Data.Typeable
1415import Network.Socket
1516import Network.URI
1617import Network.Wai
1718import Network.Wai.Handler.Warp
18−import Prelude hiding (mapM_)
1919import Test.Hspec
20+import Test.Hspec.QuickCheck
21+import Test.QuickCheck
2022
2123import Servant.API
2224import Servant.Client
@@ -31,6 +33,12 @@ type Api =
3133 :<|> "param" :> QueryParam "name" String :> Get Person
3234 :<|> "params" :> QueryParams "names" String :> Get [Person]
3335 :<|> "flag" :> QueryFlag "flag" :> Get Bool
36+ :<|> "multiple" :>
37+ Capture "first" String :>
38+ QueryParam "second" Int :>
39+ QueryFlag "third" :>
40+ ReqBody [(String, [Rational])] :>
41+ Get (String, Maybe Int, Bool, [(String, [Rational])])
3442api :: Proxy Api
3543api = Proxy
3644
@@ -45,6 +53,7 @@ server = serve api (
4553 Nothing -> left (400, "missing parameter"))
4654 :<|> (\ names -> return (zipWith Person names [0..]))
4755 :<|> return
56+ :<|> \ a b c d -> return (a, b, c, d)
4857 )
4958
5059withServer :: (URIAuth -> IO a) -> IO a
@@ -56,12 +65,16 @@ getBody :: Person -> URIAuth -> EitherT String IO Person
5665getQueryParam :: Maybe String -> URIAuth -> EitherT String IO Person
5766getQueryParams :: [String] -> URIAuth -> EitherT String IO [Person]
5867getQueryFlag :: Bool -> URIAuth -> EitherT String IO Bool
68+getMultiple :: String -> Maybe Int -> Bool -> [(String, [Rational])]
69+ -> URIAuth
70+ -> EitherT String IO (String, Maybe Int, Bool, [(String, [Rational])])
5971( getGet
6072 :<|> getCapture
6173 :<|> getBody
6274 :<|> getQueryParam
6375 :<|> getQueryParams
64− :<|> getQueryFlag)
76+ :<|> getQueryFlag
77+ :<|> getMultiple)
6578 = client api
6679
6780spec :: Spec
@@ -91,6 +104,15 @@ spec = do
91104 it (show flag) $ withServer $ \ host -> do
92105 runEitherT (getQueryFlag flag host) `shouldReturn` Right flag
93106
107+ modifyMaxSuccess (const 20) $ do
108+ it "works for a combination of Capture, QueryParam, QueryFlag and ReqBody" $
109+ property $ forAllShrink pathGen shrink $ \ a -> \ b c d ->
110+ ioProperty $ do
111+ withServer $ \ host -> do
112+ result <- runEitherT (getMultiple a b c d host)
113+ return $
114+ result === Right (a, b, c, d)
115+
94116
95117 context "client correctly handles error status codes" $ do
96118 let test :: WrappedApi -> Spec
@@ -156,3 +178,9 @@ openTestSocket = do
156178 listen s 1
157179 port <- socketPort s
158180 return (fromIntegral port, s)
181+
182+pathGen :: Gen String
183+pathGen = listOf $ elements $
184+ filter (not . (`elem` "?%[]/#")) $
185+ filter isPrint $
186+ map chr [0..127]