Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
tchoutri committed Apr 30, 2024
1 parent ec3058d commit 67abf0f
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 234 deletions.
20 changes: 0 additions & 20 deletions servant-client/test/Servant/StreamSpec.hs
@@ -1,17 +1,13 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
Expand All @@ -21,12 +17,6 @@

module Servant.StreamSpec (spec) where

import Control.Monad
(when)
import Control.Monad.Codensity
(Codensity (..))
import Control.Monad.IO.Class
(MonadIO (..))
import Control.Monad.Trans.Except
import qualified Data.ByteString as BS
import Data.Proxy
Expand All @@ -45,20 +35,10 @@ import System.Entropy
(getEntropy, getHardwareEntropy)
import System.IO.Unsafe
(unsafePerformIO)
import System.Mem
(performGC)
import Test.Hspec
import Servant.ClientTestUtils (Person(..))
import qualified Servant.ClientTestUtils as CT

#if MIN_VERSION_base(4,10,0)
import GHC.Stats
(gc, gcdetails_live_bytes, getRTSStats)
#else
import GHC.Stats
(currentBytesUsed, getGCStats)
#endif

-- This declaration simply checks that all instances are in place.
-- Note: this is streaming client
_ = client comprehensiveAPI
Expand Down
52 changes: 19 additions & 33 deletions servant-client/test/Servant/SuccessSpec.hs
Expand Up @@ -10,7 +10,6 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -freduction-depth=100 #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
Expand All @@ -22,13 +21,11 @@ import Prelude ()
import Prelude.Compat

import Control.Arrow
((+++), left)
(left)
import Control.Concurrent.STM
(atomically)
import Control.Concurrent.STM.TVar
(newTVar, readTVar)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import Data.Foldable
(forM_, toList)
import Data.Maybe
Expand Down Expand Up @@ -56,30 +53,23 @@ import Servant.Test.ComprehensiveAPI
_ = client comprehensiveAPIWithoutStreaming

spec :: Spec
spec = describe "Servant.SuccessSpec" $ do
successSpec
spec = describe "Servant.SuccessSpec" $ successSpec

successSpec :: Spec
successSpec = beforeAll (startWaiApp server) $ afterAll endWaiApp $ do
describe "Servant.API.Get" $ do
it "get root endpoint" $ \(_, baseUrl) -> do
left show <$> runClient getRoot baseUrl `shouldReturn` Right carol
it "get root endpoint" $ \(_, baseUrl) -> left show <$> runClient getRoot baseUrl `shouldReturn` Right carol

it "get simple endpoint" $ \(_, baseUrl) -> do
left show <$> runClient getGet baseUrl `shouldReturn` Right alice
it "get simple endpoint" $ \(_, baseUrl) -> left show <$> runClient getGet baseUrl `shouldReturn` Right alice

it "get redirection endpoint" $ \(_, baseUrl) -> do
left show <$> runClient getGet307 baseUrl `shouldReturn` Right "redirecting"
it "get redirection endpoint" $ \(_, baseUrl) -> left show <$> runClient getGet307 baseUrl `shouldReturn` Right "redirecting"

describe "Servant.API.Delete" $ do
it "allows empty content type" $ \(_, baseUrl) -> do
left show <$> runClient getDeleteEmpty baseUrl `shouldReturn` Right NoContent
it "allows empty content type" $ \(_, baseUrl) -> left show <$> runClient getDeleteEmpty baseUrl `shouldReturn` Right NoContent

it "allows content type" $ \(_, baseUrl) -> do
left show <$> runClient getDeleteContentType baseUrl `shouldReturn` Right NoContent
it "allows content type" $ \(_, baseUrl) -> left show <$> runClient getDeleteContentType baseUrl `shouldReturn` Right NoContent

it "Servant.API.Capture" $ \(_, baseUrl) -> do
left show <$> runClient (getCapture "Paula") baseUrl `shouldReturn` Right (Person "Paula" 0)
it "Servant.API.Capture" $ \(_, baseUrl) -> left show <$> runClient (getCapture "Paula") baseUrl `shouldReturn` Right (Person "Paula" 0)

it "Servant.API.CaptureAll" $ \(_, baseUrl) -> do
let expected = [Person "Paula" 0, Person "Peta" 1]
Expand Down Expand Up @@ -107,18 +97,15 @@ successSpec = beforeAll (startWaiApp server) $ afterAll endWaiApp $ do
`shouldReturn` Right [Person "alice" 0, Person "bob" 1]

context "Servant.API.QueryParam.QueryFlag" $
forM_ [False, True] $ \ flag -> it (show flag) $ \(_, baseUrl) -> do
left show <$> runClient (getQueryFlag flag) baseUrl `shouldReturn` Right flag
forM_ [False, True] $ \ flag -> it (show flag) $ \(_, baseUrl) -> left show <$> runClient (getQueryFlag flag) baseUrl `shouldReturn` Right flag

