mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 01:12:56 +03:00
api-tests: If a GET request fails with the wrong status code, print the response.
It's pretty frustrating to see an error in CI and not know the actual cause, because we just dropped the information. This adds the actual status code and body to the error message. Previously, `getWithStatus` was only used by the `healthCheck'` function. This also refactors `get_` to use the same function, so we don't have to duplicate the error-handling logic. PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7752 GitOrigin-RevId: 474e4c02ad6c5b676abc311b90b21998b4a93d94
This commit is contained in:
parent
67bc017cf3
commit
5dbca897c6
@ -12,11 +12,14 @@ module Harness.Http
|
|||||||
)
|
)
|
||||||
where
|
where
|
||||||
|
|
||||||
|
import Conduit (foldMapC, runConduit, (.|))
|
||||||
import Control.Concurrent.Extended (sleep)
|
import Control.Concurrent.Extended (sleep)
|
||||||
import Control.Exception
|
import Control.Exception
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
import Data.ByteString.Lazy.Char8 qualified as L8
|
import Data.ByteString.Lazy.Char8 qualified as L8
|
||||||
import Data.String
|
import Data.String
|
||||||
|
import Data.Text qualified as T
|
||||||
|
import Data.Text.Encoding qualified as T
|
||||||
import GHC.Stack
|
import GHC.Stack
|
||||||
import Hasura.Prelude
|
import Hasura.Prelude
|
||||||
import Network.HTTP.Simple qualified as Http
|
import Network.HTTP.Simple qualified as Http
|
||||||
@ -28,18 +31,25 @@ import Network.HTTP.Types qualified as Http
|
|||||||
-- | Performs get, doesn't return the result. Simply throws if there's
|
-- | Performs get, doesn't return the result. Simply throws if there's
|
||||||
-- not a 200 response.
|
-- not a 200 response.
|
||||||
get_ :: HasCallStack => String -> IO ()
|
get_ :: HasCallStack => String -> IO ()
|
||||||
get_ url = do
|
get_ = getWithStatus [200]
|
||||||
response <- Http.httpNoBody (fromString url)
|
|
||||||
unless (Http.getResponseStatusCode response == 200) $
|
|
||||||
error ("Non-200 response code from HTTP request: " ++ url)
|
|
||||||
|
|
||||||
-- | Performs get, doesn't return the result. Simply throws if there's
|
-- | Performs get, doesn't return the result. Simply throws if there's
|
||||||
-- not an expected response status code.
|
-- not an expected response status code.
|
||||||
getWithStatus :: HasCallStack => [Int] -> String -> IO ()
|
getWithStatus :: HasCallStack => [Int] -> String -> IO ()
|
||||||
getWithStatus acceptableStatusCodes url = do
|
getWithStatus acceptableStatusCodes url =
|
||||||
response <- Http.httpNoBody (fromString url)
|
Http.withResponse @_ @IO (fromString url) \response -> do
|
||||||
unless (Http.getResponseStatusCode response `elem` acceptableStatusCodes) $
|
let actualStatusCode = Http.getResponseStatusCode response
|
||||||
error ("Unexpected response code from HTTP request: " ++ url ++ ". Expected: " ++ show acceptableStatusCodes)
|
unless (actualStatusCode `elem` acceptableStatusCodes) $ do
|
||||||
|
body <- runConduit $ Http.getResponseBody response .| foldMapC id
|
||||||
|
fail $
|
||||||
|
unlines
|
||||||
|
[ "The HTTP response had an unexpected response code.",
|
||||||
|
"URL: " <> url,
|
||||||
|
"Expected status codes: " <> show acceptableStatusCodes,
|
||||||
|
"Actual status code: " <> show actualStatusCode,
|
||||||
|
"Body:",
|
||||||
|
T.unpack $ T.decodeUtf8 body
|
||||||
|
]
|
||||||
|
|
||||||
-- | Post the JSON to the given URL, and produces a very descriptive
|
-- | Post the JSON to the given URL, and produces a very descriptive
|
||||||
-- exception on failure.
|
-- exception on failure.
|
||||||
@ -98,12 +108,12 @@ healthCheck url = do
|
|||||||
case result of
|
case result of
|
||||||
Healthy -> return ()
|
Healthy -> return ()
|
||||||
Unhealthy failures ->
|
Unhealthy failures ->
|
||||||
error
|
fail $
|
||||||
( "Health check failed for URL: "
|
"Health check failed for URL: "
|
||||||
++ url
|
++ url
|
||||||
++ ", with failures: "
|
++ ", with failures: "
|
||||||
++ show failures
|
++ show failures
|
||||||
)
|
++ "\nIs graphql-engine starting up without errors outside of this test suite?"
|
||||||
|
|
||||||
data HealthCheckResult = Healthy | Unhealthy [Http.HttpException]
|
data HealthCheckResult = Healthy | Unhealthy [Http.HttpException]
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user