2016-01-09 17:22:52 +01:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
2016-01-10 16:40:56 +01:00
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
2016-01-09 17:22:52 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
|
|
|
|
|
|
|
module Servant.Server.UsingConfigSpec where
|
|
|
|
|
|
|
|
import Network.Wai
|
|
|
|
import Test.Hspec (Spec, describe, it)
|
|
|
|
import Test.Hspec.Wai
|
|
|
|
|
|
|
|
import Servant
|
2016-01-10 15:39:55 +01:00
|
|
|
import Servant.Server.UsingConfigSpec.CustomCombinator
|
2016-01-09 17:22:52 +01:00
|
|
|
|
|
|
|
-- * API
|
|
|
|
|
2016-01-10 15:59:33 +01:00
|
|
|
type OneEntryAPI =
|
2016-01-11 13:59:23 +01:00
|
|
|
CustomCombinator () :> Get '[JSON] String
|
2016-01-09 17:22:52 +01:00
|
|
|
|
2016-01-10 15:59:33 +01:00
|
|
|
testServer :: Server OneEntryAPI
|
2016-01-10 16:50:17 +01:00
|
|
|
testServer s = return s
|
2016-01-09 17:22:52 +01:00
|
|
|
|
2016-01-10 15:59:33 +01:00
|
|
|
oneEntryApp :: Application
|
|
|
|
oneEntryApp =
|
|
|
|
serve (Proxy :: Proxy OneEntryAPI) config testServer
|
2016-01-09 17:22:52 +01:00
|
|
|
where
|
2016-01-11 13:59:23 +01:00
|
|
|
config = ("configValue" :: String) :. EmptyConfig
|
2016-01-09 17:22:52 +01:00
|
|
|
|
2016-01-10 15:59:33 +01:00
|
|
|
type OneEntryTwiceAPI =
|
2016-01-11 13:59:23 +01:00
|
|
|
"foo" :> CustomCombinator () :> Get '[JSON] String :<|>
|
|
|
|
"bar" :> CustomCombinator () :> Get '[JSON] String
|
2016-01-10 15:59:33 +01:00
|
|
|
|
|
|
|
oneEntryTwiceApp :: Application
|
|
|
|
oneEntryTwiceApp = serve (Proxy :: Proxy OneEntryTwiceAPI) config $
|
|
|
|
testServer :<|>
|
|
|
|
testServer
|
|
|
|
where
|
2016-01-11 13:59:23 +01:00
|
|
|
config = ("configValueTwice" :: String) :. EmptyConfig
|
2016-01-10 15:59:33 +01:00
|
|
|
|
|
|
|
type TwoDifferentEntries =
|
2016-01-11 13:59:23 +01:00
|
|
|
"foo" :> CustomCombinator "foo" :> Get '[JSON] String :<|>
|
|
|
|
"bar" :> CustomCombinator "bar" :> Get '[JSON] String
|
2016-01-10 15:59:33 +01:00
|
|
|
|
|
|
|
twoDifferentEntries :: Application
|
|
|
|
twoDifferentEntries = serve (Proxy :: Proxy TwoDifferentEntries) config $
|
|
|
|
testServer :<|>
|
|
|
|
testServer
|
|
|
|
where
|
|
|
|
config =
|
2016-01-11 13:59:23 +01:00
|
|
|
(Tag "firstConfigValue" :: Tagged "foo" String) :.
|
|
|
|
(Tag "secondConfigValue" :: Tagged "bar" String) :.
|
2016-01-10 15:59:33 +01:00
|
|
|
EmptyConfig
|
|
|
|
|
2016-01-09 17:22:52 +01:00
|
|
|
-- * tests
|
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = do
|
|
|
|
describe "using Config in a custom combinator" $ do
|
2016-01-10 15:59:33 +01:00
|
|
|
with (return oneEntryApp) $ do
|
|
|
|
it "allows to retrieve a ConfigEntry" $ do
|
2016-01-09 17:22:52 +01:00
|
|
|
get "/" `shouldRespondWith` "\"configValue\""
|
2016-01-10 15:59:33 +01:00
|
|
|
|
|
|
|
with (return oneEntryTwiceApp) $ do
|
|
|
|
it "allows to retrieve the same ConfigEntry twice" $ do
|
|
|
|
get "/foo" `shouldRespondWith` "\"configValueTwice\""
|
|
|
|
get "/bar" `shouldRespondWith` "\"configValueTwice\""
|
|
|
|
|
|
|
|
with (return twoDifferentEntries) $ do
|
|
|
|
it "allows to retrieve different ConfigEntries for the same combinator" $ do
|
|
|
|
get "/foo" `shouldRespondWith` "\"firstConfigValue\""
|
|
|
|
get "/bar" `shouldRespondWith` "\"secondConfigValue\""
|