Skip to content

Commit

Permalink
Remove instances of string-conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
tchoutri committed Apr 30, 2024
1 parent 28cfb32 commit ec3058d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 31 deletions.
30 changes: 14 additions & 16 deletions servant-quickcheck/src/Servant/QuickCheck/Internal/ErrorTypes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Servant.QuickCheck.Internal.ErrorTypes where

import Control.Exception (Exception (..))
import qualified Data.ByteString.Lazy as LBS
import Data.String.Conversions (cs)
import qualified Data.ByteString.Char8 as BS8
import qualified Data.Text as T
import Data.Typeable (Typeable)
import GHC.Generics (Generic)
Expand All @@ -12,6 +12,7 @@ import Network.HTTP.Types (Header, statusCode)
import Text.PrettyPrint

import Prelude.Compat hiding ((<>))
import qualified Data.ByteString.Lazy.Char8 as BSL8

data PredicateFailure
= PredicateFailure T.Text (Maybe C.Request) (C.Response LBS.ByteString)
Expand Down Expand Up @@ -42,37 +43,34 @@ prettyHeaders hdrs = vcat $ prettyHdr <$> hdrs

prettyReq :: C.Request -> Doc
prettyReq r =
text "Request:" $$ (nest 5 $
text "Method:" <+> (nest 5 $ text . show $ C.method r)
$$ text "Path:" <+> (nest 5 $ text . cs $ C.path r)
$$ text "Headers:" <+> (nest 5 $ prettyHeaders $ C.requestHeaders r)
$$ text "Body:" <+> (nest 5 $ text . getReqBody $ C.requestBody r))
text "Request:" $$ nest 5 (text "Method:" <+> nest 5 (text . show $ C.method r)
$$ text "Path:" <+> nest 5 (text . BS8.unpack $ C.path r)
$$ text "Headers:" <+> nest 5 (prettyHeaders $ C.requestHeaders r)
$$ text "Body:" <+> nest 5 (text . getReqBody $ C.requestBody r))
where
getReqBody (C.RequestBodyLBS lbs ) = cs lbs
getReqBody (C.RequestBodyBS bs ) = cs bs
getReqBody :: C.RequestBody -> String
getReqBody (C.RequestBodyLBS lbs ) = BSL8.unpack lbs
getReqBody (C.RequestBodyBS bs ) = BS8.unpack bs
getReqBody _ = error "expected bytestring body"

prettyResp :: C.Response LBS.ByteString -> Doc
prettyResp r =
text "Response:" $$ (nest 5 $
text "Status code:" <+> (nest 5 $ text . show . statusCode $ C.responseStatus r)
$$ text "Headers:" $$ (nest 10 $ prettyHeaders $ C.responseHeaders r)
$$ text "Body:" <+> (nest 5 $ text . cs $ C.responseBody r))
text "Response:" $$ nest 5 (text "Status code:" <+> nest 5 (text . show . statusCode $ C.responseStatus r)
$$ text "Headers:" $$ nest 10 (prettyHeaders $ C.responseHeaders r)
$$ text "Body:" <+> nest 5 (text . BSL8.unpack $ C.responseBody r))



