packages feed

warp-effectful-1.0.0: test/Main.hs

module Main where

import Data.ByteString.Lazy (LazyByteString)
import Effectful
import Effectful.Hspec
import Effectful.HttpClient
import Effectful.Wai hiding (Response, responseStatus)
import Effectful.Wai.Handler.Warp
import Network.HTTP.Types qualified as HTTP
import Prelude

post :: (HttpClient :> es) => LazyByteString -> Port -> Eff es (Response LazyByteString)
post body port = do
    req <- parseRequest $ "http://127.0.0.1:" <> show port <> "/hello"
    httpLbs
        req
            { method = "POST"
            , requestBody = RequestBodyLBS body
            , requestHeaders = [("Connection", "close")]
            }

main :: IO ()
main = runEff . runHttpClient . runHspec . describe "Warp" $ do
    it "serves a request over a real listener" do
        let app :: Application es
            app _req respond = respond $ responseLBS HTTP.ok200 [] "hello world"
        resp <- withApplication (pure app) (post mempty)
        responseStatus resp `shouldBe` HTTP.ok200

    it "round-trips a request body" do
        let app :: (IOE :> es) => Application es
            app req respond = do
                body <- strictRequestBody req
                respond $ responseLBS HTTP.ok200 [] body
        resp <- withApplication (pure app) (post "hello world")
        responseBody resp `shouldBe` "hello world"

    it "serves through testWithApplicationSettings" do
        let app :: Application es
            app _req respond = respond $ responseLBS HTTP.ok200 [] "hello world"
        (_counter, settings) <- makeSettingsAndCounter
        resp <- testWithApplicationSettings settings (pure app) (post mempty)
        responseStatus resp `shouldBe` HTTP.ok200