git.iko.soy/typeable/servant
on master help sign in
add a test for hoistClient
commit9eb57a6119f9c9045dbd49050bed6f1282611d99 authorAlp Mestanogullari <alpmestan@gmail.com> dateWed 4 Apr 2018, 01:14 (9 years ago) parent200311e
3 files changed+25−1
nix/shell.nix+1−1 servant-client/src/Servant/Client.hs+1 servant-client/test/Servant/ClientSpec.hs+23
nix/shell.nix+1−1
@@ -1,5 +1,5 @@
11{ pkgs ? import <nixpkgs> {}
2−, compiler ? "ghc821"
2+, compiler ? "ghc822"
33, tutorial ? false
44}:
55
servant-client/src/Servant/Client.hs+1
@@ -9,6 +9,7 @@ module Servant.Client
99 , runClientM
1010 , ClientEnv(..)
1111 , mkClientEnv
12+ , hoistClient
1213 , module Servant.Client.Core.Reexport
1314 ) where
1415
servant-client/test/Servant/ClientSpec.hs+23
@@ -89,6 +89,7 @@ spec = describe "Servant.Client" $ do
8989 basicAuthSpec
9090 genAuthSpec
9191 genericClientSpec
92+ hoistClientSpec
9293
9394-- * test data types
9495
@@ -491,6 +492,28 @@ genericClientSpec = beforeAll (startWaiApp genericClientServer) $ afterAll endWa
491492 left show <$> runClient (getSum 3 4) baseUrl `shouldReturn` Right 7
492493 left show <$> runClient doNothing baseUrl `shouldReturn` Right ()
493494
495+-- * hoistClient
496+
497+type HoistClientAPI = Get '[JSON] Int :<|> Capture "n" Int :> Post '[JSON] Int
498+
499+hoistClientAPI :: Proxy HoistClientAPI
500+hoistClientAPI = Proxy
501+
502+hoistClientServer :: Application -- implements HoistClientAPI
503+hoistClientServer = serve hoistClientAPI $ return 5 :<|> (\n -> return n)
504+
505+hoistClientSpec :: Spec
506+hoistClientSpec = beforeAll (startWaiApp hoistClientServer) $ afterAll endWaiApp $ do
507+ describe "Servant.Client.hoistClient" $ do
508+ it "allows us to GET/POST/... requests in IO instead of ClientM" $ \(_, baseUrl) -> do
509+ let (getInt :<|> postInt)
510+ = hoistClient hoistClientAPI
511+ (fmap (either (error . show) id) . flip runClient baseUrl)
512+ (client hoistClientAPI)
513+
514+ getInt `shouldReturn` 5
515+ postInt 5 `shouldReturn` 5
516+
494517-- * utils
495518
496519startWaiApp :: Application -> IO (ThreadId, BaseUrl)