2015-05-02 16:46:43 +02:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
|
|
|
module Servant.Server.Internal.EnterSpec where
|
|
|
|
|
2015-08-17 23:56:29 +02:00
|
|
|
import qualified Control.Category as C
|
2015-05-02 16:46:43 +02:00
|
|
|
import Control.Monad.Reader
|
2015-09-12 14:11:24 +02:00
|
|
|
import Control.Monad.Trans.Except
|
2015-05-02 16:46:43 +02:00
|
|
|
import Data.Proxy
|
|
|
|
import Servant.API
|
|
|
|
import Servant.Server
|
|
|
|
|
2015-07-31 18:07:38 +02:00
|
|
|
import Servant.Utils.StaticFiles (serveDirectory)
|
|
|
|
import Network.Wai (Application)
|
|
|
|
import Control.Exception (bracket)
|
|
|
|
import System.Directory (getCurrentDirectory, setCurrentDirectory,
|
|
|
|
createDirectory)
|
|
|
|
import System.IO.Temp (withSystemTempDirectory)
|
|
|
|
import Test.Hspec (Spec, describe, it, around_)
|
2015-08-17 23:56:29 +02:00
|
|
|
import Test.Hspec.Wai (get, matchStatus, post,
|
|
|
|
shouldRespondWith, with)
|
2015-05-02 16:46:43 +02:00
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = describe "module Servant.Server.Enter" $ do
|
|
|
|
enterSpec
|
|
|
|
|
2015-09-30 22:51:56 +02:00
|
|
|
type ReaderAPI' = "ep1" :> Get '[JSON] String :<|> "ep2" :> Get '[JSON] String
|
|
|
|
readerServera' :: Reader String String :<|> Reader String String
|
|
|
|
readerServera' = ask :<|> ask
|
|
|
|
|
2015-10-01 16:05:49 +02:00
|
|
|
x :: Reader String :~> ExceptT ServantErr IO
|
2015-09-30 22:51:56 +02:00
|
|
|
x = (generalizeNat C.. (runReaderTNat "hi"))
|
|
|
|
mainServer' :: Server ReaderAPI'
|
|
|
|
mainServer' = enter x readerServera'
|
|
|
|
|
2015-05-02 16:46:43 +02:00
|
|
|
type ReaderAPI = "int" :> Get '[JSON] Int
|
|
|
|
:<|> "string" :> Post '[JSON] String
|
2015-08-10 23:16:42 +02:00
|
|
|
:<|> "static" :> Raw (Reader String) Application
|
2015-05-02 16:46:43 +02:00
|
|
|
|
|
|
|
type IdentityAPI = "bool" :> Get '[JSON] Bool
|
|
|
|
|
|
|
|
type CombinedAPI = ReaderAPI :<|> IdentityAPI
|
|
|
|
|
|
|
|
readerAPI :: Proxy ReaderAPI
|
|
|
|
readerAPI = Proxy
|
|
|
|
|
|
|
|
combinedAPI :: Proxy CombinedAPI
|
|
|
|
combinedAPI = Proxy
|
|
|
|
|
|
|
|
readerServer' :: ServerT ReaderAPI (Reader String)
|
2015-07-31 18:07:38 +02:00
|
|
|
readerServer' = return 1797
|
|
|
|
:<|> ask
|
|
|
|
:<|> serveDirectory "static"
|
2015-05-02 16:46:43 +02:00
|
|
|
|
2015-09-12 14:11:24 +02:00
|
|
|
fReader :: Reader String :~> ExceptT ServantErr IO
|
2015-05-02 16:46:43 +02:00
|
|
|
fReader = generalizeNat C.. (runReaderTNat "hi")
|
|
|
|
|
|
|
|
readerServer :: Server ReaderAPI
|
|
|
|
readerServer = enter fReader readerServer'
|
|
|
|
|
|
|
|
combinedReaderServer' :: ServerT CombinedAPI (Reader String)
|
|
|
|
combinedReaderServer' = readerServer' :<|> enter generalizeNat (return True)
|
|
|
|
|
|
|
|
combinedReaderServer :: Server CombinedAPI
|
|
|
|
combinedReaderServer = enter fReader combinedReaderServer'
|
|
|
|
|
2015-07-31 18:07:38 +02:00
|
|
|
withStaticFiles :: IO () -> IO ()
|
|
|
|
withStaticFiles action = withSystemTempDirectory "servant-test" $ \ tmpDir ->
|
|
|
|
bracket (setup tmpDir) teardown (const action)
|
|
|
|
where
|
|
|
|
setup tmpDir = do
|
|
|
|
outer <- getCurrentDirectory
|
|
|
|
setCurrentDirectory tmpDir
|
|
|
|
createDirectory "static"
|
|
|
|
writeFile "static/foo.txt" "bar"
|
|
|
|
writeFile "static/index.html" "index"
|
|
|
|
return outer
|
|
|
|
|
|
|
|
teardown outer = do
|
|
|
|
setCurrentDirectory outer
|
|
|
|
|
2015-05-02 16:46:43 +02:00
|
|
|
enterSpec :: Spec
|
|
|
|
enterSpec = describe "Enter" $ do
|
2015-07-31 18:07:38 +02:00
|
|
|
around_ withStaticFiles $ with (return (serve readerAPI readerServer)) $ do
|
2015-05-02 16:46:43 +02:00
|
|
|
|
|
|
|
it "allows running arbitrary monads" $ do
|
|
|
|
get "int" `shouldRespondWith` "1797"
|
|
|
|
post "string" "3" `shouldRespondWith` "\"hi\""{ matchStatus = 201 }
|
|
|
|
|
|
|
|
with (return (serve combinedAPI combinedReaderServer)) $ do
|
|
|
|
it "allows combnation of enters" $ do
|
|
|
|
get "bool" `shouldRespondWith` "true"
|