on master help sign in
Merge pull request #17 from haskell-servant/raw-fullresponse

Return complete `Response` in client for Raw endpoint

commit989849c8a1e6262401bf3e928c46c64d1da2384d authorAlp Mestanogullari <alpmestan@gmail.com> dateThu 26 Mar 2015, 12:55 (12 years ago) parentac733beecb8627
4 files changed+29−16
servant-client.cabal+1 src/Servant/Client.hs+4−3 src/Servant/Common/Req.hs+3−3 test/Servant/ClientSpec.hs+21−10
servant-client.cabal+1
@@ -74,6 +74,7 @@ test-suite spec
7474 , http-client
7575 , http-media
7676 , http-types
77+ , HUnit
7778 , network >= 2.6
7879 , QuickCheck >= 2.7
7980 , servant >= 0.2.1
src/Servant/Client.hs+4−3
@@ -23,6 +23,7 @@ import Data.Proxy
2323import Data.String.Conversions
2424import Data.Text (unpack)
2525import GHC.TypeLits
26+import Network.HTTP.Client (Response)
2627import Network.HTTP.Media
2728import qualified Network.HTTP.Types as H
2829import Servant.API
@@ -411,12 +412,12 @@ instance (KnownSymbol sym, HasClient sublayout)
411412 where paramname = cs $ symbolVal (Proxy :: Proxy sym)
412413
413414-- | Pick a 'Method' and specify where the server you want to query is. You get
414−-- back the status code and the response body as a 'ByteString'.
415+-- back the full `Response`.
415416instance HasClient Raw where
416− type Client Raw = H.Method -> BaseUrl -> EitherT ServantError IO (Int, ByteString, MediaType)
417+ type Client Raw = H.Method -> BaseUrl -> EitherT ServantError IO (Int, ByteString, MediaType, Response ByteString)
417418
418419 clientWithRoute :: Proxy Raw -> Req -> Client Raw
419− clientWithRoute Proxy req httpMethod host =
420+ clientWithRoute Proxy req httpMethod host = do
420421 performRequest httpMethod req (const True) host
421422
422423-- | If you use a 'ReqBody' in one of your endpoints in your API,
src/Servant/Common/Req.hs+3−3
@@ -133,7 +133,7 @@ displayHttpRequest :: Method -> String
133133displayHttpRequest httpmethod = "HTTP " ++ cs httpmethod ++ " request"
134134
135135
136−performRequest :: Method -> Req -> (Int -> Bool) -> BaseUrl -> EitherT ServantError IO (Int, ByteString, MediaType)
136+performRequest :: Method -> Req -> (Int -> Bool) -> BaseUrl -> EitherT ServantError IO (Int, ByteString, MediaType, Response ByteString)
137137performRequest reqMethod req isWantedStatus reqHost = do
138138 partialRequest <- liftIO $ reqToRequest req reqHost
139139
@@ -159,13 +159,13 @@ performRequest reqMethod req isWantedStatus reqHost = do
159159 Just t' -> pure t'
160160 unless (isWantedStatus status_code) $
161161 left $ FailureResponse status ct body
162− return (status_code, body, ct)
162+ return (status_code, body, ct, response)
163163
164164performRequestCT :: MimeUnrender ct result =>
165165 Proxy ct -> Method -> Req -> [Int] -> BaseUrl -> EitherT ServantError IO result
166166performRequestCT ct reqMethod req wantedStatus reqHost = do
167167 let acceptCT = contentType ct
168− (_status, respBody, respCT) <-
168+ (_status, respBody, respCT, _response) <-
169169 performRequest reqMethod (req { reqAccept = [acceptCT] }) (`elem` wantedStatus) reqHost
170170 unless (matches respCT (acceptCT)) $
171171 left $ UnsupportedContentType respCT respBody
test/Servant/ClientSpec.hs+21−10
@@ -23,14 +23,15 @@ import Data.Monoid
2323import Data.Proxy
2424import qualified Data.Text as T
2525import GHC.Generics
26−import Network.HTTP.Client (HttpException(..))
26+import qualified Network.HTTP.Client as C
2727import Network.HTTP.Media
2828import Network.HTTP.Types
2929import Network.Socket
30−import Network.Wai
30+import Network.Wai hiding (Response)
3131import Network.Wai.Handler.Warp
3232import Test.Hspec
3333import Test.Hspec.QuickCheck
34+import Test.HUnit
3435import Test.QuickCheck
3536
3637import Servant.API
@@ -64,7 +65,7 @@ instance FromFormUrlEncoded Person where
6465
6566deriving instance Eq ServantError
6667
67−instance Eq HttpException where
68+instance Eq C.HttpException where
6869 a == b = show a == show b
6970
7071alice :: Person
@@ -128,8 +129,8 @@ getQueryFlag :: Bool -> BaseUrl -> EitherT ServantError IO Bool
128129getMatrixParam :: Maybe String -> BaseUrl -> EitherT ServantError IO Person
129130getMatrixParams :: [String] -> BaseUrl -> EitherT ServantError IO [Person]
130131getMatrixFlag :: Bool -> BaseUrl -> EitherT ServantError IO Bool
131−getRawSuccess :: Method -> BaseUrl -> EitherT ServantError IO (Int, ByteString, MediaType)
132−getRawFailure :: Method -> BaseUrl -> EitherT ServantError IO (Int, ByteString, MediaType)
132+getRawSuccess :: Method -> BaseUrl -> EitherT ServantError IO (Int, ByteString, MediaType, C.Response ByteString)
133+getRawFailure :: Method -> BaseUrl -> EitherT ServantError IO (Int, ByteString, MediaType, C.Response ByteString)
133134getMultiple :: String -> Maybe Int -> Bool -> [(String, [Rational])]
134135 -> BaseUrl
135136 -> EitherT ServantError IO (String, Maybe Int, Bool, [(String, [Rational])])
@@ -211,12 +212,22 @@ spec = do
211212 Arrow.left show <$> runEitherT (getMatrixFlag flag host) `shouldReturn` Right flag
212213
213214 it "Servant.API.Raw on success" $ withServer $ \ host -> do
214− (Arrow.left show <$> runEitherT (getRawSuccess methodGet host))
215− `shouldReturn` Right (200, "rawSuccess", "application"//"octet-stream")
215+ res <- runEitherT (getRawSuccess methodGet host)
216+ case res of
217+ Left e -> assertFailure $ show e
218+ Right (code, body, ct, response) -> do
219+ (code, body, ct) `shouldBe` (200, "rawSuccess", "application"//"octet-stream")
220+ C.responseBody response `shouldBe` body
221+ C.responseStatus response `shouldBe` ok200
216222
217223 it "Servant.API.Raw on failure" $ withServer $ \ host -> do
218− (Arrow.left show <$> runEitherT (getRawFailure methodGet host))
219− `shouldReturn` Right (400, "rawFailure", "application"//"octet-stream")
224+ res <- runEitherT (getRawFailure methodGet host)
225+ case res of
226+ Left e -> assertFailure $ show e
227+ Right (code, body, ct, response) -> do
228+ (code, body, ct) `shouldBe` (400, "rawFailure", "application"//"octet-stream")
229+ C.responseBody response `shouldBe` body
230+ C.responseStatus response `shouldBe` badRequest400
220231
221232 modifyMaxSuccess (const 20) $ do
222233 it "works for a combination of Capture, QueryParam, QueryFlag and ReqBody" $
@@ -262,7 +273,7 @@ spec = do
262273 Right host <- return $ parseBaseUrl "127.0.0.1:987654"
263274 Left res <- runEitherT (getGet host)
264275 case res of
265− ConnectionError (FailedConnectionException2 "127.0.0.1" 987654 False _) -> return ()
276+ ConnectionError (C.FailedConnectionException2 "127.0.0.1" 987654 False _) -> return ()
266277 _ -> fail $ "expected ConnectionError, but got " <> show res
267278
268279 it "reports UnsupportedContentType" $ withFailServer $ \ host -> do