on master help sign in
client tests: QueryParams
c2be4f3Sönke Hahn
src/Servant/API/QueryParam.hs+2−1
@@ -7,6 +7,7 @@
7{-# LANGUAGE ScopedTypeVariables #-}
7{-# LANGUAGE ScopedTypeVariables #-}
8module Servant.API.QueryParam where
8module Servant.API.QueryParam where
9
9
10+import Data.List
10import Data.Maybe
11import Data.Maybe
11import Data.Proxy
12import Data.Proxy
12import Data.String.Conversions
13import Data.String.Conversions
@@ -104,7 +105,7 @@ instance (KnownSymbol sym, ToText a, HasClient sublayout)
104
105
105 clientWithRoute Proxy req paramlist =
106 clientWithRoute Proxy req paramlist =
106 clientWithRoute (Proxy :: Proxy sublayout) $
107 clientWithRoute (Proxy :: Proxy sublayout) $
107− foldr (appendToQueryString pname) req paramlist'
108+ foldl' (\ value req -> appendToQueryString pname req value) req paramlist'
108
109
109 where pname = cs pname'
110 where pname = cs pname'
110 pname' = symbolVal (Proxy :: Proxy sym)
111 pname' = symbolVal (Proxy :: Proxy sym)
test/Servant/ClientSpec.hs+14−1
@@ -27,6 +27,7 @@ type Api =
27 :<|> "capture" :> Capture "name" String :> Get Person
27 :<|> "capture" :> Capture "name" String :> Get Person
28 :<|> "body" :> ReqBody Person :> Post Person
28 :<|> "body" :> ReqBody Person :> Post Person
29 :<|> "param" :> QueryParam "name" String :> Get Person
29 :<|> "param" :> QueryParam "name" String :> Get Person
30+ :<|> "params" :> QueryParams "names" String :> Get [Person]
30api :: Proxy Api
31api :: Proxy Api
31api = Proxy
32api = Proxy
32
33
@@ -39,6 +40,7 @@ server = serve api (
39 Just "alice" -> return alice
40 Just "alice" -> return alice
40 Just name -> left (400, name ++ " not found")
41 Just name -> left (400, name ++ " not found")
41 Nothing -> left (400, "missing parameter"))
42 Nothing -> left (400, "missing parameter"))
43+ :<|> (\ names -> return (zipWith Person names [0..]))
42 )
44 )
43
45
44withServer :: (URIAuth -> IO a) -> IO a
46withServer :: (URIAuth -> IO a) -> IO a
@@ -48,7 +50,13 @@ getGet :: URIAuth -> EitherT String IO Person
48getCapture :: String -> URIAuth -> EitherT String IO Person
50getCapture :: String -> URIAuth -> EitherT String IO Person
49getBody :: Person -> URIAuth -> EitherT String IO Person
51getBody :: Person -> URIAuth -> EitherT String IO Person
50getQueryParam :: Maybe String -> URIAuth -> EitherT String IO Person
52getQueryParam :: Maybe String -> URIAuth -> EitherT String IO Person
53+getQueryParams :: [String] -> URIAuth -> EitherT String IO [Person]
54+( getGet
55+ :<|> getCapture
56+ :<|> getBody
51−(getGet :<|> getCapture :<|> getBody :<|> getQueryParam) = client api
57+ :<|> getQueryParam
58+ :<|> getQueryParams)
59+ = client api
52
60
53spec :: Spec
61spec :: Spec
54spec = do
62spec = do
@@ -67,6 +75,11 @@ spec = do
67 Left result <- runEitherT (getQueryParam (Just "bob") host)
75 Left result <- runEitherT (getQueryParam (Just "bob") host)
68 result `shouldContain` "bob not found"
76 result `shouldContain` "bob not found"
69
77
78+ it "Servant.API.QueryParam.QueryParams" $ withServer $ \ host -> do
79+ runEitherT (getQueryParams [] host) `shouldReturn` Right []
80+ runEitherT (getQueryParams ["alice", "bob"] host)
81+ `shouldReturn` Right [Person "alice" 0, Person "bob" 1]
82+
70 context "client correctly handles error status codes" $ do
83 context "client correctly handles error status codes" $ do
71 let test :: WrappedApi -> Spec
84 let test :: WrappedApi -> Spec
72 test (WrappedApi api) =
85 test (WrappedApi api) =