add test for using the Config machinery
This commit is contained in:
parent
eafc5d33bb
commit
dcd2c8078c
2 changed files with 73 additions and 2 deletions
|
@ -50,8 +50,8 @@ instance (Eq a, Eq (Config as)) => Eq (Config (a ': as)) where
|
|||
e .:. cfg = ConsConfig (ConfigEntry e) cfg
|
||||
infixr 5 .:.
|
||||
|
||||
class HasConfigEntry (cfg :: [*]) (a :: k) (val :: *) | cfg a -> val where
|
||||
getConfigEntry :: proxy a -> Config cfg -> val
|
||||
class HasConfigEntry (cfg :: [*]) (tag :: k) (val :: *) | cfg tag -> val where
|
||||
getConfigEntry :: proxy tag -> Config cfg -> val
|
||||
|
||||
instance OVERLAPPABLE_
|
||||
HasConfigEntry xs tag val => HasConfigEntry (notIt ': xs) tag val where
|
||||
|
|
71
servant-server/test/Servant/Server/UsingConfigSpec.hs
Normal file
71
servant-server/test/Servant/Server/UsingConfigSpec.hs
Normal file
|
@ -0,0 +1,71 @@
|
|||
{-# LANGUAGE DataKinds #-}
|
||||
{-# LANGUAGE FlexibleContexts #-}
|
||||
{-# LANGUAGE FlexibleInstances #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE ScopedTypeVariables #-}
|
||||
{-# LANGUAGE TypeFamilies #-}
|
||||
{-# LANGUAGE TypeOperators #-}
|
||||
{-# LANGUAGE UndecidableInstances #-}
|
||||
|
||||
module Servant.Server.UsingConfigSpec where
|
||||
|
||||
import Network.Wai
|
||||
import Test.Hspec (Spec, describe, it)
|
||||
import Test.Hspec.Wai
|
||||
|
||||
import Servant
|
||||
import Servant.Server.Internal.Config
|
||||
import Servant.Server.Internal.Router
|
||||
import Servant.Server.Internal.RoutingApplication
|
||||
|
||||
-- * custom test combinator
|
||||
|
||||
data CustomCombinator
|
||||
|
||||
data CustomConfig = CustomConfig String
|
||||
|
||||
data Tag
|
||||
|
||||
instance forall subApi (c :: [*]) .
|
||||
(HasServer subApi) =>
|
||||
HasServer (CustomCombinator :> subApi) where
|
||||
|
||||
type ServerT (CustomCombinator :> subApi) m =
|
||||
CustomConfig -> ServerT subApi m
|
||||
type HasCfg (CustomCombinator :> subApi) c =
|
||||
(HasConfigEntry c Tag CustomConfig, HasCfg subApi c)
|
||||
|
||||
route Proxy config delayed =
|
||||
route subProxy config (fmap (inject config) delayed :: Delayed (Server subApi))
|
||||
where
|
||||
subProxy :: Proxy subApi
|
||||
subProxy = Proxy
|
||||
|
||||
inject config f = f (getConfigEntry (Proxy :: Proxy Tag) config)
|
||||
|
||||
-- * API
|
||||
|
||||
type API =
|
||||
CustomCombinator :> Get '[JSON] String
|
||||
|
||||
api :: Proxy API
|
||||
api = Proxy
|
||||
|
||||
testServer :: Server API
|
||||
testServer (CustomConfig s) = return s
|
||||
|
||||
app :: Application
|
||||
app =
|
||||
serve api config testServer
|
||||
where
|
||||
config :: Config '[ConfigEntry Tag CustomConfig]
|
||||
config = CustomConfig "configValue" .: EmptyConfig
|
||||
|
||||
-- * tests
|
||||
|
||||
spec :: Spec
|
||||
spec = do
|
||||
describe "using Config in a custom combinator" $ do
|
||||
with (return app) $ do
|
||||
it "allows to retrieve the ConfigEntry" $ do
|
||||
get "/" `shouldRespondWith` "\"configValue\""
|
Loading…
Reference in a new issue