2015-05-01 08:09:23 +02:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
2015-05-01 08:06:24 +02:00
|
|
|
{-# LANGUAGE OverlappingInstances #-}
|
2015-05-01 08:09:23 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE PolyKinds #-}
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
2015-05-01 08:06:24 +02:00
|
|
|
|
2016-05-13 14:56:09 +02:00
|
|
|
module Servant.EkgSpec (spec) where
|
|
|
|
|
2015-05-01 08:09:23 +02:00
|
|
|
import Control.Concurrent
|
2016-05-13 14:56:09 +02:00
|
|
|
import Control.Monad.Trans.Except
|
2015-05-01 08:09:23 +02:00
|
|
|
import Data.Aeson
|
|
|
|
import Data.Monoid
|
|
|
|
import Data.Proxy
|
2016-05-13 14:56:09 +02:00
|
|
|
import qualified Data.HashMap.Strict as H
|
2015-05-01 08:09:23 +02:00
|
|
|
import Data.Text
|
|
|
|
import GHC.Generics
|
2016-05-13 14:56:09 +02:00
|
|
|
import Network.HTTP.Client (defaultManagerSettings, newManager)
|
2015-05-01 08:09:23 +02:00
|
|
|
import Network.Wai
|
|
|
|
import Network.Wai.Handler.Warp
|
|
|
|
import Servant
|
2016-05-13 14:56:09 +02:00
|
|
|
import Servant.Client
|
2015-05-01 08:09:23 +02:00
|
|
|
import System.Metrics
|
2016-05-13 14:56:09 +02:00
|
|
|
import qualified System.Metrics.Counter as Counter
|
|
|
|
import Test.Hspec
|
2015-05-01 08:06:24 +02:00
|
|
|
|
2015-05-01 08:09:23 +02:00
|
|
|
import Servant.Ekg
|
2015-05-01 08:06:24 +02:00
|
|
|
|
2016-05-13 14:56:09 +02:00
|
|
|
|
|
|
|
-- * Spec
|
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = describe "servant-ekg" $ do
|
|
|
|
|
|
|
|
let getEp :<|> postEp :<|> deleteEp = client testApi
|
|
|
|
|
|
|
|
it "collects GET data" $ do
|
|
|
|
withApp $ \port mvar -> do
|
|
|
|
mgr <- newManager defaultManagerSettings
|
|
|
|
result <- runExceptT $ getEp "name" Nothing mgr (BaseUrl Http "localhost" port "")
|
|
|
|
m <- readMVar mvar
|
|
|
|
print $ H.keys m
|
|
|
|
case H.lookup "hello.:name.GET" m of
|
|
|
|
Nothing -> fail "Expected some value"
|
|
|
|
Just v -> Counter.read (metersC2XX v) `shouldReturn` 1
|
|
|
|
|
|
|
|
|
2015-05-01 08:06:24 +02:00
|
|
|
-- * Example
|
|
|
|
|
|
|
|
-- | A greet message data type
|
|
|
|
newtype Greet = Greet { _msg :: Text }
|
|
|
|
deriving (Generic, Show)
|
|
|
|
|
|
|
|
instance FromJSON Greet
|
|
|
|
instance ToJSON Greet
|
|
|
|
|
|
|
|
-- API specification
|
|
|
|
type TestApi =
|
|
|
|
-- GET /hello/:name?capital={true, false} returns a Greet as JSON
|
2016-05-13 12:27:26 +02:00
|
|
|
"hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON] Greet
|
2015-05-01 08:06:24 +02:00
|
|
|
|
|
|
|
-- POST /greet with a Greet as JSON in the request body,
|
|
|
|
-- returns a Greet as JSON
|
2016-05-13 12:27:26 +02:00
|
|
|
:<|> "greet" :> ReqBody '[JSON] Greet :> Post '[JSON] Greet
|
2015-05-01 08:06:24 +02:00
|
|
|
|
|
|
|
-- DELETE /greet/:greetid
|
2016-05-13 12:27:26 +02:00
|
|
|
:<|> "greet" :> Capture "greetid" Text :> Delete '[JSON] ()
|
2015-05-01 08:06:24 +02:00
|
|
|
|
|
|
|
testApi :: Proxy TestApi
|
|
|
|
testApi = Proxy
|
|
|
|
|
|
|
|
-- Server-side handlers.
|
|
|
|
--
|
|
|
|
-- There's one handler per endpoint, which, just like in the type
|
|
|
|
-- that represents the API, are glued together using :<|>.
|
|
|
|
--
|
|
|
|
-- Each handler runs in the 'EitherT (Int, String) IO' monad.
|
|
|
|
server :: Server TestApi
|
|
|
|
server = helloH :<|> postGreetH :<|> deleteGreetH
|
|
|
|
|
|
|
|
where helloH name Nothing = helloH name (Just False)
|
|
|
|
helloH name (Just False) = return . Greet $ "Hello, " <> name
|
|
|
|
helloH name (Just True) = return . Greet . toUpper $ "Hello, " <> name
|
|
|
|
|
2015-05-01 08:09:23 +02:00
|
|
|
postGreetH = return
|
2015-05-01 08:06:24 +02:00
|
|
|
|
|
|
|
deleteGreetH _ = return ()
|
|
|
|
|
|
|
|
-- Turn the server into a WAI app. 'serve' is provided by servant,
|
|
|
|
-- more precisely by the Servant.Server module.
|
|
|
|
test :: Application
|
|
|
|
test = serve testApi server
|
|
|
|
|
2016-05-13 14:56:09 +02:00
|
|
|
withApp :: (Port -> MVar (H.HashMap Text Meters) -> IO a) -> IO a
|
|
|
|
withApp a = do
|
|
|
|
ekg <- newStore
|
|
|
|
ms <- newMVar mempty
|
|
|
|
withApplication (return $ monitorEndpoints testApi ekg ms test) $ \p -> a p ms
|