git.iko.soy/typeable/servant
on master help sign in
Changed servant-client tests to reflect the changes to the client function
commit89b0758dc85465b7ff1f85a145ea96700ddaddb6 authormbg <michael.gale@cl.cam.ac.uk> dateMon 28 Mar 2016, 14:52 (11 years ago) parent7379b74
1 file changed+50−49
servant-client/test/Servant/ClientSpec.hs+50−49
servant-client/test/Servant/ClientSpec.hs+50−49
@@ -28,7 +28,7 @@ import Control.Applicative ((<$>))
2828import Control.Arrow (left)
2929import Control.Concurrent (forkIO, killThread, ThreadId)
3030import Control.Exception (bracket)
31−import Control.Monad.Trans.Except (ExceptT, runExceptT, throwE)
31+import Control.Monad.Trans.Except (ExceptT, throwE)
3232import Data.Aeson
3333import Data.Char (chr, isPrint)
3434import Data.Foldable (forM_)
@@ -208,47 +208,48 @@ sucessSpec :: Spec
208208sucessSpec = beforeAll (startWaiApp server) $ afterAll endWaiApp $ do
209209
210210 it "Servant.API.Get" $ \(_, baseUrl) -> do
211− let getGet = getNth (Proxy :: Proxy 0) $ client api baseUrl manager
212− (left show <$> runExceptT getGet) `shouldReturn` Right alice
211+ let getGet = getNth (Proxy :: Proxy 0) $ client api
212+ (left show <$> SCR.runClientM getGet baseUrl manager) `shouldReturn` Right alice
213213
214214 describe "Servant.API.Delete" $ do
215215 it "allows empty content type" $ \(_, baseUrl) -> do
216− let getDeleteEmpty = getNth (Proxy :: Proxy 1) $ client api baseUrl manager
217− (left show <$> runExceptT getDeleteEmpty) `shouldReturn` Right NoContent
216+ let getDeleteEmpty = getNth (Proxy :: Proxy 1) $ client api
217+ (left show <$> SCR.runClientM getDeleteEmpty baseUrl manager) `shouldReturn` Right NoContent
218218
219219 it "allows content type" $ \(_, baseUrl) -> do
220− let getDeleteContentType = getLast $ client api baseUrl manager
221− (left show <$> runExceptT getDeleteContentType) `shouldReturn` Right NoContent
220+ let getDeleteContentType :: SCR.ClientM NoContent
221+ getDeleteContentType = getLast $ client api
222+ (left show <$> SCR.runClientM getDeleteContentType baseUrl manager) `shouldReturn` Right NoContent
222223
223224 it "Servant.API.Capture" $ \(_, baseUrl) -> do
224− let getCapture = getNth (Proxy :: Proxy 2) $ client api baseUrl manager
225− (left show <$> runExceptT (getCapture "Paula")) `shouldReturn` Right (Person "Paula" 0)
225+ let getCapture = getNth (Proxy :: Proxy 2) $ client api
226+ (left show <$> SCR.runClientM (getCapture "Paula") baseUrl manager) `shouldReturn` Right (Person "Paula" 0)
226227
227228 it "Servant.API.ReqBody" $ \(_, baseUrl) -> do
228229 let p = Person "Clara" 42
229− getBody = getNth (Proxy :: Proxy 3) $ client api baseUrl manager
230− (left show <$> runExceptT (getBody p)) `shouldReturn` Right p
230+ getBody = getNth (Proxy :: Proxy 3) $ client api
231+ (left show <$> SCR.runClientM (getBody p) baseUrl manager) `shouldReturn` Right p
231232
232233 it "Servant.API.QueryParam" $ \(_, baseUrl) -> do
233− let getQueryParam = getNth (Proxy :: Proxy 4) $ client api baseUrl manager
234− left show <$> runExceptT (getQueryParam (Just "alice")) `shouldReturn` Right alice
235− Left FailureResponse{..} <- runExceptT (getQueryParam (Just "bob"))
234+ let getQueryParam = getNth (Proxy :: Proxy 4) $ client api
235+ left show <$> SCR.runClientM (getQueryParam (Just "alice")) baseUrl manager `shouldReturn` Right alice
236+ Left FailureResponse{..} <- SCR.runClientM (getQueryParam (Just "bob")) baseUrl manager
236237 responseStatus `shouldBe` Status 400 "bob not found"
237238
238239 it "Servant.API.QueryParam.QueryParams" $ \(_, baseUrl) -> do
239− let getQueryParams = getNth (Proxy :: Proxy 5) $ client api baseUrl manager
240− (left show <$> runExceptT (getQueryParams [])) `shouldReturn` Right []
241− (left show <$> runExceptT (getQueryParams ["alice", "bob"]))
240+ let getQueryParams = getNth (Proxy :: Proxy 5) $ client api
241+ (left show <$> SCR.runClientM (getQueryParams []) baseUrl manager) `shouldReturn` Right []
242+ (left show <$> SCR.runClientM (getQueryParams ["alice", "bob"]) baseUrl manager)
242243 `shouldReturn` Right [Person "alice" 0, Person "bob" 1]
243244
244245 context "Servant.API.QueryParam.QueryFlag" $
245246 forM_ [False, True] $ \ flag -> it (show flag) $ \(_, baseUrl) -> do
246− let getQueryFlag = getNth (Proxy :: Proxy 6) $ client api baseUrl manager
247− (left show <$> runExceptT (getQueryFlag flag)) `shouldReturn` Right flag
247+ let getQueryFlag = getNth (Proxy :: Proxy 6) $ client api
248+ (left show <$> SCR.runClientM (getQueryFlag flag) baseUrl manager) `shouldReturn` Right flag
248249
249250 it "Servant.API.Raw on success" $ \(_, baseUrl) -> do
250− let getRawSuccess = getNth (Proxy :: Proxy 7) $ client api baseUrl manager
251− res <- runExceptT (getRawSuccess methodGet)
251+ let getRawSuccess = getNth (Proxy :: Proxy 7) $ client api
252+ res <- SCR.runClientM (getRawSuccess methodGet) baseUrl manager
252253 case res of
253254 Left e -> assertFailure $ show e
254255 Right (code, body, ct, _, response) -> do
@@ -257,8 +258,8 @@ sucessSpec = beforeAll (startWaiApp server) $ afterAll endWaiApp $ do
257258 C.responseStatus response `shouldBe` ok200
258259
259260 it "Servant.API.Raw should return a Left in case of failure" $ \(_, baseUrl) -> do
260− let getRawFailure = getNth (Proxy :: Proxy 8) $ client api baseUrl manager
261− res <- runExceptT (getRawFailure methodGet)
261+ let getRawFailure = getNth (Proxy :: Proxy 8) $ client api
262+ res <- SCR.runClientM (getRawFailure methodGet) baseUrl manager
262263 case res of
263264 Right _ -> assertFailure "expected Left, but got Right"
264265 Left e -> do
@@ -266,18 +267,18 @@ sucessSpec = beforeAll (startWaiApp server) $ afterAll endWaiApp $ do
266267 Servant.Client.responseBody e `shouldBe` "rawFailure"
267268
268269 it "Returns headers appropriately" $ \(_, baseUrl) -> do
269− let getRespHeaders = getNth (Proxy :: Proxy 10) $ client api baseUrl manager
270− res <- runExceptT getRespHeaders
270+ let getRespHeaders = getNth (Proxy :: Proxy 10) $ client api
271+ res <- SCR.runClientM getRespHeaders baseUrl manager
271272 case res of
272273 Left e -> assertFailure $ show e
273274 Right val -> getHeaders val `shouldBe` [("X-Example1", "1729"), ("X-Example2", "eg2")]
274275
275276 modifyMaxSuccess (const 20) $ do
276277 it "works for a combination of Capture, QueryParam, QueryFlag and ReqBody" $ \(_, baseUrl) ->
277− let getMultiple = getNth (Proxy :: Proxy 9) $ client api baseUrl manager
278+ let getMultiple = getNth (Proxy :: Proxy 9) $ client api
278279 in property $ forAllShrink pathGen shrink $ \(NonEmpty cap) num flag body ->
279280 ioProperty $ do
280− result <- left show <$> runExceptT (getMultiple cap num flag body)
281+ result <- left show <$> SCR.runClientM (getMultiple cap num flag body) baseUrl manager
281282 return $
282283 result === Right (cap, num, flag, body)
283284
@@ -289,9 +290,9 @@ wrappedApiSpec = describe "error status codes" $ do
289290 let test :: (WrappedApi, String) -> Spec
290291 test (WrappedApi api, desc) =
291292 it desc $ bracket (startWaiApp $ serveW api) endWaiApp $ \(_, baseUrl) -> do
292− let getResponse :: ExceptT ServantError IO ()
293− getResponse = client api baseUrl manager
294− Left FailureResponse{..} <- runExceptT getResponse
293+ let getResponse :: SCR.ClientM ()
294+ getResponse = client api
295+ Left FailureResponse{..} <- SCR.runClientM getResponse baseUrl manager
295296 responseStatus `shouldBe` (Status 500 "error message")
296297 in mapM_ test $
297298 (WrappedApi (Proxy :: Proxy (Delete '[JSON] ())), "Delete") :
@@ -305,43 +306,43 @@ failSpec = beforeAll (startWaiApp failServer) $ afterAll endWaiApp $ do
305306
306307 context "client returns errors appropriately" $ do
307308 it "reports FailureResponse" $ \(_, baseUrl) -> do
308− let (_ :<|> getDeleteEmpty :<|> _) = client api baseUrl manager
309− Left res <- runExceptT getDeleteEmpty
309+ let (_ :<|> getDeleteEmpty :<|> _) = client api
310+ Left res <- SCR.runClientM getDeleteEmpty baseUrl manager
310311 case res of
311312 FailureResponse (Status 404 "Not Found") _ _ -> return ()
312313 _ -> fail $ "expected 404 response, but got " <> show res
313314
314315 it "reports DecodeFailure" $ \(_, baseUrl) -> do
315− let (_ :<|> _ :<|> getCapture :<|> _) = client api baseUrl manager
316− Left res <- runExceptT (getCapture "foo")
316+ let (_ :<|> _ :<|> getCapture :<|> _) = client api
317+ Left res <- SCR.runClientM (getCapture "foo") baseUrl manager
317318 case res of
318319 DecodeFailure _ ("application/json") _ -> return ()
319320 _ -> fail $ "expected DecodeFailure, but got " <> show res
320321
321322 it "reports ConnectionError" $ \_ -> do
322− let (getGetWrongHost :<|> _) = client api (BaseUrl Http "127.0.0.1" 19872 "") manager
323− Left res <- runExceptT getGetWrongHost
323+ let (getGetWrongHost :<|> _) = client api
324+ Left res <- SCR.runClientM getGetWrongHost (BaseUrl Http "127.0.0.1" 19872 "") manager
324325 case res of
325326 ConnectionError _ -> return ()
326327 _ -> fail $ "expected ConnectionError, but got " <> show res
327328
328329 it "reports UnsupportedContentType" $ \(_, baseUrl) -> do
329− let (getGet :<|> _ ) = client api baseUrl manager
330− Left res <- runExceptT getGet
330+ let (getGet :<|> _ ) = client api
331+ Left res <- SCR.runClientM getGet baseUrl manager
331332 case res of
332333 UnsupportedContentType ("application/octet-stream") _ -> return ()
333334 _ -> fail $ "expected UnsupportedContentType, but got " <> show res
334335
335336 it "reports InvalidContentTypeHeader" $ \(_, baseUrl) -> do
336− let (_ :<|> _ :<|> _ :<|> getBody :<|> _) = client api baseUrl manager
337− Left res <- runExceptT (getBody alice)
337+ let (_ :<|> _ :<|> _ :<|> getBody :<|> _) = client api
338+ Left res <- SCR.runClientM (getBody alice) baseUrl manager
338339 case res of
339340 InvalidContentTypeHeader "fooooo" _ -> return ()
340341 _ -> fail $ "expected InvalidContentTypeHeader, but got " <> show res
341342
342343data WrappedApi where
343344 WrappedApi :: (HasServer (api :: *) '[], Server api ~ ExceptT ServantErr IO a,
344− HasClient api, Client api ~ ExceptT ServantError IO ()) =>
345+ HasClient api, Client api ~ SCR.ClientM ()) =>
345346 Proxy api -> WrappedApi
346347
347348basicAuthSpec :: Spec
@@ -349,16 +350,16 @@ basicAuthSpec = beforeAll (startWaiApp basicAuthServer) $ afterAll endWaiApp $ d
349350 context "Authentication works when requests are properly authenticated" $ do
350351
351352 it "Authenticates a BasicAuth protected server appropriately" $ \(_,baseUrl) -> do
352− let getBasic = client basicAuthAPI baseUrl manager
353+ let getBasic = client basicAuthAPI
353354 let basicAuthData = BasicAuthData "servant" "server"
354− (left show <$> runExceptT (getBasic basicAuthData)) `shouldReturn` Right alice
355+ (left show <$> SCR.runClientM (getBasic basicAuthData) baseUrl manager) `shouldReturn` Right alice
355356
356357 context "Authentication is rejected when requests are not authenticated properly" $ do
357358
358359 it "Authenticates a BasicAuth protected server appropriately" $ \(_,baseUrl) -> do
359− let getBasic = client basicAuthAPI baseUrl manager
360+ let getBasic = client basicAuthAPI
360361 let basicAuthData = BasicAuthData "not" "password"
361− Left FailureResponse{..} <- runExceptT (getBasic basicAuthData)
362+ Left FailureResponse{..} <- SCR.runClientM (getBasic basicAuthData) baseUrl manager
362363 responseStatus `shouldBe` Status 403 "Forbidden"
363364
364365genAuthSpec :: Spec
@@ -366,16 +367,16 @@ genAuthSpec = beforeAll (startWaiApp genAuthServer) $ afterAll endWaiApp $ do
366367 context "Authentication works when requests are properly authenticated" $ do
367368
368369 it "Authenticates a AuthProtect protected server appropriately" $ \(_, baseUrl) -> do
369− let getProtected = client genAuthAPI baseUrl manager
370+ let getProtected = client genAuthAPI
370371 let authRequest = mkAuthenticateReq () (\_ req -> SCR.addHeader "AuthHeader" ("cool" :: String) req)
371− (left show <$> runExceptT (getProtected authRequest)) `shouldReturn` Right alice
372+ (left show <$> SCR.runClientM (getProtected authRequest) baseUrl manager) `shouldReturn` Right alice
372373
373374 context "Authentication is rejected when requests are not authenticated properly" $ do
374375
375376 it "Authenticates a AuthProtect protected server appropriately" $ \(_, baseUrl) -> do
376− let getProtected = client genAuthAPI baseUrl manager
377+ let getProtected = client genAuthAPI
377378 let authRequest = mkAuthenticateReq () (\_ req -> SCR.addHeader "Wrong" ("header" :: String) req)
378− Left FailureResponse{..} <- runExceptT (getProtected authRequest)
379+ Left FailureResponse{..} <- SCR.runClientM (getProtected authRequest) baseUrl manager
379380 responseStatus `shouldBe` (Status 401 "Unauthorized")
380381
381382-- * utils