@@ -118,19 +118,6 @@ server = serve api (
118118withServer :: (BaseUrl -> IO a) -> IO a
119119withServer action = withWaiDaemon (return server) action
120120
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−
134121getGet :: BaseUrl -> EitherT ServantError IO Person
135122getDelete :: BaseUrl -> EitherT ServantError IO ()
136123getCapture :: String -> BaseUrl -> EitherT ServantError IO Person
@@ -161,6 +148,23 @@ getMultiple :: String -> Maybe Int -> Bool -> [(String, [Rational])]
161148 :<|> getMultiple)
162149 = client api
163150
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+
164168spec :: Spec
165169spec = do
166170 it "Servant.API.Get" $ withServer $ \ host -> do
@@ -242,20 +246,36 @@ spec = do
242246 []
243247
244248 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
250250 Left res <- runEitherT (getDelete host)
251251 case res of
252252 FailureResponse (Status 404 "Not Found") _ _ -> return ()
253253 _ -> fail $ "expected 404 response, but got " <> show res
254− it "reports unsupported content types" $ withFailServer $ \ host -> do
254+
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"
255263 Left res <- runEitherT (getGet host)
256264 case res of
257− FailureResponse (Status 404 "Not Found") _ _ -> return ()
258− _ -> fail $ "expected 404 response, but got " <> show res
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 ()
278+ _ -> fail $ "expected InvalidContentTypeHeader, but got " <> show res
259279
260280data WrappedApi where
261281 WrappedApi :: (HasServer api, Server api ~ EitherT (Int, String) IO a,