on master help sign in
Tests for all reported errors
07d84d0Timo von Holtz
test/Servant/ClientSpec.hs+41−21
@@ -118,19 +118,6 @@ server = serve api (
118withServer :: (BaseUrl -> IO a) -> IO a
118withServer :: (BaseUrl -> IO a) -> IO a
119withServer action = withWaiDaemon (return server) action
119withServer action = withWaiDaemon (return server) action
120
120
121−type FailApi =
122− "get" :> Get '[FormUrlEncoded] Person
123−failApi :: Proxy FailApi
124−failApi = Proxy
125−
126−failServer :: Application
127−failServer = serve failApi (
128− return alice
129− )
130−
131−withFailServer :: (BaseUrl -> IO a) -> IO a
132−withFailServer action = withWaiDaemon (return failServer) action
133−
134getGet :: BaseUrl -> EitherT ServantError IO Person
121getGet :: BaseUrl -> EitherT ServantError IO Person
135getDelete :: BaseUrl -> EitherT ServantError IO ()
122getDelete :: BaseUrl -> EitherT ServantError IO ()
136getCapture :: String -> BaseUrl -> EitherT ServantError IO Person
123getCapture :: String -> BaseUrl -> EitherT ServantError IO Person
@@ -161,6 +148,23 @@ getMultiple :: String -> Maybe Int -> Bool -> [(String, [Rational])]
161 :<|> getMultiple)
148 :<|> getMultiple)
162 = client api
149 = client api
163
150
151+type FailApi =
152+ "get" :> Raw
153+ :<|> "capture" :> Capture "name" String :> Raw
154+ :<|> "body" :> Raw
155+failApi :: Proxy FailApi
156+failApi = Proxy
157+
158+failServer :: Application
159+failServer = serve failApi (
160+ (\ _request respond -> respond $ responseLBS ok200 [] "")
161+ :<|> (\ _capture _request respond -> respond $ responseLBS ok200 [("content-type", "application/json")] "")
162+ :<|> (\_request respond -> respond $ responseLBS ok200 [("content-type", "fooooo")] "")
163+ )
164+
165+withFailServer :: (BaseUrl -> IO a) -> IO a
166+withFailServer action = withWaiDaemon (return failServer) action
167+
164spec :: Spec
168spec :: Spec
165spec = do
169spec = do
166 it "Servant.API.Get" $ withServer $ \ host -> do
170 it "Servant.API.Get" $ withServer $ \ host -> do
@@ -242,20 +246,36 @@ spec = do
242 []
246 []
243
247
244 context "client returns errors appropriately" $ do
248 context "client returns errors appropriately" $ do
245− it "reports connection errors" $ do
246− Right host <- return $ parseBaseUrl "127.0.0.1:987654"
247− Left (ConnectionError (FailedConnectionException2 "127.0.0.1" 987654 False _)) <- runEitherT (getGet host)
248− return ()
249− it "reports non-success responses" $ withFailServer $ \ host -> do
249+ it "reports FailureResponse" $ withFailServer $ \ host -> do
250 Left res <- runEitherT (getDelete host)
250 Left res <- runEitherT (getDelete host)
251 case res of
251 case res of
252 FailureResponse (Status 404 "Not Found") _ _ -> return ()
252 FailureResponse (Status 404 "Not Found") _ _ -> return ()
253 _ -> fail $ "expected 404 response, but got " <> show res
253 _ -> fail $ "expected 404 response, but got " <> show res
254+
254− it "reports unsupported content types" $ withFailServer $ \ host -> do
255+ it "reports DecodeFailure" $ withFailServer $ \ host -> do
256+ Left res <- runEitherT (getCapture "foo" host)
257+ case res of
258+ DecodeFailure _ ("application/json") _ -> return ()
259+ _ -> fail $ "expected DecodeFailure, but got " <> show res
260+
261+ it "reports ConnectionError" $ do
262+ Right host <- return $ parseBaseUrl "127.0.0.1:987654"
255 Left res <- runEitherT (getGet host)
263 Left res <- runEitherT (getGet host)
256 case res of
264 case res of
257− FailureResponse (Status 404 "Not Found") _ _ -> return ()
265+ ConnectionError (FailedConnectionException2 "127.0.0.1" 987654 False _) -> return ()
266+ _ -> fail $ "expected ConnectionError, but got " <> show res
267+
268+ it "reports UnsupportedContentType" $ withFailServer $ \ host -> do
269+ Left res <- runEitherT (getGet host)
270+ case res of
271+ UnsupportedContentType ("application/octet-stream") _ -> return ()
272+ _ -> fail $ "expected UnsupportedContentType, but got " <> show res
273+
274+ it "reports InvalidContentTypeHeader" $ withFailServer $ \ host -> do
275+ Left res <- runEitherT (getBody alice host)
276+ case res of
277+ InvalidContentTypeHeader "fooooo" _ -> return ()
258− _ -> fail $ "expected 404 response, but got " <> show res
278+ _ -> fail $ "expected InvalidContentTypeHeader, but got " <> show res
259
279
260data WrappedApi where
280data WrappedApi where
261 WrappedApi :: (HasServer api, Server api ~ EitherT (Int, String) IO a,
281 WrappedApi :: (HasServer api, Server api ~ EitherT (Int, String) IO a,