Skip to content

Commit 9eb57a6

Browse files
committed
add a test for hoistClient
1 parent 200311e commit 9eb57a6

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

nix/shell.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

496519
startWaiApp :: Application -> IO (ThreadId, BaseUrl)

0 commit comments

Comments
 (0)