add test for using the Config machinery

This commit is contained in:
Sönke Hahn 2016-01-09 17:22:52 +01:00
parent eafc5d33bb
commit dcd2c8078c
2 changed files with 73 additions and 2 deletions

View file

@ -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

View 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\""