@@ -8,12 +8,14 @@ module Servant.ClientSpec where
8import Control.Concurrent
8import Control.Concurrent
9import Control.Exception
9import Control.Exception
10import Control.Monad.Trans.Either
10import Control.Monad.Trans.Either
11import Data.Proxy
12import Data.Proxy
12import Data.Typeable
13import Data.Typeable
13import Network.Socket
14import Network.Socket
14import Network.URI
15import Network.URI
15import Network.Wai
16import Network.Wai
16import Network.Wai.Handler.Warp
17import Network.Wai.Handler.Warp
18+import Prelude hiding (mapM_)
17import Test.Hspec
19import Test.Hspec
19import Servant.API
21import Servant.API
@@ -28,6 +30,7 @@ type Api =
28 :<|> "body" :> ReqBody Person :> Post Person
30 :<|> "body" :> ReqBody Person :> Post Person
29 :<|> "param" :> QueryParam "name" String :> Get Person
31 :<|> "param" :> QueryParam "name" String :> Get Person
30 :<|> "params" :> QueryParams "names" String :> Get [Person]
32 :<|> "params" :> QueryParams "names" String :> Get [Person]
33+ :<|> "flag" :> QueryFlag "flag" :> Get Bool
31api :: Proxy Api
34api :: Proxy Api
32api = Proxy
35api = Proxy
@@ -41,6 +44,7 @@ server = serve api (
41 Just name -> left (400, name ++ " not found")
44 Just name -> left (400, name ++ " not found")
42 Nothing -> left (400, "missing parameter"))
45 Nothing -> left (400, "missing parameter"))
43 :<|> (\ names -> return (zipWith Person names [0..]))
46 :<|> (\ names -> return (zipWith Person names [0..]))
46withServer :: (URIAuth -> IO a) -> IO a
50withServer :: (URIAuth -> IO a) -> IO a
@@ -51,11 +55,13 @@ getCapture :: String -> URIAuth -> EitherT String IO Person
51getBody :: Person -> URIAuth -> EitherT String IO Person
55getBody :: Person -> URIAuth -> EitherT String IO Person
52getQueryParam :: Maybe String -> URIAuth -> EitherT String IO Person
56getQueryParam :: Maybe String -> URIAuth -> EitherT String IO Person
53getQueryParams :: [String] -> URIAuth -> EitherT String IO [Person]
57getQueryParams :: [String] -> URIAuth -> EitherT String IO [Person]
58+getQueryFlag :: Bool -> URIAuth -> EitherT String IO Bool
55 :<|> getCapture
60 :<|> getCapture
56 :<|> getBody
61 :<|> getBody
57 :<|> getQueryParam
62 :<|> getQueryParam
58− :<|> getQueryParams)
63+ :<|> getQueryParams
59 = client api
65 = client api
61spec :: Spec
67spec :: Spec
@@ -80,6 +86,12 @@ spec = do
80 runEitherT (getQueryParams ["alice", "bob"] host)
86 runEitherT (getQueryParams ["alice", "bob"] host)
81 `shouldReturn` Right [Person "alice" 0, Person "bob" 1]
87 `shouldReturn` Right [Person "alice" 0, Person "bob" 1]
89+ context "Servant.API.QueryParam.QueryFlag" $
90+ forM_ [False, True] $ \ flag ->
91+ it (show flag) $ withServer $ \ host -> do
92+ runEitherT (getQueryFlag flag host) `shouldReturn` Right flag
83 context "client correctly handles error status codes" $ do
95 context "client correctly handles error status codes" $ do
84 let test :: WrappedApi -> Spec
96 let test :: WrappedApi -> Spec
85 test (WrappedApi api) =
97 test (WrappedApi api) =