@@ -32,7 +32,7 @@ import Control.Applicative ((<$>))
32import Control.Arrow (left)
32import Control.Arrow (left)
33import Control.Concurrent (forkIO, killThread, ThreadId)
33import Control.Concurrent (forkIO, killThread, ThreadId)
34import Control.Exception (bracket)
34import Control.Exception (bracket)
35−import Control.Monad.Trans.Except (throwE, runExceptT)
35+import Control.Monad.Trans.Except (throwE )
36import Data.Aeson
36import Data.Aeson
37import qualified Data.ByteString.Lazy as BS
37import qualified Data.ByteString.Lazy as BS
38import Data.Char (chr, isPrint)
38import Data.Char (chr, isPrint)
@@ -123,22 +123,22 @@ type Api =
123api :: Proxy Api
123api :: Proxy Api
124api = Proxy
124api = Proxy
126−getGet :: C.Manager -> BaseUrl -> SCR.ClientM Person
126+getGet :: SCR.ClientM Person
127−getDeleteEmpty :: C.Manager -> BaseUrl -> SCR.ClientM NoContent
127+getDeleteEmpty :: SCR.ClientM NoContent
128−getCapture :: String -> C.Manager -> BaseUrl -> SCR.ClientM Person
128+getCapture :: String -> SCR.ClientM Person
129−getCaptureAll :: [String] -> C.Manager -> BaseUrl -> SCR.ClientM [Person]
129+getCaptureAll :: [String] -> SCR.ClientM [Person]
130−getBody :: Person -> C.Manager -> BaseUrl -> SCR.ClientM Person
130+getBody :: Person -> SCR.ClientM Person
131−getQueryParam :: Maybe String -> C.Manager -> BaseUrl -> SCR.ClientM Person
131+getQueryParam :: Maybe String -> SCR.ClientM Person
132−getQueryParams :: [String] -> C.Manager -> BaseUrl -> SCR.ClientM [Person]
132+getQueryParams :: [String] -> SCR.ClientM [Person]
133−getQueryFlag :: Bool -> C.Manager -> BaseUrl -> SCR.ClientM Bool
133+getQueryFlag :: Bool -> SCR.ClientM Bool
134−getRawSuccess :: HTTP.Method -> C.Manager -> BaseUrl
134+getRawSuccess :: HTTP.Method
135 -> SCR.ClientM (Int, BS.ByteString, MediaType, [HTTP.Header], C.Response BS.ByteString)
135 -> SCR.ClientM (Int, BS.ByteString, MediaType, [HTTP.Header], C.Response BS.ByteString)
136−getRawFailure :: HTTP.Method -> C.Manager -> BaseUrl
136+getRawFailure :: HTTP.Method
137 -> SCR.ClientM (Int, BS.ByteString, MediaType, [HTTP.Header], C.Response BS.ByteString)
137 -> SCR.ClientM (Int, BS.ByteString, MediaType, [HTTP.Header], C.Response BS.ByteString)
138−getMultiple :: String -> Maybe Int -> Bool -> [(String, [Rational])] -> C.Manager -> BaseUrl
138+getMultiple :: String -> Maybe Int -> Bool -> [(String, [Rational])]
139 -> SCR.ClientM (String, Maybe Int, Bool, [(String, [Rational])])
139 -> SCR.ClientM (String, Maybe Int, Bool, [(String, [Rational])])
140−getRespHeaders :: C.Manager -> BaseUrl -> SCR.ClientM (Headers TestHeaders Bool)
140+getRespHeaders :: SCR.ClientM (Headers TestHeaders Bool)
141−getDeleteContentType :: C.Manager -> BaseUrl -> SCR.ClientM NoContent
141+getDeleteContentType :: SCR.ClientM NoContent
143 :<|> getDeleteEmpty
143 :<|> getDeleteEmpty
144 :<|> getCapture
144 :<|> getCapture
@@ -242,42 +242,42 @@ sucessSpec :: Spec
242sucessSpec = beforeAll (startWaiApp server) $ afterAll endWaiApp $ do
242sucessSpec = beforeAll (startWaiApp server) $ afterAll endWaiApp $ do
244 it "Servant.API.Get" $ \(_, baseUrl) -> do
244 it "Servant.API.Get" $ \(_, baseUrl) -> do
245− (left show <$> runExceptT (getGet manager baseUrl)) `shouldReturn` Right alice
245+ (left show <$> (runClientM getGet (ClientEnv manager baseUrl))) `shouldReturn` Right alice
247 describe "Servant.API.Delete" $ do
247 describe "Servant.API.Delete" $ do
248 it "allows empty content type" $ \(_, baseUrl) -> do
248 it "allows empty content type" $ \(_, baseUrl) -> do
249− (left show <$> runExceptT (getDeleteEmpty manager baseUrl)) `shouldReturn` Right NoContent
249+ (left show <$> (runClientM getDeleteEmpty (ClientEnv manager baseUrl))) `shouldReturn` Right NoContent
251 it "allows content type" $ \(_, baseUrl) -> do
251 it "allows content type" $ \(_, baseUrl) -> do
252− (left show <$> runExceptT (getDeleteContentType manager baseUrl)) `shouldReturn` Right NoContent
252+ (left show <$> (runClientM getDeleteContentType (ClientEnv manager baseUrl))) `shouldReturn` Right NoContent
254 it "Servant.API.Capture" $ \(_, baseUrl) -> do
254 it "Servant.API.Capture" $ \(_, baseUrl) -> do
255− (left show <$> runExceptT (getCapture "Paula" manager baseUrl)) `shouldReturn` Right (Person "Paula" 0)
255+ (left show <$> (runClientM (getCapture "Paula") (ClientEnv manager baseUrl))) `shouldReturn` Right (Person "Paula" 0)
257 it "Servant.API.CaptureAll" $ \(_, baseUrl) -> do
257 it "Servant.API.CaptureAll" $ \(_, baseUrl) -> do
258 let expected = [(Person "Paula" 0), (Person "Peta" 1)]
258 let expected = [(Person "Paula" 0), (Person "Peta" 1)]
259− (left show <$> runExceptT (getCaptureAll ["Paula", "Peta"] manager baseUrl)) `shouldReturn` Right expected
259+ (left show <$> (runClientM (getCaptureAll ["Paula", "Peta"]) (ClientEnv manager baseUrl))) `shouldReturn` Right expected
261 it "Servant.API.ReqBody" $ \(_, baseUrl) -> do
261 it "Servant.API.ReqBody" $ \(_, baseUrl) -> do
262 let p = Person "Clara" 42
262 let p = Person "Clara" 42
263− (left show <$> runExceptT (getBody p manager baseUrl)) `shouldReturn` Right p
263+ (left show <$> runClientM (getBody p) (ClientEnv manager baseUrl)) `shouldReturn` Right p
265 it "Servant.API.QueryParam" $ \(_, baseUrl) -> do
265 it "Servant.API.QueryParam" $ \(_, baseUrl) -> do
266− left show <$> runExceptT (getQueryParam (Just "alice") manager baseUrl) `shouldReturn` Right alice
266+ left show <$> runClientM (getQueryParam (Just "alice")) (ClientEnv manager baseUrl) `shouldReturn` Right alice
267− Left FailureResponse{..} <- runExceptT (getQueryParam (Just "bob") manager baseUrl)
267+ Left FailureResponse{..} <- runClientM (getQueryParam (Just "bob")) (ClientEnv manager baseUrl)
268 responseStatus `shouldBe` HTTP.Status 400 "bob not found"
268 responseStatus `shouldBe` HTTP.Status 400 "bob not found"
270 it "Servant.API.QueryParam.QueryParams" $ \(_, baseUrl) -> do
270 it "Servant.API.QueryParam.QueryParams" $ \(_, baseUrl) -> do
271− (left show <$> runExceptT (getQueryParams [] manager baseUrl)) `shouldReturn` Right []
271+ (left show <$> runClientM (getQueryParams []) (ClientEnv manager baseUrl)) `shouldReturn` Right []
272− (left show <$> runExceptT (getQueryParams ["alice", "bob"] manager baseUrl))
272+ (left show <$> runClientM (getQueryParams ["alice", "bob"]) (ClientEnv manager baseUrl))
273 `shouldReturn` Right [Person "alice" 0, Person "bob" 1]
273 `shouldReturn` Right [Person "alice" 0, Person "bob" 1]
275 context "Servant.API.QueryParam.QueryFlag" $
275 context "Servant.API.QueryParam.QueryFlag" $
276 forM_ [False, True] $ \ flag -> it (show flag) $ \(_, baseUrl) -> do
276 forM_ [False, True] $ \ flag -> it (show flag) $ \(_, baseUrl) -> do
277− (left show <$> runExceptT (getQueryFlag flag manager baseUrl)) `shouldReturn` Right flag
277+ (left show <$> runClientM (getQueryFlag flag) (ClientEnv manager baseUrl)) `shouldReturn` Right flag
279 it "Servant.API.Raw on success" $ \(_, baseUrl) -> do
279 it "Servant.API.Raw on success" $ \(_, baseUrl) -> do
280− res <- runExceptT (getRawSuccess HTTP.methodGet manager baseUrl)
280+ res <- runClientM (getRawSuccess HTTP.methodGet) (ClientEnv manager baseUrl)
281 case res of
281 case res of
282 Left e -> assertFailure $ show e
282 Left e -> assertFailure $ show e
283 Right (code, body, ct, _, response) -> do
283 Right (code, body, ct, _, response) -> do
@@ -286,7 +286,7 @@ sucessSpec = beforeAll (startWaiApp server) $ afterAll endWaiApp $ do
286 C.responseStatus response `shouldBe` HTTP.ok200
286 C.responseStatus response `shouldBe` HTTP.ok200
288 it "Servant.API.Raw should return a Left in case of failure" $ \(_, baseUrl) -> do
288 it "Servant.API.Raw should return a Left in case of failure" $ \(_, baseUrl) -> do
289− res <- runExceptT (getRawFailure HTTP.methodGet manager baseUrl)
289+ res <- runClientM (getRawFailure HTTP.methodGet) (ClientEnv manager baseUrl)
290 case res of
290 case res of
291 Right _ -> assertFailure "expected Left, but got Right"
291 Right _ -> assertFailure "expected Left, but got Right"
292 Left e -> do
292 Left e -> do
@@ -294,7 +294,7 @@ sucessSpec = beforeAll (startWaiApp server) $ afterAll endWaiApp $ do
294 Servant.Client.responseBody e `shouldBe` "rawFailure"
294 Servant.Client.responseBody e `shouldBe` "rawFailure"
296 it "Returns headers appropriately" $ \(_, baseUrl) -> do
296 it "Returns headers appropriately" $ \(_, baseUrl) -> do
297− res <- runExceptT (getRespHeaders manager baseUrl)
297+ res <- runClientM getRespHeaders (ClientEnv manager baseUrl)
298 case res of
298 case res of
299 Left e -> assertFailure $ show e
299 Left e -> assertFailure $ show e
300 Right val -> getHeaders val `shouldBe` [("X-Example1", "1729"), ("X-Example2", "eg2")]
300 Right val -> getHeaders val `shouldBe` [("X-Example1", "1729"), ("X-Example2", "eg2")]
@@ -303,7 +303,7 @@ sucessSpec = beforeAll (startWaiApp server) $ afterAll endWaiApp $ do
303 it "works for a combination of Capture, QueryParam, QueryFlag and ReqBody" $ \(_, baseUrl) ->
303 it "works for a combination of Capture, QueryParam, QueryFlag and ReqBody" $ \(_, baseUrl) ->
304 property $ forAllShrink pathGen shrink $ \(NonEmpty cap) num flag body ->
304 property $ forAllShrink pathGen shrink $ \(NonEmpty cap) num flag body ->
305 ioProperty $ do
305 ioProperty $ do
306− result <- left show <$> runExceptT (getMultiple cap num flag body manager baseUrl)
306+ result <- left show <$> runClientM (getMultiple cap num flag body) (ClientEnv manager baseUrl)
308 result === Right (cap, num, flag, body)
308 result === Right (cap, num, flag, body)
@@ -315,9 +315,9 @@ wrappedApiSpec = describe "error status codes" $ do
315 let test :: (WrappedApi, String) -> Spec
315 let test :: (WrappedApi, String) -> Spec
316 test (WrappedApi api, desc) =
316 test (WrappedApi api, desc) =
317 it desc $ bracket (startWaiApp $ serveW api) endWaiApp $ \(_, baseUrl) -> do
317 it desc $ bracket (startWaiApp $ serveW api) endWaiApp $ \(_, baseUrl) -> do
318− let getResponse :: C.Manager -> BaseUrl -> SCR.ClientM ()
318+ let getResponse :: SCR.ClientM ()
319 getResponse = client api
319 getResponse = client api
320− Left FailureResponse{..} <- runExceptT (getResponse manager baseUrl)
320+ Left FailureResponse{..} <- runClientM getResponse (ClientEnv manager baseUrl)
321 responseStatus `shouldBe` (HTTP.Status 500 "error message")
321 responseStatus `shouldBe` (HTTP.Status 500 "error message")
322 in mapM_ test $
322 in mapM_ test $
323 (WrappedApi (Proxy :: Proxy (Delete '[JSON] ())), "Delete") :
323 (WrappedApi (Proxy :: Proxy (Delete '[JSON] ())), "Delete") :
@@ -332,42 +332,42 @@ failSpec = beforeAll (startWaiApp failServer) $ afterAll endWaiApp $ do
332 context "client returns errors appropriately" $ do
332 context "client returns errors appropriately" $ do
333 it "reports FailureResponse" $ \(_, baseUrl) -> do
333 it "reports FailureResponse" $ \(_, baseUrl) -> do
334 let (_ :<|> getDeleteEmpty :<|> _) = client api
334 let (_ :<|> getDeleteEmpty :<|> _) = client api
335− Left res <- runExceptT (getDeleteEmpty manager baseUrl)
335+ Left res <- runClientM getDeleteEmpty (ClientEnv manager baseUrl)
336 case res of
336 case res of
337 FailureResponse (HTTP.Status 404 "Not Found") _ _ -> return ()
337 FailureResponse (HTTP.Status 404 "Not Found") _ _ -> return ()
338 _ -> fail $ "expected 404 response, but got " <> show res
338 _ -> fail $ "expected 404 response, but got " <> show res
340 it "reports DecodeFailure" $ \(_, baseUrl) -> do
340 it "reports DecodeFailure" $ \(_, baseUrl) -> do
341 let (_ :<|> _ :<|> getCapture :<|> _) = client api
341 let (_ :<|> _ :<|> getCapture :<|> _) = client api
342− Left res <- runExceptT (getCapture "foo" manager baseUrl)
342+ Left res <- runClientM (getCapture "foo") (ClientEnv manager baseUrl)
343 case res of
343 case res of
344 DecodeFailure _ ("application/json") _ -> return ()
344 DecodeFailure _ ("application/json") _ -> return ()
345 _ -> fail $ "expected DecodeFailure, but got " <> show res
345 _ -> fail $ "expected DecodeFailure, but got " <> show res
347 it "reports ConnectionError" $ \_ -> do
347 it "reports ConnectionError" $ \_ -> do
348 let (getGetWrongHost :<|> _) = client api
348 let (getGetWrongHost :<|> _) = client api
349− Left res <- runExceptT (getGetWrongHost manager (BaseUrl Http "127.0.0.1" 19872 ""))
349+ Left res <- runClientM getGetWrongHost (ClientEnv manager (BaseUrl Http "127.0.0.1" 19872 ""))
350 case res of
350 case res of
351 ConnectionError _ -> return ()
351 ConnectionError _ -> return ()
352 _ -> fail $ "expected ConnectionError, but got " <> show res
352 _ -> fail $ "expected ConnectionError, but got " <> show res
354 it "reports UnsupportedContentType" $ \(_, baseUrl) -> do
354 it "reports UnsupportedContentType" $ \(_, baseUrl) -> do
355 let (getGet :<|> _ ) = client api
355 let (getGet :<|> _ ) = client api
356− Left res <- runExceptT (getGet manager baseUrl)
356+ Left res <- runClientM getGet (ClientEnv manager baseUrl)
357 case res of
357 case res of
358 UnsupportedContentType ("application/octet-stream") _ -> return ()
358 UnsupportedContentType ("application/octet-stream") _ -> return ()
359 _ -> fail $ "expected UnsupportedContentType, but got " <> show res
359 _ -> fail $ "expected UnsupportedContentType, but got " <> show res
361 it "reports InvalidContentTypeHeader" $ \(_, baseUrl) -> do
361 it "reports InvalidContentTypeHeader" $ \(_, baseUrl) -> do
362 let (_ :<|> _ :<|> _ :<|> _ :<|> getBody :<|> _) = client api
362 let (_ :<|> _ :<|> _ :<|> _ :<|> getBody :<|> _) = client api
363− Left res <- runExceptT (getBody alice manager baseUrl)
363+ Left res <- runClientM (getBody alice) (ClientEnv manager baseUrl)
364 case res of
364 case res of
365 InvalidContentTypeHeader "fooooo" _ -> return ()
365 InvalidContentTypeHeader "fooooo" _ -> return ()
366 _ -> fail $ "expected InvalidContentTypeHeader, but got " <> show res
366 _ -> fail $ "expected InvalidContentTypeHeader, but got " <> show res
368data WrappedApi where
368data WrappedApi where
369 WrappedApi :: (HasServer (api :: *) '[], Server api ~ Handler a,
369 WrappedApi :: (HasServer (api :: *) '[], Server api ~ Handler a,
370− HasClient api, Client api ~ (C.Manager -> BaseUrl -> SCR.ClientM ())) =>
370+ HasClient api, Client api ~ SCR.ClientM ()) =>
371 Proxy api -> WrappedApi
371 Proxy api -> WrappedApi
373basicAuthSpec :: Spec
373basicAuthSpec :: Spec
@@ -377,14 +377,14 @@ basicAuthSpec = beforeAll (startWaiApp basicAuthServer) $ afterAll endWaiApp $ d
377 it "Authenticates a BasicAuth protected server appropriately" $ \(_,baseUrl) -> do
377 it "Authenticates a BasicAuth protected server appropriately" $ \(_,baseUrl) -> do
378 let getBasic = client basicAuthAPI
378 let getBasic = client basicAuthAPI
379 let basicAuthData = BasicAuthData "servant" "server"
379 let basicAuthData = BasicAuthData "servant" "server"
380− (left show <$> runExceptT (getBasic basicAuthData manager baseUrl)) `shouldReturn` Right alice
380+ (left show <$> runClientM (getBasic basicAuthData) (ClientEnv manager baseUrl)) `shouldReturn` Right alice
382 context "Authentication is rejected when requests are not authenticated properly" $ do
382 context "Authentication is rejected when requests are not authenticated properly" $ do
384 it "Authenticates a BasicAuth protected server appropriately" $ \(_,baseUrl) -> do
384 it "Authenticates a BasicAuth protected server appropriately" $ \(_,baseUrl) -> do
385 let getBasic = client basicAuthAPI
385 let getBasic = client basicAuthAPI
386 let basicAuthData = BasicAuthData "not" "password"
386 let basicAuthData = BasicAuthData "not" "password"
387− Left FailureResponse{..} <- runExceptT (getBasic basicAuthData manager baseUrl)
387+ Left FailureResponse{..} <- runClientM (getBasic basicAuthData) (ClientEnv manager baseUrl)
388 responseStatus `shouldBe` HTTP.Status 403 "Forbidden"
388 responseStatus `shouldBe` HTTP.Status 403 "Forbidden"
390genAuthSpec :: Spec
390genAuthSpec :: Spec
@@ -394,14 +394,14 @@ genAuthSpec = beforeAll (startWaiApp genAuthServer) $ afterAll endWaiApp $ do
394 it "Authenticates a AuthProtect protected server appropriately" $ \(_, baseUrl) -> do
394 it "Authenticates a AuthProtect protected server appropriately" $ \(_, baseUrl) -> do
395 let getProtected = client genAuthAPI
395 let getProtected = client genAuthAPI
396 let authRequest = mkAuthenticateReq () (\_ req -> SCR.addHeader "AuthHeader" ("cool" :: String) req)
396 let authRequest = mkAuthenticateReq () (\_ req -> SCR.addHeader "AuthHeader" ("cool" :: String) req)
397− (left show <$> runExceptT (getProtected authRequest manager baseUrl)) `shouldReturn` Right alice
397+ (left show <$> runClientM (getProtected authRequest) (ClientEnv manager baseUrl)) `shouldReturn` Right alice
399 context "Authentication is rejected when requests are not authenticated properly" $ do
399 context "Authentication is rejected when requests are not authenticated properly" $ do
401 it "Authenticates a AuthProtect protected server appropriately" $ \(_, baseUrl) -> do
401 it "Authenticates a AuthProtect protected server appropriately" $ \(_, baseUrl) -> do
402 let getProtected = client genAuthAPI
402 let getProtected = client genAuthAPI
403 let authRequest = mkAuthenticateReq () (\_ req -> SCR.addHeader "Wrong" ("header" :: String) req)
403 let authRequest = mkAuthenticateReq () (\_ req -> SCR.addHeader "Wrong" ("header" :: String) req)
404− Left FailureResponse{..} <- runExceptT (getProtected authRequest manager baseUrl)
404+ Left FailureResponse{..} <- runClientM (getProtected authRequest) (ClientEnv manager baseUrl)
405 responseStatus `shouldBe` (HTTP.Status 401 "Unauthorized")
405 responseStatus `shouldBe` (HTTP.Status 401 "Unauthorized")
407-- * utils
407-- * utils