@@ -8,6 +8,8 @@
88{-# OPTIONS_GHC -fcontext-stack=25 #-}
99module Servant.ClientSpec where
1010
11+import Control.Applicative
12+import qualified Control.Arrow as Arrow
1113import Control.Concurrent
1214import Control.Exception
1315import Control.Monad.Trans.Either
@@ -29,7 +31,6 @@ import Test.Hspec.QuickCheck
2931import Test.QuickCheck
3032
3133import Servant.API
32−import Servant.API.ContentTypes
3334import Servant.Client
3435import Servant.Server
3536
@@ -110,21 +111,21 @@ server = serve api (
110111withServer :: (BaseUrl -> IO a) -> IO a
111112withServer action = withWaiDaemon (return server) action
112113
113−getGet :: BaseUrl -> EitherT String IO Person
114−getDelete :: BaseUrl -> EitherT String IO ()
115−getCapture :: String -> BaseUrl -> EitherT String IO Person
116−getBody :: Person -> BaseUrl -> EitherT String IO Person
117−getQueryParam :: Maybe String -> BaseUrl -> EitherT String IO Person
118−getQueryParams :: [String] -> BaseUrl -> EitherT String IO [Person]
119−getQueryFlag :: Bool -> BaseUrl -> EitherT String IO Bool
120−getMatrixParam :: Maybe String -> BaseUrl -> EitherT String IO Person
121−getMatrixParams :: [String] -> BaseUrl -> EitherT String IO [Person]
122−getMatrixFlag :: Bool -> BaseUrl -> EitherT String IO Bool
123−getRawSuccess :: Method -> BaseUrl -> EitherT String IO (Int, ByteString, MediaType)
124−getRawFailure :: Method -> BaseUrl -> EitherT String IO (Int, ByteString, MediaType)
114+getGet :: BaseUrl -> EitherT ServantError IO Person
115+getDelete :: BaseUrl -> EitherT ServantError IO ()
116+getCapture :: String -> BaseUrl -> EitherT ServantError IO Person
117+getBody :: Person -> BaseUrl -> EitherT ServantError IO Person
118+getQueryParam :: Maybe String -> BaseUrl -> EitherT ServantError IO Person
119+getQueryParams :: [String] -> BaseUrl -> EitherT ServantError IO [Person]
120+getQueryFlag :: Bool -> BaseUrl -> EitherT ServantError IO Bool
121+getMatrixParam :: Maybe String -> BaseUrl -> EitherT ServantError IO Person
122+getMatrixParams :: [String] -> BaseUrl -> EitherT ServantError IO [Person]
123+getMatrixFlag :: Bool -> BaseUrl -> EitherT ServantError IO Bool
124+getRawSuccess :: Method -> BaseUrl -> EitherT ServantError IO (Int, ByteString, MediaType)
125+getRawFailure :: Method -> BaseUrl -> EitherT ServantError IO (Int, ByteString, MediaType)
125126getMultiple :: String -> Maybe Int -> Bool -> [(String, [Rational])]
126127 -> BaseUrl
127− -> EitherT String IO (String, Maybe Int, Bool, [(String, [Rational])])
128+ -> EitherT ServantError IO (String, Maybe Int, Bool, [(String, [Rational])])
128129( getGet
129130 :<|> getDelete
130131 :<|> getCapture
@@ -143,32 +144,32 @@ getMultiple :: String -> Maybe Int -> Bool -> [(String, [Rational])]
143144spec :: Spec
144145spec = do
145146 it "Servant.API.Get" $ withServer $ \ host -> do
146− runEitherT (getGet host) `shouldReturn` Right alice
147+ (Arrow.left show <$> runEitherT (getGet host)) `shouldReturn` Right alice
147148
148149 it "Servant.API.Delete" $ withServer $ \ host -> do
149− runEitherT (getDelete host) `shouldReturn` Right ()
150+ (Arrow.left show <$> runEitherT (getDelete host)) `shouldReturn` Right ()
150151
151152 it "Servant.API.Capture" $ withServer $ \ host -> do
152− runEitherT (getCapture "Paula" host) `shouldReturn` Right (Person "Paula" 0)
153+ (Arrow.left show <$> runEitherT (getCapture "Paula" host)) `shouldReturn` Right (Person "Paula" 0)
153154
154155 it "Servant.API.ReqBody" $ withServer $ \ host -> do
155156 let p = Person "Clara" 42
156− runEitherT (getBody p host) `shouldReturn` Right p
157+ (Arrow.left show <$> runEitherT (getBody p host)) `shouldReturn` Right p
157158
158159 it "Servant.API.QueryParam" $ withServer $ \ host -> do
159− runEitherT (getQueryParam (Just "alice") host) `shouldReturn` Right alice
160− Left result <- runEitherT (getQueryParam (Just "bob") host)
161− result `shouldContain` "bob not found"
160+ (Arrow.left show <$> runEitherT (getQueryParam (Just "alice") host)) `shouldReturn` Right alice
161+ Left (FailureResponse s _ _) <- runEitherT (getQueryParam (Just "bob") host)
162+ statusCode s `shouldBe` 400
162163
163164 it "Servant.API.QueryParam.QueryParams" $ withServer $ \ host -> do
164− runEitherT (getQueryParams [] host) `shouldReturn` Right []
165− runEitherT (getQueryParams ["alice", "bob"] host)
165+ (Arrow.left show <$> runEitherT (getQueryParams [] host)) `shouldReturn` Right []
166+ (Arrow.left show <$> runEitherT (getQueryParams ["alice", "bob"] host))
166167 `shouldReturn` Right [Person "alice" 0, Person "bob" 1]
167168
168169 context "Servant.API.QueryParam.QueryFlag" $
169170 forM_ [False, True] $ \ flag ->
170171 it (show flag) $ withServer $ \ host -> do
171− runEitherT (getQueryFlag flag host) `shouldReturn` Right flag
172+ (Arrow.left show <$> runEitherT (getQueryFlag flag host)) `shouldReturn` Right flag
172173
173174{-
174175 it "Servant.API.MatrixParam" $ withServer $ \ host -> do
@@ -188,17 +189,19 @@ spec = do
188189-}
189190
190191 it "Servant.API.Raw on success" $ withServer $ \ host -> do
191− runEitherT (getRawSuccess methodGet host) `shouldReturn` Right (200, "rawSuccess", "application"//"octet-stream")
192+ (Arrow.left show <$> runEitherT (getRawSuccess methodGet host))
193+ `shouldReturn` Right (200, "rawSuccess", "application"//"octet-stream")
192194
193195 it "Servant.API.Raw on failure" $ withServer $ \ host -> do
194− runEitherT (getRawFailure methodGet host) `shouldReturn` Right (400, "rawFailure", "application"//"octet-stream")
196+ (Arrow.left show <$> runEitherT (getRawFailure methodGet host))
197+ `shouldReturn` Right (400, "rawFailure", "application"//"octet-stream")
195198
196199 modifyMaxSuccess (const 20) $ do
197200 it "works for a combination of Capture, QueryParam, QueryFlag and ReqBody" $
198201 property $ forAllShrink pathGen shrink $ \(NonEmpty cap) num flag body ->
199202 ioProperty $ do
200203 withServer $ \ host -> do
201− result <- runEitherT (getMultiple cap num flag body host)
204+ result <- Arrow.left show <$> runEitherT (getMultiple cap num flag body host)
202205 return $
203206 result === Right (cap, num, flag, body)
204207
@@ -209,10 +212,10 @@ spec = do
209212 it desc $
210213 withWaiDaemon (return (serve api (left (500, "error message")))) $
211214 \ host -> do
212− let getResponse :: BaseUrl -> EitherT String IO ()
215+ let getResponse :: BaseUrl -> EitherT ServantError IO ()
213216 getResponse = client api
214− Left result <- runEitherT (getResponse host)
215− result `shouldContain` "error message"
217+ Left (FailureResponse status _ _) <- runEitherT (getResponse host)
218+ status `shouldBe` (Status 500 "error message")
216219 mapM_ test $
217220 (WrappedApi (Proxy :: Proxy Delete), "Delete") :
218221 (WrappedApi (Proxy :: Proxy (Get '[JSON] ())), "Delete") :
@@ -222,7 +225,7 @@ spec = do
222225
223226data WrappedApi where
224227 WrappedApi :: (HasServer api, Server api ~ EitherT (Int, String) IO a,
225− HasClient api, Client api ~ (BaseUrl -> EitherT String IO ())) =>
228+ HasClient api, Client api ~ (BaseUrl -> EitherT ServantError IO ())) =>
226229 Proxy api -> WrappedApi
227230
228231