Merge pull request #24 from declension/23-fix-queryparams

Fix multiple QueryParams / QueryFlags
This commit is contained in:
Julian Arni 2017-03-10 15:31:39 -06:00 committed by GitHub
commit be5909d30f
2 changed files with 46 additions and 7 deletions

View File

@ -15,9 +15,8 @@ import Servant
import Servant.API.ContentTypes (AllMimeRender (..)) import Servant.API.ContentTypes (AllMimeRender (..))
import Servant.Client (BaseUrl (..), Scheme (..)) import Servant.Client (BaseUrl (..), Scheme (..))
import Test.QuickCheck (Arbitrary (..), Gen, elements, oneof) import Test.QuickCheck (Arbitrary (..), Gen, elements, oneof)
#if MIN_VERSION_servant(0,8,0)
import qualified Data.ByteString as BS import qualified Data.ByteString as BS
#endif
class HasGenRequest a where class HasGenRequest a where
@ -91,9 +90,10 @@ instance (KnownSymbol x, Arbitrary c, ToHttpApiData c, HasGenRequest b)
genRequest _ = do genRequest _ = do
new' <- new new' <- new
old' <- old old' <- old
return $ \burl -> let r = old' burl in r { return $ \burl -> let r = old' burl
queryString = queryString r newExpr = param <> "=" <> cs (toQueryParam new')
<> param <> "=" <> cs (toQueryParam new') } qs = queryString r in r {
queryString = if BS.null qs then newExpr else newExpr <> "&" <> qs }
where where
old = genRequest (Proxy :: Proxy b) old = genRequest (Proxy :: Proxy b)
param = cs $ symbolVal (Proxy :: Proxy x) param = cs $ symbolVal (Proxy :: Proxy x)
@ -118,8 +118,9 @@ instance (KnownSymbol x, HasGenRequest b)
=> HasGenRequest (QueryFlag x :> b) where => HasGenRequest (QueryFlag x :> b) where
genRequest _ = do genRequest _ = do
old' <- old old' <- old
return $ \burl -> let r = old' burl in r { return $ \burl -> let r = old' burl
queryString = queryString r <> param <> "=" } qs = queryString r in r {
queryString = if BS.null qs then param else param <> "&" <> qs }
where where
old = genRequest (Proxy :: Proxy b) old = genRequest (Proxy :: Proxy b)
param = cs $ symbolVal (Proxy :: Proxy x) param = cs $ symbolVal (Proxy :: Proxy x)

View File

@ -4,12 +4,16 @@ module Servant.QuickCheck.InternalSpec (spec) where
import Control.Concurrent.MVar (newMVar, readMVar, swapMVar) import Control.Concurrent.MVar (newMVar, readMVar, swapMVar)
import Control.Monad.IO.Class (liftIO) import Control.Monad.IO.Class (liftIO)
import qualified Data.ByteString as BS import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as C
import Prelude.Compat import Prelude.Compat
import Servant import Servant
import Test.Hspec (Spec, context, describe, it, shouldBe, import Test.Hspec (Spec, context, describe, it, shouldBe,
shouldContain) shouldContain)
import Test.Hspec.Core.Spec (Arg, Example, Result (..), import Test.Hspec.Core.Spec (Arg, Example, Result (..),
defaultParams, evaluateExample) defaultParams, evaluateExample)
import Test.QuickCheck.Gen (unGen)
import Test.QuickCheck.Random (mkQCGen)
import Network.HTTP.Client (queryString)
#if MIN_VERSION_servant(0,8,0) #if MIN_VERSION_servant(0,8,0)
import Servant.API.Internal.Test.ComprehensiveAPI (comprehensiveAPIWithoutRaw) import Servant.API.Internal.Test.ComprehensiveAPI (comprehensiveAPIWithoutRaw)
@ -28,6 +32,8 @@ spec = do
isComprehensiveSpec isComprehensiveSpec
onlyJsonObjectSpec onlyJsonObjectSpec
notLongerThanSpec notLongerThanSpec
queryParamsSpec
queryFlagsSpec
serversEqualSpec :: Spec serversEqualSpec :: Spec
serversEqualSpec = describe "serversEqual" $ do serversEqualSpec = describe "serversEqual" $ do
@ -107,6 +113,27 @@ isComprehensiveSpec = describe "HasGenRequest" $ do
let _g = genRequest comprehensiveAPIWithoutRaw let _g = genRequest comprehensiveAPIWithoutRaw
True `shouldBe` True -- This is a type-level check True `shouldBe` True -- This is a type-level check
queryParamsSpec :: Spec
queryParamsSpec = describe "QueryParams" $ do
it "reduce to an HTTP query string correctly" $ do
let rng = mkQCGen 0
burl = BaseUrl Http "localhost" 80 ""
gen = genRequest paramsAPI
req = (unGen gen rng 0) burl
qs = C.unpack $ queryString req
qs `shouldBe` "one=_&two=_"
queryFlagsSpec :: Spec
queryFlagsSpec = describe "QueryFlags" $ do
it "reduce to an HTTP query string correctly" $ do
let rng = mkQCGen 0
burl = BaseUrl Http "localhost" 80 ""
gen = genRequest flagsAPI
req = (unGen gen rng 0) burl
qs = C.unpack $ queryString req
qs `shouldBe` "one&two"
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
-- APIs -- APIs
@ -119,6 +146,17 @@ type API = ReqBody '[JSON] String :> Post '[JSON] String
api :: Proxy API api :: Proxy API
api = Proxy api = Proxy
type ParamsAPI = QueryParam "one" () :> QueryParam "two" () :> Get '[JSON] ()
paramsAPI :: Proxy ParamsAPI
paramsAPI = Proxy
type FlagsAPI = QueryFlag "one" :> QueryFlag "two" :> Get '[JSON] ()
flagsAPI :: Proxy FlagsAPI
flagsAPI = Proxy
server :: IO (Server API) server :: IO (Server API)
server = do server = do
mvar <- newMVar "" mvar <- newMVar ""