Fix Request's Show instance (#1492)

This commit is contained in:
antoine-fl 2021-12-01 19:16:59 +01:00 committed by GitHub
parent a975cfc361
commit cb294aa2b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View file

@ -78,12 +78,13 @@ instance (Show a, Show b) =>
. showString ", requestAccept = " . showString ", requestAccept = "
. showsPrec 0 (requestAccept req) . showsPrec 0 (requestAccept req)
. showString ", requestHeaders = " . showString ", requestHeaders = "
. showsPrec 0 (redactSensitiveHeader <$> requestHeaders req)) . showsPrec 0 (redactSensitiveHeader <$> requestHeaders req)
. showString ", requestHttpVersion = " . showString ", requestHttpVersion = "
. showsPrec 0 (requestHttpVersion req) . showsPrec 0 (requestHttpVersion req)
. showString ", requestMethod = " . showString ", requestMethod = "
. showsPrec 0 (requestMethod req) . showsPrec 0 (requestMethod req)
. showString "}" . showString "}"
)
where where
redactSensitiveHeader :: Header -> Header redactSensitiveHeader :: Header -> Header
redactSensitiveHeader ("Authorization", _) = ("Authorization", "<REDACTED>") redactSensitiveHeader ("Authorization", _) = ("Authorization", "<REDACTED>")

View file

@ -10,10 +10,22 @@ import Data.List (isInfixOf)
import Servant.Client.Core.Request import Servant.Client.Core.Request
import Test.Hspec import Test.Hspec
newtype DataWithRequest = DataWithRequest (RequestF RequestBody ())
deriving Show
spec :: Spec spec :: Spec
spec = do spec = do
describe "Request" $ do describe "Request" $ do
describe "show" $ do describe "show" $ do
it "has parenthesis correctly positioned" $ do
let d = DataWithRequest (void defaultRequest)
show d `shouldBe` "DataWithRequest (Request {requestPath = ()\
\, requestQueryString = fromList []\
\, requestBody = Nothing\
\, requestAccept = fromList []\
\, requestHeaders = fromList []\
\, requestHttpVersion = HTTP/1.1\
\, requestMethod = \"GET\"})"
it "redacts the authorization header" $ do it "redacts the authorization header" $ do
let request = void $ defaultRequest { requestHeaders = pure ("authorization", "secret") } let request = void $ defaultRequest { requestHeaders = pure ("authorization", "secret") }
isInfixOf "secret" (show request) `shouldBe` False isInfixOf "secret" (show request) `shouldBe` False