@@ -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
122− "get" :> Get '[FormUrlEncoded] Person
123−failApi :: Proxy FailApi
126−failServer :: Application
127−failServer = serve failApi (
131−withFailServer :: (BaseUrl -> IO a) -> IO a
132−withFailServer action = withWaiDaemon (return failServer) action
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
153+ :<|> "capture" :> Capture "name" String :> Raw
155+failApi :: Proxy FailApi
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")] "")
165+withFailServer :: (BaseUrl -> IO a) -> IO a
166+withFailServer action = withWaiDaemon (return failServer) action
164spec :: Spec
168spec :: Spec
166 it "Servant.API.Get" $ withServer $ \ host -> do
170 it "Servant.API.Get" $ withServer $ \ host -> do
@@ -242,20 +246,36 @@ spec = do
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)
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− it "reports unsupported content types" $ withFailServer $ \ host -> do
255+ it "reports DecodeFailure" $ withFailServer $ \ host -> do
256+ Left res <- runEitherT (getCapture "foo" host)
258+ DecodeFailure _ ("application/json") _ -> return ()
259+ _ -> fail $ "expected DecodeFailure, but got " <> show res
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
268+ it "reports UnsupportedContentType" $ withFailServer $ \ host -> do
269+ Left res <- runEitherT (getGet host)
271+ UnsupportedContentType ("application/octet-stream") _ -> return ()
272+ _ -> fail $ "expected UnsupportedContentType, but got " <> show res
274+ it "reports InvalidContentTypeHeader" $ withFailServer $ \ host -> do
275+ Left res <- runEitherT (getBody alice host)
277+ InvalidContentTypeHeader "fooooo" _ -> return ()
258− _ -> fail $ "expected 404 response, but got " <> show res
278+ _ -> fail $ "expected InvalidContentTypeHeader, but got " <> show res
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,