it "Servant.API.QueryParam.QueryString" $ \(_, baseUrl) -> do
let qs = [("name", Just "bob"), ("age", Just "1")]
left show <$> runClient (getQueryString qs) baseUrl `shouldReturn` (Right (Person "bob" 1))
left show <$> runClient (getQueryString qs) baseUrl `shouldReturn` Right (Person "bob" 1)

it "Servant.API.QueryParam.DeepQuery" $ \(_, baseUrl) -> do
left show <$> runClient (getDeepQuery $ Filter 1 "bob") baseUrl `shouldReturn` (Right (Person "bob" 1))
it "Servant.API.QueryParam.DeepQuery" $ \(_, baseUrl) -> left show <$> runClient (getDeepQuery $ Filter 1 "bob") baseUrl `shouldReturn` (Right (Person "bob" 1))

it "Servant.API.Fragment" $ \(_, baseUrl) -> do
left id <$> runClient getFragment baseUrl `shouldReturn` Right alice
it "Servant.API.Fragment" $ \(_, baseUrl) -> left id <$> runClient getFragment baseUrl `shouldReturn` Right alice

it "Servant.API.Raw on success" $ \(_, baseUrl) -> do
res <- runClient (getRawSuccess HTTP.methodGet) baseUrl
Expand Down Expand Up @@ -180,13 +167,12 @@ successSpec = beforeAll (startWaiApp server) $ afterAll endWaiApp $ do
Right r ->
("X-Added-Header", "XXX") `elem` toList (responseHeaders r) `shouldBe` True

modifyMaxSuccess (const 20) $ do
it "works for a combination of Capture, QueryParam, QueryFlag and ReqBody" $ \(_, baseUrl) ->
property $ forAllShrink pathGen shrink $ \(NonEmpty cap) num flag body ->
ioProperty $ do
result <- left show <$> runClient (getMultiple cap num flag body) baseUrl
return $
result === Right (cap, num, flag, body)
modifyMaxSuccess (const 20) $ it "works for a combination of Capture, QueryParam, QueryFlag and ReqBody" $ \(_, baseUrl) ->
property $ forAllShrink pathGen shrink $ \(NonEmpty cap) num flag body ->
ioProperty $ do
result <- left show <$> runClient (getMultiple cap num flag body) baseUrl
return $
result === Right (cap, num, flag, body)

context "With a route that can either return success or redirect" $ do
it "Redirects when appropriate" $ \(_, baseUrl) -> do
Expand All @@ -203,7 +189,7 @@ successSpec = beforeAll (startWaiApp server) $ afterAll endWaiApp $ do

context "with a route that uses uverb but only has a single response" $
it "returns the expected response" $ \(_, baseUrl) -> do
eitherResponse <- runClient (uverbGetCreated) baseUrl
eitherResponse <- runClient uverbGetCreated baseUrl
case eitherResponse of
Left clientError -> fail $ show clientError
Right response -> matchUnion response `shouldBe` Just (WithStatus @201 carol)
Expand Down
12 changes: 5 additions & 7 deletions servant-server/test/Servant/Server/ErrorSpec.hs
Expand Up @@ -11,10 +11,8 @@ import Control.Monad
import Data.Aeson
(encode)
import qualified Data.ByteString.Char8 as BC
import qualified Data.ByteString.Lazy.Char8 as BCL
import qualified Data.ByteString.Lazy.Char8 as BSL8
import Data.Proxy
import Data.String.Conversions
(cs)
import Network.HTTP.Types
(hAccept, hAuthorization, hContentType, methodGet, methodPost,
methodPut)
Expand Down Expand Up @@ -299,7 +297,7 @@ errorChoiceSpec = describe "Multiple handlers return errors"
-- * Custom errors {{{

customFormatter :: ErrorFormatter
customFormatter _ _ err = err400 { errBody = "CUSTOM! " <> cs err }
customFormatter _ _ err = err400 { errBody = "CUSTOM! " <> BSL8.pack err }

customFormatters :: ErrorFormatters
customFormatters = defaultErrorFormatters
Expand Down Expand Up @@ -328,7 +326,7 @@ customFormattersSpec = describe "Custom errors from combinators"
let startsWithCustom = ResponseMatcher
{ matchStatus = 400
, matchHeaders = []
, matchBody = MatchBody $ \_ body -> if "CUSTOM!" `BCL.isPrefixOf` body
, matchBody = MatchBody $ \_ body -> if "CUSTOM!" `BSL8.isPrefixOf` body
then Nothing
else Just $ show body <> " does not start with \"CUSTOM!\""
}
Expand All @@ -354,8 +352,8 @@ customFormattersSpec = describe "Custom errors from combinators"
-- * Instances {{{

instance MimeUnrender PlainText Int where
mimeUnrender _ x = maybe (Left "no parse") Right (readMay $ BCL.unpack x)
mimeUnrender _ x = maybe (Left "no parse") Right (readMay $ BSL8.unpack x)

instance MimeRender PlainText Int where
mimeRender _ = BCL.pack . show
mimeRender _ = BSL8.pack . show
-- }}}

0 comments on commit 67abf0f

Please sign in to comment.