2015-05-02 16:46:43 +02:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
2016-04-21 13:31:51 +02:00
|
|
|
module Servant.ArbitraryMonadServerSpec where
|
2015-05-02 16:46:43 +02:00
|
|
|
|
|
|
|
import Control.Monad.Reader
|
2017-10-01 18:20:09 +02:00
|
|
|
import Data.Functor.Identity
|
2015-05-02 16:46:43 +02:00
|
|
|
import Data.Proxy
|
|
|
|
import Servant.API
|
|
|
|
import Servant.Server
|
|
|
|
|
2018-06-29 21:08:26 +02:00
|
|
|
import Test.Hspec
|
|
|
|
(Spec, describe, it)
|
|
|
|
import Test.Hspec.Wai
|
|
|
|
(get, matchStatus, post, shouldRespondWith, with)
|
2015-05-02 16:46:43 +02:00
|
|
|
|
|
|
|
spec :: Spec
|
2016-04-28 16:27:50 +02:00
|
|
|
spec = describe "Arbitrary monad server" $ do
|
2015-05-02 16:46:43 +02:00
|
|
|
enterSpec
|
|
|
|
|
|
|
|
type ReaderAPI = "int" :> Get '[JSON] Int
|
|
|
|
:<|> "string" :> Post '[JSON] String
|
|
|
|
|
|
|
|
type IdentityAPI = "bool" :> Get '[JSON] Bool
|
|
|
|
|
|
|
|
type CombinedAPI = ReaderAPI :<|> IdentityAPI
|
|
|
|
|
|
|
|
readerAPI :: Proxy ReaderAPI
|
|
|
|
readerAPI = Proxy
|
|
|
|
|
2017-10-01 18:20:09 +02:00
|
|
|
identityAPI :: Proxy IdentityAPI
|
|
|
|
identityAPI = Proxy
|
|
|
|
|
2015-05-02 16:46:43 +02:00
|
|
|
combinedAPI :: Proxy CombinedAPI
|
|
|
|
combinedAPI = Proxy
|
|
|
|
|
|
|
|
readerServer' :: ServerT ReaderAPI (Reader String)
|
|
|
|
readerServer' = return 1797 :<|> ask
|
|
|
|
|
2017-10-01 18:20:09 +02:00
|
|
|
fReader :: Reader String a -> Handler a
|
|
|
|
fReader x = return (runReader x "hi")
|
2015-05-02 16:46:43 +02:00
|
|
|
|
|
|
|
readerServer :: Server ReaderAPI
|
2017-10-01 18:20:09 +02:00
|
|
|
readerServer = hoistServer readerAPI fReader readerServer'
|
2015-05-02 16:46:43 +02:00
|
|
|
|
|
|
|
combinedReaderServer' :: ServerT CombinedAPI (Reader String)
|
2017-10-01 18:20:09 +02:00
|
|
|
combinedReaderServer' = readerServer' :<|> hoistServer identityAPI (return . runIdentity) (return True)
|
2015-05-02 16:46:43 +02:00
|
|
|
|
|
|
|
combinedReaderServer :: Server CombinedAPI
|
2017-10-01 18:20:09 +02:00
|
|
|
combinedReaderServer = hoistServer combinedAPI fReader combinedReaderServer'
|
2015-05-02 16:46:43 +02:00
|
|
|
|
|
|
|
enterSpec :: Spec
|
|
|
|
enterSpec = describe "Enter" $ do
|
2016-02-18 16:36:24 +01:00
|
|
|
with (return (serve readerAPI readerServer)) $ do
|
2015-05-02 16:46:43 +02:00
|
|
|
|
|
|
|
it "allows running arbitrary monads" $ do
|
|
|
|
get "int" `shouldRespondWith` "1797"
|
2015-11-27 02:05:34 +01:00
|
|
|
post "string" "3" `shouldRespondWith` "\"hi\""{ matchStatus = 200 }
|
2015-05-02 16:46:43 +02:00
|
|
|
|
2016-02-18 16:36:24 +01:00
|
|
|
with (return (serve combinedAPI combinedReaderServer)) $ do
|
2015-05-02 16:46:43 +02:00
|
|
|
it "allows combnation of enters" $ do
|
|
|
|
get "bool" `shouldRespondWith` "true"
|