http-env-gen-0.0.0.0: test/Network/HTTP/Env/ManagerSpec.hs
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Network.HTTP.Env.ManagerSpec (spec) where
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Lazy as LB
import Network.HTTP.Client (HttpException, ManagerSettings (..), httpLbs, newManager, parseRequest, requestHeaders, responseBody)
import Network.HTTP.Client.TLS (tlsManagerSettings)
import Network.HTTP.Env.Manager (tlsManagerSettingsWithMaxHeaderLength, withUserAgent)
import Network.HTTP.Types (status200)
import Network.Wai (responseLBS)
import qualified Network.Wai as Wai
import qualified Network.Wai.Handler.Warp as Warp
import Test.Syd
import UnliftIO (try)
spec :: Spec
spec = do
describe "tlsManagerSettingsWithMaxHeaderLength" $ do
it "reads a response carrying a header field up to the limit" $ do
man <- newManager (tlsManagerSettingsWithMaxHeaderLength (16 * 1024))
let application _ respond =
respond $
responseLBS
status200
[("Content-Security-Policy", ByteString.replicate (9 * 1024) 0x78)]
"hello"
Warp.testWithApplication (pure application) $ \port -> do
request <- parseRequest (concat ["http://127.0.0.1:", show port])
response <- httpLbs request man
responseBody response `shouldBe` "hello"
it "refuses a response carrying a header field past the limit" $ do
man <- newManager (tlsManagerSettingsWithMaxHeaderLength 1024)
let application _ respond =
respond $
responseLBS
status200
[("Content-Security-Policy", ByteString.replicate (64 * 1024) 0x78)]
"hello"
Warp.testWithApplication (pure application) $ \port -> do
request <- parseRequest (concat ["http://127.0.0.1:", show port])
result <- try (httpLbs request man)
case result of
Left (_ :: HttpException) -> pure ()
Right _ -> expectationFailure "the overlong header was read"
describe "withUserAgent" $ do
it "sends its user agent in place of the one a request already carries" $ do
man <- newManager (withUserAgent "some-agent" tlsManagerSettings)
let application request respond =
respond $
responseLBS
status200
[]
(LB.fromStrict (ByteString.intercalate "," [v | ("User-Agent", v) <- Wai.requestHeaders request]))
Warp.testWithApplication (pure application) $ \port -> do
request <- parseRequest (concat ["http://127.0.0.1:", show port])
response <- httpLbs request {requestHeaders = [("User-Agent", "something-else")]} man
responseBody response `shouldBe` "some-agent"
it "keeps the request modifier of the settings it is given" $ do
let inner =
tlsManagerSettings
{ managerModifyRequest = \request ->
-- Replacing rather than adding, because http-client may run a
-- modifier more than once on one request.
pure request {requestHeaders = ("X-Inner", "1") : filter ((/= "X-Inner") . fst) (requestHeaders request)}
}
man <- newManager (withUserAgent "some-agent" inner)
let application request respond =
respond $
responseLBS
status200
[]
( LB.fromStrict
( ByteString.intercalate
","
[ v
| (name, v) <- Wai.requestHeaders request,
name `elem` ["User-Agent", "X-Inner"]
]
)
)
Warp.testWithApplication (pure application) $ \port -> do
request <- parseRequest (concat ["http://127.0.0.1:", show port])
response <- httpLbs request man
responseBody response `shouldBe` "1,some-agent"