prettyServerEqualityFailure :: ServerEqualityFailure -> Doc
prettyServerEqualityFailure (ServerEqualityFailure req resp1 resp2) =
text "Server equality failed" $$ (nest 5 $
prettyReq req
text "Server equality failed" $$ nest 5 (prettyReq req
$$ prettyResp resp1
$$ prettyResp resp2)


prettyPredicateFailure :: PredicateFailure -> Doc
prettyPredicateFailure (PredicateFailure predicate req resp) =
text "Predicate failed" $$ (nest 5 $
text "Predicate:" <+> (text $ T.unpack predicate)
text "Predicate failed" $$ nest 5 (text "Predicate:" <+> text (T.unpack predicate)
$$ r
$$ prettyResp resp)
where
Expand Down
25 changes: 13 additions & 12 deletions servant-quickcheck/src/Servant/QuickCheck/Internal/HasGenRequest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module Servant.QuickCheck.Internal.HasGenRequest where

import Data.Kind (Type)
import Data.String (fromString)
import Data.String.Conversions (cs)
import qualified Data.Text.Encoding as Text
import qualified Data.ByteString.Char8 as BS8
import GHC.TypeLits (KnownSymbol, Nat, symbolVal)
import Network.HTTP.Client (Request, RequestBody (..),
defaultRequest, host, method, path,
Expand Down Expand Up @@ -65,7 +66,7 @@ instance (KnownSymbol path, HasGenRequest b) => HasGenRequest (path :> b) where
in r { path = "/" <> BS.intercalate "/" paths })
where
(oldf, old) = genRequest (Proxy :: Proxy b)
new = cs $ symbolVal (Proxy :: Proxy path)
new = BS8.pack $ symbolVal (Proxy :: Proxy path)

instance HasGenRequest EmptyAPI where
genRequest _ = (0, error "EmptyAPIs cannot be queried.")
Expand All @@ -80,7 +81,7 @@ instance (Arbitrary c, HasGenRequest b, ToHttpApiData c )
genRequest _ = (oldf, do
old' <- old
new' <- toUrlPiece <$> new
return $ \burl -> let r = old' burl in r { path = cs new' <> path r })
return $ \burl -> let r = old' burl in r { path = Text.encodeUtf8 new' <> path r })
where
(oldf, old) = genRequest (Proxy :: Proxy b)
new = arbitrary :: Gen c
Expand All @@ -89,7 +90,7 @@ instance (Arbitrary c, HasGenRequest b, ToHttpApiData c )
=> HasGenRequest (CaptureAll x c :> b) where
genRequest _ = (oldf, do
old' <- old
new' <- fmap (cs . toUrlPiece) <$> new
new' <- fmap (Text.encodeUtf8 . toUrlPiece) <$> new
let new'' = BS.intercalate "/" new'
return $ \burl -> let r = old' burl in r { path = new'' <> path r })
where
Expand All @@ -102,7 +103,7 @@ instance (Arbitrary c, KnownSymbol h, HasGenRequest b, ToHttpApiData c)
old' <- old
new' <- toUrlPiece <$> new -- TODO: generate lenient or/and optional
return $ \burl -> let r = old' burl in r {
requestHeaders = (hdr, cs new') : requestHeaders r })
requestHeaders = (hdr, Text.encodeUtf8 new') : requestHeaders r })
where
(oldf, old) = genRequest (Proxy :: Proxy b)
hdr = fromString $ symbolVal (Proxy :: Proxy h)
Expand All @@ -128,12 +129,12 @@ instance (KnownSymbol x, Arbitrary c, ToHttpApiData c, HasGenRequest b)
new' <- new -- TODO: generate lenient or/and optional
old' <- old
return $ \burl -> let r = old' burl
newExpr = param <> "=" <> cs (toQueryParam new')
newExpr = param <> "=" <> Text.encodeUtf8 (toQueryParam new')
qs = queryString r in r {
queryString = if BS.null qs then newExpr else newExpr <> "&" <> qs })
where
(oldf, old) = genRequest (Proxy :: Proxy b)
param = cs $ symbolVal (Proxy :: Proxy x)
param = BS8.pack $ symbolVal (Proxy :: Proxy x)
new = arbitrary :: Gen c

instance (KnownSymbol x, Arbitrary c, ToHttpApiData c, HasGenRequest b)
Expand All @@ -146,9 +147,9 @@ instance (KnownSymbol x, Arbitrary c, ToHttpApiData c, HasGenRequest b)
<> if not (null new') then fold (toParam <$> new') else ""})
where
(oldf, old) = genRequest (Proxy :: Proxy b)
param = cs $ symbolVal (Proxy :: Proxy x)
param = BS8.pack $ symbolVal (Proxy :: Proxy x)
new = arbitrary :: Gen [c]
toParam c = param <> "[]=" <> cs (toQueryParam c)
toParam c = param <> "[]=" <> Text.encodeUtf8 (toQueryParam c)
fold = foldr1 (\a b -> a <> "&" <> b)

instance (KnownSymbol x, HasGenRequest b)
Expand All @@ -160,12 +161,12 @@ instance (KnownSymbol x, HasGenRequest b)
queryString = if BS.null qs then param else param <> "&" <> qs })
where
(oldf, old) = genRequest (Proxy :: Proxy b)
param = cs $ symbolVal (Proxy :: Proxy x)
param = BS8.pack $ symbolVal (Proxy :: Proxy x)

instance (ReflectMethod method)
=> HasGenRequest (Verb (method :: k) (status :: Nat) (cts :: [Type]) a) where
genRequest _ = (1, return $ \burl -> defaultRequest
{ host = cs $ baseUrlHost burl
{ host = BS8.pack $ baseUrlHost burl
, port = baseUrlPort burl
, secure = baseUrlScheme burl == Https
, method = reflectMethod (Proxy :: Proxy method)
Expand All @@ -174,7 +175,7 @@ instance (ReflectMethod method)
instance (ReflectMethod method)
=> HasGenRequest (NoContentVerb (method :: k)) where
genRequest _ = (1, return $ \burl -> defaultRequest
{ host = cs $ baseUrlHost burl
{ host = BS8.pack $ baseUrlHost burl
, port = baseUrlPort burl
, secure = baseUrlScheme burl == Https
, method = reflectMethod (Proxy :: Proxy method)
Expand Down
5 changes: 2 additions & 3 deletions servant/test/Servant/API/ContentTypesSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ spec = describe "Servant.API.ContentTypes" $ do
let p = Proxy :: Proxy '[JSON]

it "does not render any content" $
allMimeRender p NoContent `shouldSatisfy` (all (BSL8.null . snd))
allMimeRender p NoContent `shouldSatisfy` all (BSL8.null . snd)

it "evaluates the NoContent value" $
evaluate (allMimeRender p (undefined :: NoContent)) `shouldThrow` anyErrorCall
Expand Down Expand Up @@ -155,8 +155,7 @@ spec = describe "Servant.API.ContentTypes" $ do
#endif
let acceptH a b c = addToAccept (Proxy :: Proxy OctetStream) a $
addToAccept (Proxy :: Proxy JSON) b $
addToAccept (Proxy :: Proxy PlainText ) c $
""
addToAccept (Proxy :: Proxy PlainText ) c ""
let val a b c i = handleAcceptH (Proxy :: Proxy '[OctetStream, JSON, PlainText])
(acceptH a b c) (i :: Int)
property $ \a b c i ->
Expand Down

0 comments on commit ec3058d

Please sign in to comment.