2015-04-22 14:57:30 +02:00
|
|
|
{-# LANGUAGE CPP #-}
|
2015-04-19 18:31:23 +02:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE GADTs #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
2015-03-08 23:59:40 +01:00
|
|
|
{-# LANGUAGE StandaloneDeriving #-}
|
2015-04-19 18:31:23 +02:00
|
|
|
{-# LANGUAGE TypeOperators #-}
|
2015-05-02 03:21:03 +02:00
|
|
|
{-# OPTIONS_GHC -fcontext-stack=100 #-}
|
2015-03-08 23:59:40 +01:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2015-05-05 17:39:35 +02:00
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
module Servant.ClientSpec where
|
|
|
|
|
2015-04-22 14:57:30 +02:00
|
|
|
#if !MIN_VERSION_base(4,8,0)
|
2015-09-12 14:11:24 +02:00
|
|
|
import Control.Applicative ((<$>))
|
2015-04-22 14:57:30 +02:00
|
|
|
#endif
|
2015-09-12 14:11:24 +02:00
|
|
|
import Control.Arrow (left)
|
2015-04-19 18:31:23 +02:00
|
|
|
import Control.Concurrent
|
|
|
|
import Control.Exception
|
2015-09-12 14:11:24 +02:00
|
|
|
import Control.Monad.Trans.Except
|
2015-04-19 18:31:23 +02:00
|
|
|
import Data.Aeson
|
|
|
|
import Data.ByteString.Lazy (ByteString)
|
|
|
|
import Data.Char
|
|
|
|
import Data.Foldable (forM_)
|
|
|
|
import Data.Monoid
|
|
|
|
import Data.Proxy
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import GHC.Generics
|
|
|
|
import qualified Network.HTTP.Client as C
|
|
|
|
import Network.HTTP.Media
|
2015-05-02 03:21:03 +02:00
|
|
|
import Network.HTTP.Types hiding (Header)
|
|
|
|
import qualified Network.HTTP.Types as HTTP
|
2015-04-19 18:31:23 +02:00
|
|
|
import Network.Socket
|
|
|
|
import Network.Wai hiding (Response)
|
|
|
|
import Network.Wai.Handler.Warp
|
|
|
|
import Test.Hspec
|
|
|
|
import Test.Hspec.QuickCheck
|
|
|
|
import Test.HUnit
|
|
|
|
import Test.QuickCheck
|
|
|
|
|
|
|
|
import Servant.API
|
|
|
|
import Servant.Client
|
|
|
|
import Servant.Server
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
-- * test data types
|
|
|
|
|
|
|
|
data Person = Person {
|
|
|
|
name :: String,
|
|
|
|
age :: Integer
|
|
|
|
}
|
|
|
|
deriving (Eq, Show, Generic)
|
|
|
|
|
|
|
|
instance ToJSON Person
|
|
|
|
instance FromJSON Person
|
|
|
|
|
2015-02-24 23:56:06 +01:00
|
|
|
instance ToFormUrlEncoded Person where
|
|
|
|
toFormUrlEncoded Person{..} =
|
|
|
|
[("name", T.pack name), ("age", T.pack (show age))]
|
|
|
|
|
|
|
|
lookupEither :: (Show a, Eq a) => a -> [(a,b)] -> Either String b
|
|
|
|
lookupEither x xs = do
|
|
|
|
maybe (Left $ "could not find key " <> show x) return $ lookup x xs
|
|
|
|
|
|
|
|
instance FromFormUrlEncoded Person where
|
|
|
|
fromFormUrlEncoded xs = do
|
|
|
|
n <- lookupEither "name" xs
|
|
|
|
a <- lookupEither "age" xs
|
|
|
|
return $ Person (T.unpack n) (read $ T.unpack a)
|
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
alice :: Person
|
|
|
|
alice = Person "Alice" 42
|
|
|
|
|
2015-05-02 03:21:03 +02:00
|
|
|
type TestHeaders = '[Header "X-Example1" Int, Header "X-Example2" String]
|
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
type Api =
|
2015-02-17 07:17:10 +01:00
|
|
|
"get" :> Get '[JSON] Person
|
2015-05-17 15:51:49 +02:00
|
|
|
:<|> "deleteEmpty" :> Delete '[] ()
|
2015-02-24 23:56:06 +01:00
|
|
|
:<|> "capture" :> Capture "name" String :> Get '[JSON,FormUrlEncoded] Person
|
|
|
|
:<|> "body" :> ReqBody '[FormUrlEncoded,JSON] Person :> Post '[JSON] Person
|
|
|
|
:<|> "param" :> QueryParam "name" String :> Get '[FormUrlEncoded,JSON] Person
|
2015-02-17 07:17:10 +01:00
|
|
|
:<|> "params" :> QueryParams "names" String :> Get '[JSON] [Person]
|
|
|
|
:<|> "flag" :> QueryFlag "flag" :> Get '[JSON] Bool
|
|
|
|
:<|> "matrixparam" :> MatrixParam "name" String :> Get '[JSON] Person
|
|
|
|
:<|> "matrixparams" :> MatrixParams "name" String :> Get '[JSON] [Person]
|
|
|
|
:<|> "matrixflag" :> MatrixFlag "flag" :> Get '[JSON] Bool
|
2014-11-27 18:28:01 +01:00
|
|
|
:<|> "rawSuccess" :> Raw
|
|
|
|
:<|> "rawFailure" :> Raw
|
|
|
|
:<|> "multiple" :>
|
|
|
|
Capture "first" String :>
|
|
|
|
QueryParam "second" Int :>
|
|
|
|
QueryFlag "third" :>
|
2015-02-17 07:17:10 +01:00
|
|
|
ReqBody '[JSON] [(String, [Rational])] :>
|
|
|
|
Get '[JSON] (String, Maybe Int, Bool, [(String, [Rational])])
|
2015-05-02 03:21:03 +02:00
|
|
|
:<|> "headers" :> Get '[JSON] (Headers TestHeaders Bool)
|
2015-05-17 15:51:49 +02:00
|
|
|
:<|> "deleteContentType" :> Delete '[JSON] ()
|
2014-11-27 18:28:01 +01:00
|
|
|
api :: Proxy Api
|
|
|
|
api = Proxy
|
|
|
|
|
|
|
|
server :: Application
|
|
|
|
server = serve api (
|
|
|
|
return alice
|
2015-02-01 23:33:05 +01:00
|
|
|
:<|> return ()
|
2014-11-27 18:28:01 +01:00
|
|
|
:<|> (\ name -> return $ Person name 0)
|
|
|
|
:<|> return
|
|
|
|
:<|> (\ name -> case name of
|
|
|
|
Just "alice" -> return alice
|
2015-09-12 14:11:24 +02:00
|
|
|
Just name -> throwE $ ServantErr 400 (name ++ " not found") "" []
|
|
|
|
Nothing -> throwE $ ServantErr 400 "missing parameter" "" [])
|
2014-11-27 18:28:01 +01:00
|
|
|
:<|> (\ names -> return (zipWith Person names [0..]))
|
|
|
|
:<|> return
|
2015-01-01 23:43:29 +01:00
|
|
|
:<|> (\ name -> case name of
|
|
|
|
Just "alice" -> return alice
|
2015-09-12 14:11:24 +02:00
|
|
|
Just name -> throwE $ ServantErr 400 (name ++ " not found") "" []
|
|
|
|
Nothing -> throwE $ ServantErr 400 "missing parameter" "" [])
|
2015-01-01 23:43:29 +01:00
|
|
|
:<|> (\ names -> return (zipWith Person names [0..]))
|
|
|
|
:<|> return
|
2014-11-27 18:28:01 +01:00
|
|
|
:<|> (\ _request respond -> respond $ responseLBS ok200 [] "rawSuccess")
|
|
|
|
:<|> (\ _request respond -> respond $ responseLBS badRequest400 [] "rawFailure")
|
2015-05-02 03:21:03 +02:00
|
|
|
:<|> (\ a b c d -> return (a, b, c, d))
|
|
|
|
:<|> (return $ addHeader 1729 $ addHeader "eg2" True)
|
2015-05-17 15:51:49 +02:00
|
|
|
:<|> return ()
|
2014-11-27 18:28:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
withServer :: (BaseUrl -> IO a) -> IO a
|
|
|
|
withServer action = withWaiDaemon (return server) action
|
|
|
|
|
2015-03-09 04:38:32 +01:00
|
|
|
type FailApi =
|
|
|
|
"get" :> Raw
|
|
|
|
:<|> "capture" :> Capture "name" String :> Raw
|
|
|
|
:<|> "body" :> Raw
|
|
|
|
failApi :: Proxy FailApi
|
|
|
|
failApi = Proxy
|
|
|
|
|
|
|
|
failServer :: Application
|
|
|
|
failServer = serve failApi (
|
|
|
|
(\ _request respond -> respond $ responseLBS ok200 [] "")
|
|
|
|
:<|> (\ _capture _request respond -> respond $ responseLBS ok200 [("content-type", "application/json")] "")
|
|
|
|
:<|> (\_request respond -> respond $ responseLBS ok200 [("content-type", "fooooo")] "")
|
|
|
|
)
|
|
|
|
|
|
|
|
withFailServer :: (BaseUrl -> IO a) -> IO a
|
|
|
|
withFailServer action = withWaiDaemon (return failServer) action
|
|
|
|
|
2015-05-05 17:39:35 +02:00
|
|
|
spec :: IO ()
|
|
|
|
spec = withServer $ \ baseUrl -> do
|
2015-09-28 16:34:53 +02:00
|
|
|
manager <- C.newManager C.defaultManagerSettings
|
2015-09-12 14:11:24 +02:00
|
|
|
let getGet :: ExceptT ServantError IO Person
|
|
|
|
getDeleteEmpty :: ExceptT ServantError IO ()
|
|
|
|
getCapture :: String -> ExceptT ServantError IO Person
|
|
|
|
getBody :: Person -> ExceptT ServantError IO Person
|
|
|
|
getQueryParam :: Maybe String -> ExceptT ServantError IO Person
|
|
|
|
getQueryParams :: [String] -> ExceptT ServantError IO [Person]
|
|
|
|
getQueryFlag :: Bool -> ExceptT ServantError IO Bool
|
|
|
|
getMatrixParam :: Maybe String -> ExceptT ServantError IO Person
|
|
|
|
getMatrixParams :: [String] -> ExceptT ServantError IO [Person]
|
|
|
|
getMatrixFlag :: Bool -> ExceptT ServantError IO Bool
|
|
|
|
getRawSuccess :: Method -> ExceptT ServantError IO (Int, ByteString, MediaType, [HTTP.Header], C.Response ByteString)
|
|
|
|
getRawFailure :: Method -> ExceptT ServantError IO (Int, ByteString, MediaType, [HTTP.Header], C.Response ByteString)
|
|
|
|
getMultiple :: String -> Maybe Int -> Bool -> [(String, [Rational])] -> ExceptT ServantError IO (String, Maybe Int, Bool, [(String, [Rational])])
|
|
|
|
getRespHeaders :: ExceptT ServantError IO (Headers TestHeaders Bool)
|
|
|
|
getDeleteContentType :: ExceptT ServantError IO ()
|
2015-05-05 17:39:35 +02:00
|
|
|
( getGet
|
2015-05-17 15:51:49 +02:00
|
|
|
:<|> getDeleteEmpty
|
2015-05-05 17:39:35 +02:00
|
|
|
:<|> getCapture
|
|
|
|
:<|> getBody
|
|
|
|
:<|> getQueryParam
|
|
|
|
:<|> getQueryParams
|
|
|
|
:<|> getQueryFlag
|
|
|
|
:<|> getMatrixParam
|
|
|
|
:<|> getMatrixParams
|
|
|
|
:<|> getMatrixFlag
|
|
|
|
:<|> getRawSuccess
|
|
|
|
:<|> getRawFailure
|
|
|
|
:<|> getMultiple
|
2015-05-17 15:51:49 +02:00
|
|
|
:<|> getRespHeaders
|
|
|
|
:<|> getDeleteContentType)
|
2015-09-28 16:34:53 +02:00
|
|
|
= client api baseUrl manager
|
2015-05-05 17:39:35 +02:00
|
|
|
|
|
|
|
hspec $ do
|
|
|
|
it "Servant.API.Get" $ do
|
2015-09-12 14:11:24 +02:00
|
|
|
(left show <$> runExceptT getGet) `shouldReturn` Right alice
|
2015-05-05 17:39:35 +02:00
|
|
|
|
2015-05-17 15:51:49 +02:00
|
|
|
describe "Servant.API.Delete" $ do
|
|
|
|
it "allows empty content type" $ do
|
2015-09-12 14:11:24 +02:00
|
|
|
(left show <$> runExceptT getDeleteEmpty) `shouldReturn` Right ()
|
2015-05-17 15:51:49 +02:00
|
|
|
|
|
|
|
it "allows content type" $ do
|
2015-09-12 14:11:24 +02:00
|
|
|
(left show <$> runExceptT getDeleteContentType) `shouldReturn` Right ()
|
2015-05-05 17:39:35 +02:00
|
|
|
|
|
|
|
it "Servant.API.Capture" $ do
|
2015-09-12 14:11:24 +02:00
|
|
|
(left show <$> runExceptT (getCapture "Paula")) `shouldReturn` Right (Person "Paula" 0)
|
2015-05-05 17:39:35 +02:00
|
|
|
|
|
|
|
it "Servant.API.ReqBody" $ do
|
|
|
|
let p = Person "Clara" 42
|
2015-09-12 14:11:24 +02:00
|
|
|
(left show <$> runExceptT (getBody p)) `shouldReturn` Right p
|
2015-05-05 17:39:35 +02:00
|
|
|
|
|
|
|
it "Servant.API.QueryParam" $ do
|
2015-09-12 14:11:24 +02:00
|
|
|
left show <$> runExceptT (getQueryParam (Just "alice")) `shouldReturn` Right alice
|
|
|
|
Left FailureResponse{..} <- runExceptT (getQueryParam (Just "bob"))
|
2015-05-05 17:39:35 +02:00
|
|
|
responseStatus `shouldBe` Status 400 "bob not found"
|
|
|
|
|
|
|
|
it "Servant.API.QueryParam.QueryParams" $ do
|
2015-09-12 14:11:24 +02:00
|
|
|
(left show <$> runExceptT (getQueryParams [])) `shouldReturn` Right []
|
|
|
|
(left show <$> runExceptT (getQueryParams ["alice", "bob"]))
|
2015-05-05 17:39:35 +02:00
|
|
|
`shouldReturn` Right [Person "alice" 0, Person "bob" 1]
|
|
|
|
|
|
|
|
context "Servant.API.QueryParam.QueryFlag" $
|
|
|
|
forM_ [False, True] $ \ flag ->
|
|
|
|
it (show flag) $ do
|
2015-09-12 14:11:24 +02:00
|
|
|
(left show <$> runExceptT (getQueryFlag flag)) `shouldReturn` Right flag
|
2015-05-05 17:39:35 +02:00
|
|
|
|
|
|
|
it "Servant.API.MatrixParam" $ do
|
2015-09-12 14:11:24 +02:00
|
|
|
left show <$> runExceptT (getMatrixParam (Just "alice")) `shouldReturn` Right alice
|
|
|
|
Left FailureResponse{..} <- runExceptT (getMatrixParam (Just "bob"))
|
2015-05-05 17:39:35 +02:00
|
|
|
responseStatus `shouldBe` Status 400 "bob not found"
|
|
|
|
|
|
|
|
it "Servant.API.MatrixParam.MatrixParams" $ do
|
2015-09-12 14:11:24 +02:00
|
|
|
left show <$> runExceptT (getMatrixParams []) `shouldReturn` Right []
|
|
|
|
left show <$> runExceptT (getMatrixParams ["alice", "bob"])
|
2015-05-05 17:39:35 +02:00
|
|
|
`shouldReturn` Right [Person "alice" 0, Person "bob" 1]
|
|
|
|
|
|
|
|
context "Servant.API.MatrixParam.MatrixFlag" $
|
|
|
|
forM_ [False, True] $ \ flag ->
|
|
|
|
it (show flag) $ do
|
2015-09-12 14:11:24 +02:00
|
|
|
left show <$> runExceptT (getMatrixFlag flag) `shouldReturn` Right flag
|
2015-05-05 17:39:35 +02:00
|
|
|
|
|
|
|
it "Servant.API.Raw on success" $ do
|
2015-09-12 14:11:24 +02:00
|
|
|
res <- runExceptT (getRawSuccess methodGet)
|
2015-03-08 23:59:40 +01:00
|
|
|
case res of
|
2015-05-05 17:39:35 +02:00
|
|
|
Left e -> assertFailure $ show e
|
|
|
|
Right (code, body, ct, _, response) -> do
|
|
|
|
(code, body, ct) `shouldBe` (200, "rawSuccess", "application"//"octet-stream")
|
|
|
|
C.responseBody response `shouldBe` body
|
|
|
|
C.responseStatus response `shouldBe` ok200
|
|
|
|
|
|
|
|
it "Servant.API.Raw on failure" $ do
|
2015-09-12 14:11:24 +02:00
|
|
|
res <- runExceptT (getRawFailure methodGet)
|
2015-03-09 04:38:32 +01:00
|
|
|
case res of
|
2015-05-05 17:39:35 +02:00
|
|
|
Left e -> assertFailure $ show e
|
|
|
|
Right (code, body, ct, _, response) -> do
|
|
|
|
(code, body, ct) `shouldBe` (400, "rawFailure", "application"//"octet-stream")
|
|
|
|
C.responseBody response `shouldBe` body
|
|
|
|
C.responseStatus response `shouldBe` badRequest400
|
|
|
|
|
|
|
|
it "Returns headers appropriately" $ withServer $ \ _ -> do
|
2015-09-12 14:11:24 +02:00
|
|
|
res <- runExceptT getRespHeaders
|
2015-03-08 23:59:40 +01:00
|
|
|
case res of
|
2015-05-05 17:39:35 +02:00
|
|
|
Left e -> assertFailure $ show e
|
|
|
|
Right val -> getHeaders val `shouldBe` [("X-Example1", "1729"), ("X-Example2", "eg2")]
|
|
|
|
|
|
|
|
modifyMaxSuccess (const 20) $ do
|
|
|
|
it "works for a combination of Capture, QueryParam, QueryFlag and ReqBody" $
|
|
|
|
property $ forAllShrink pathGen shrink $ \(NonEmpty cap) num flag body ->
|
|
|
|
ioProperty $ do
|
2015-09-12 14:11:24 +02:00
|
|
|
result <- left show <$> runExceptT (getMultiple cap num flag body)
|
2015-05-05 17:39:35 +02:00
|
|
|
return $
|
|
|
|
result === Right (cap, num, flag, body)
|
2015-03-09 04:38:32 +01:00
|
|
|
|
|
|
|
|
2015-05-05 17:39:35 +02:00
|
|
|
context "client correctly handles error status codes" $ do
|
|
|
|
let test :: (WrappedApi, String) -> Spec
|
|
|
|
test (WrappedApi api, desc) =
|
|
|
|
it desc $
|
2015-09-12 14:11:24 +02:00
|
|
|
withWaiDaemon (return (serve api (throwE $ ServantErr 500 "error message" "" []))) $
|
2015-05-05 17:39:35 +02:00
|
|
|
\ host -> do
|
2015-09-12 14:11:24 +02:00
|
|
|
let getResponse :: ExceptT ServantError IO ()
|
2015-09-28 16:34:53 +02:00
|
|
|
getResponse = client api host manager
|
2015-09-12 14:11:24 +02:00
|
|
|
Left FailureResponse{..} <- runExceptT getResponse
|
2015-05-05 17:39:35 +02:00
|
|
|
responseStatus `shouldBe` (Status 500 "error message")
|
|
|
|
mapM_ test $
|
2015-05-08 20:47:19 +02:00
|
|
|
(WrappedApi (Proxy :: Proxy (Delete '[JSON] ())), "Delete") :
|
2015-05-05 17:39:35 +02:00
|
|
|
(WrappedApi (Proxy :: Proxy (Get '[JSON] ())), "Get") :
|
|
|
|
(WrappedApi (Proxy :: Proxy (Post '[JSON] ())), "Post") :
|
|
|
|
(WrappedApi (Proxy :: Proxy (Put '[JSON] ())), "Put") :
|
|
|
|
[]
|
|
|
|
|
|
|
|
failSpec :: IO ()
|
|
|
|
failSpec = withFailServer $ \ baseUrl -> do
|
2015-09-28 16:34:53 +02:00
|
|
|
manager <- C.newManager C.defaultManagerSettings
|
2015-09-12 14:11:24 +02:00
|
|
|
let getGet :: ExceptT ServantError IO Person
|
|
|
|
getDeleteEmpty :: ExceptT ServantError IO ()
|
|
|
|
getCapture :: String -> ExceptT ServantError IO Person
|
|
|
|
getBody :: Person -> ExceptT ServantError IO Person
|
2015-05-05 17:39:35 +02:00
|
|
|
( getGet
|
2015-05-17 15:51:49 +02:00
|
|
|
:<|> getDeleteEmpty
|
2015-05-05 17:39:35 +02:00
|
|
|
:<|> getCapture
|
2015-05-08 15:07:11 +02:00
|
|
|
:<|> getBody
|
|
|
|
:<|> _ )
|
2015-09-28 16:34:53 +02:00
|
|
|
= client api baseUrl manager
|
2015-09-12 14:11:24 +02:00
|
|
|
getGetWrongHost :: ExceptT ServantError IO Person
|
2015-09-28 16:34:53 +02:00
|
|
|
(getGetWrongHost :<|> _) = client api (BaseUrl Http "127.0.0.1" 19872 "") manager
|
2015-05-05 17:39:35 +02:00
|
|
|
|
|
|
|
hspec $ do
|
|
|
|
context "client returns errors appropriately" $ do
|
|
|
|
it "reports FailureResponse" $ do
|
2015-09-12 14:11:24 +02:00
|
|
|
Left res <- runExceptT getDeleteEmpty
|
2015-05-05 17:39:35 +02:00
|
|
|
case res of
|
|
|
|
FailureResponse (Status 404 "Not Found") _ _ -> return ()
|
|
|
|
_ -> fail $ "expected 404 response, but got " <> show res
|
|
|
|
|
|
|
|
it "reports DecodeFailure" $ do
|
2015-09-12 14:11:24 +02:00
|
|
|
Left res <- runExceptT (getCapture "foo")
|
2015-05-05 17:39:35 +02:00
|
|
|
case res of
|
|
|
|
DecodeFailure _ ("application/json") _ -> return ()
|
|
|
|
_ -> fail $ "expected DecodeFailure, but got " <> show res
|
|
|
|
|
|
|
|
it "reports ConnectionError" $ do
|
2015-09-12 14:11:24 +02:00
|
|
|
Left res <- runExceptT getGetWrongHost
|
2015-05-05 17:39:35 +02:00
|
|
|
case res of
|
2015-05-25 09:51:35 +02:00
|
|
|
ConnectionError _ -> return ()
|
2015-05-05 17:39:35 +02:00
|
|
|
_ -> fail $ "expected ConnectionError, but got " <> show res
|
|
|
|
|
|
|
|
it "reports UnsupportedContentType" $ do
|
2015-09-12 14:11:24 +02:00
|
|
|
Left res <- runExceptT getGet
|
2015-05-05 17:39:35 +02:00
|
|
|
case res of
|
|
|
|
UnsupportedContentType ("application/octet-stream") _ -> return ()
|
|
|
|
_ -> fail $ "expected UnsupportedContentType, but got " <> show res
|
|
|
|
|
|
|
|
it "reports InvalidContentTypeHeader" $ do
|
2015-09-12 14:11:24 +02:00
|
|
|
Left res <- runExceptT (getBody alice)
|
2015-05-05 17:39:35 +02:00
|
|
|
case res of
|
|
|
|
InvalidContentTypeHeader "fooooo" _ -> return ()
|
|
|
|
_ -> fail $ "expected InvalidContentTypeHeader, but got " <> show res
|
2015-03-08 23:59:40 +01:00
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
data WrappedApi where
|
2015-09-12 14:11:24 +02:00
|
|
|
WrappedApi :: (HasServer api, Server api ~ ExceptT ServantErr IO a,
|
|
|
|
HasClient api, Client api ~ ExceptT ServantError IO ()) =>
|
2014-11-27 18:28:01 +01:00
|
|
|
Proxy api -> WrappedApi
|
|
|
|
|
|
|
|
|
|
|
|
-- * utils
|
|
|
|
|
|
|
|
withWaiDaemon :: IO Application -> (BaseUrl -> IO a) -> IO a
|
|
|
|
withWaiDaemon mkApplication action = do
|
|
|
|
application <- mkApplication
|
|
|
|
bracket (acquire application) free (\ (_, _, baseUrl) -> action baseUrl)
|
|
|
|
where
|
|
|
|
acquire application = do
|
|
|
|
(notifyStart, waitForStart) <- lvar
|
|
|
|
(notifyKilled, waitForKilled) <- lvar
|
|
|
|
thread <- forkIO $ (do
|
|
|
|
(krakenPort, socket) <- openTestSocket
|
|
|
|
let settings =
|
|
|
|
setPort krakenPort $ -- set here just for consistency, shouldn't be
|
|
|
|
-- used (it's set in the socket)
|
|
|
|
setBeforeMainLoop (notifyStart krakenPort)
|
|
|
|
defaultSettings
|
|
|
|
runSettingsSocket settings socket application)
|
|
|
|
`finally` notifyKilled ()
|
|
|
|
krakenPort <- waitForStart
|
2015-08-25 04:26:15 +02:00
|
|
|
let baseUrl = (BaseUrl Http "localhost" 80 ""){baseUrlPort = krakenPort}
|
2014-11-27 18:28:01 +01:00
|
|
|
return (thread, waitForKilled, baseUrl)
|
|
|
|
free (thread, waitForKilled, _) = do
|
|
|
|
killThread thread
|
|
|
|
waitForKilled
|
|
|
|
|
|
|
|
lvar :: IO (a -> IO (), IO a)
|
|
|
|
lvar = do
|
|
|
|
mvar <- newEmptyMVar
|
|
|
|
let put = putMVar mvar
|
|
|
|
wait = readMVar mvar
|
|
|
|
return (put, wait)
|
|
|
|
|
|
|
|
openTestSocket :: IO (Port, Socket)
|
|
|
|
openTestSocket = do
|
|
|
|
s <- socket AF_INET Stream defaultProtocol
|
|
|
|
localhost <- inet_addr "127.0.0.1"
|
|
|
|
bind s (SockAddrInet aNY_PORT localhost)
|
|
|
|
listen s 1
|
|
|
|
port <- socketPort s
|
|
|
|
return (fromIntegral port, s)
|
|
|
|
|
2015-02-02 03:35:10 +01:00
|
|
|
pathGen :: Gen (NonEmptyList Char)
|
|
|
|
pathGen = fmap NonEmpty path
|
|
|
|
where
|
|
|
|
path = listOf1 $ elements $
|
2015-04-20 19:52:29 +02:00
|
|
|
filter (not . (`elem` ("?%[]/#;" :: String))) $
|
2015-02-02 03:35:10 +01:00
|
|
|
filter isPrint $
|
|
|
|
map chr [0..127]
|