git.iko.soy/typeable/servant
on master help sign in
client tests: QueryParams
commitc2be4f3377a8939c240a521751c6a64c27c972fb authorSönke Hahn <soenkehahn@gmail.com> dateFri 31 Oct 2014, 18:18 (12 years ago) parent0802221
2 files changed+16−2
src/Servant/API/QueryParam.hs+2−1 test/Servant/ClientSpec.hs+14−1
src/Servant/API/QueryParam.hs+2−1
@@ -7,6 +7,7 @@
77{-# LANGUAGE ScopedTypeVariables #-}
88module Servant.API.QueryParam where
99
10+import Data.List
1011import Data.Maybe
1112import Data.Proxy
1213import Data.String.Conversions
@@ -104,7 +105,7 @@ instance (KnownSymbol sym, ToText a, HasClient sublayout)
104105
105106 clientWithRoute Proxy req paramlist =
106107 clientWithRoute (Proxy :: Proxy sublayout) $
107− foldr (appendToQueryString pname) req paramlist'
108+ foldl' (\ value req -> appendToQueryString pname req value) req paramlist'
108109
109110 where pname = cs pname'
110111 pname' = symbolVal (Proxy :: Proxy sym)
test/Servant/ClientSpec.hs+14−1
@@ -27,6 +27,7 @@ type Api =
2727 :<|> "capture" :> Capture "name" String :> Get Person
2828 :<|> "body" :> ReqBody Person :> Post Person
2929 :<|> "param" :> QueryParam "name" String :> Get Person
30+ :<|> "params" :> QueryParams "names" String :> Get [Person]
3031api :: Proxy Api
3132api = Proxy
3233
@@ -39,6 +40,7 @@ server = serve api (
3940 Just "alice" -> return alice
4041 Just name -> left (400, name ++ " not found")
4142 Nothing -> left (400, "missing parameter"))
43+ :<|> (\ names -> return (zipWith Person names [0..]))
4244 )
4345
4446withServer :: (URIAuth -> IO a) -> IO a
@@ -48,7 +50,13 @@ getGet :: URIAuth -> EitherT String IO Person
4850getCapture :: String -> URIAuth -> EitherT String IO Person
4951getBody :: Person -> URIAuth -> EitherT String IO Person
5052getQueryParam :: Maybe String -> URIAuth -> EitherT String IO Person
51−(getGet :<|> getCapture :<|> getBody :<|> getQueryParam) = client api
53+getQueryParams :: [String] -> URIAuth -> EitherT String IO [Person]
54+( getGet
55+ :<|> getCapture
56+ :<|> getBody
57+ :<|> getQueryParam
58+ :<|> getQueryParams)
59+ = client api
5260
5361spec :: Spec
5462spec = do
@@ -67,6 +75,11 @@ spec = do
6775 Left result <- runEitherT (getQueryParam (Just "bob") host)
6876 result `shouldContain` "bob not found"
6977
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+
7083 context "client correctly handles error status codes" $ do
7184 let test :: WrappedApi -> Spec
7285 test (WrappedApi api) =