2015-10-07 17:50:29 +02:00
|
|
|
{-# LANGUAGE CPP #-}
|
2016-01-14 23:43:48 +01:00
|
|
|
{-# LANGUAGE ConstraintKinds #-}
|
2015-10-07 17:50:29 +02:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
{-# LANGUAGE FunctionalDependencies #-}
|
|
|
|
{-# LANGUAGE GADTs #-}
|
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
2015-10-09 00:48:03 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2015-10-07 17:50:29 +02:00
|
|
|
{-# LANGUAGE PolyKinds #-}
|
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
2016-10-15 23:43:21 +02:00
|
|
|
{-# LANGUAGE StandaloneDeriving #-}
|
2016-02-17 21:45:08 +01:00
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
2015-10-07 17:50:29 +02:00
|
|
|
{-# LANGUAGE TypeOperators #-}
|
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
2016-04-22 08:32:39 +02:00
|
|
|
#if __GLASGOW_HASKELL__ >= 800
|
|
|
|
{-# OPTIONS_GHC -freduction-depth=100 #-}
|
|
|
|
#else
|
2015-05-02 03:21:03 +02:00
|
|
|
{-# OPTIONS_GHC -fcontext-stack=100 #-}
|
2016-04-22 08:32:39 +02:00
|
|
|
#endif
|
2015-03-08 23:59:40 +01:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2015-10-09 00:02:43 +02:00
|
|
|
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
|
2015-05-05 17:39:35 +02:00
|
|
|
|
2015-12-27 17:54:29 +01:00
|
|
|
#include "overlapping-compat.h"
|
2017-09-13 01:49:55 +02:00
|
|
|
module Servant.ClientSpec (spec) where
|
2014-11-27 18:28:01 +01:00
|
|
|
|
2017-09-12 20:43:16 +02:00
|
|
|
import Prelude ()
|
|
|
|
import Prelude.Compat
|
|
|
|
|
2017-09-12 18:38:52 +02:00
|
|
|
import Control.Arrow (left)
|
|
|
|
import Control.Concurrent (ThreadId, forkIO,
|
|
|
|
killThread)
|
|
|
|
import Control.Exception (bracket)
|
|
|
|
import Control.Monad.Error.Class (throwError)
|
2015-04-19 18:31:23 +02:00
|
|
|
import Data.Aeson
|
2017-09-12 18:38:52 +02:00
|
|
|
import Data.Char (chr, isPrint)
|
|
|
|
import Data.Foldable (forM_)
|
|
|
|
import Data.Monoid hiding (getLast)
|
2015-04-19 18:31:23 +02:00
|
|
|
import Data.Proxy
|
2017-09-12 18:38:52 +02:00
|
|
|
import qualified Generics.SOP as SOP
|
|
|
|
import GHC.Generics (Generic)
|
|
|
|
import qualified Network.HTTP.Client as C
|
|
|
|
import qualified Network.HTTP.Types as HTTP
|
2015-04-19 18:31:23 +02:00
|
|
|
import Network.Socket
|
2017-09-12 18:38:52 +02:00
|
|
|
import qualified Network.Wai as Wai
|
2015-04-19 18:31:23 +02:00
|
|
|
import Network.Wai.Handler.Warp
|
2017-09-12 18:38:52 +02:00
|
|
|
import System.IO.Unsafe (unsafePerformIO)
|
2015-04-19 18:31:23 +02:00
|
|
|
import Test.Hspec
|
|
|
|
import Test.Hspec.QuickCheck
|
2017-09-12 18:38:52 +02:00
|
|
|
import Test.HUnit
|
2015-04-19 18:31:23 +02:00
|
|
|
import Test.QuickCheck
|
2017-09-12 18:38:52 +02:00
|
|
|
import Web.FormUrlEncoded (FromForm, ToForm)
|
|
|
|
|
|
|
|
import Servant.API ((:<|>) ((:<|>)),
|
|
|
|
(:>), AuthProtect,
|
|
|
|
BasicAuth,
|
|
|
|
BasicAuthData (..),
|
|
|
|
Capture,
|
|
|
|
CaptureAll, Delete,
|
|
|
|
DeleteNoContent,
|
2017-09-12 20:43:16 +02:00
|
|
|
EmptyAPI, addHeader,
|
2017-09-12 18:38:52 +02:00
|
|
|
FormUrlEncoded,
|
|
|
|
Get, Header,
|
|
|
|
Headers, JSON,
|
2017-09-12 20:43:16 +02:00
|
|
|
NoContent (NoContent),
|
|
|
|
Post, Put, Raw,
|
|
|
|
QueryFlag,
|
2017-09-12 18:38:52 +02:00
|
|
|
QueryParam,
|
|
|
|
QueryParams,
|
2017-09-12 20:43:16 +02:00
|
|
|
ReqBody,
|
|
|
|
getHeaders)
|
2016-01-16 19:17:46 +01:00
|
|
|
import Servant.API.Internal.Test.ComprehensiveAPI
|
2015-04-19 18:31:23 +02:00
|
|
|
import Servant.Client
|
|
|
|
import Servant.Server
|
2016-03-06 22:23:55 +01:00
|
|
|
import Servant.Server.Experimental.Auth
|
2014-11-27 18:28:01 +01:00
|
|
|
|
2016-01-18 19:55:14 +01:00
|
|
|
-- This declaration simply checks that all instances are in place.
|
2017-09-12 18:38:52 +02:00
|
|
|
_ = client comprehensiveAPI
|
2016-01-16 19:17:46 +01:00
|
|
|
|
2015-10-07 17:50:29 +02:00
|
|
|
spec :: Spec
|
|
|
|
spec = describe "Servant.Client" $ do
|
|
|
|
sucessSpec
|
|
|
|
failSpec
|
|
|
|
wrappedApiSpec
|
2016-02-17 20:12:47 +01:00
|
|
|
basicAuthSpec
|
2016-02-17 21:45:08 +01:00
|
|
|
genAuthSpec
|
2016-11-21 12:27:11 +01:00
|
|
|
genericClientSpec
|
2015-10-07 17:50:29 +02:00
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
-- * test data types
|
|
|
|
|
2017-09-12 18:38:52 +02:00
|
|
|
data Person = Person
|
2017-09-13 01:49:55 +02:00
|
|
|
{ _name :: String
|
|
|
|
, _age :: Integer
|
2017-09-12 18:38:52 +02:00
|
|
|
} deriving (Eq, Show, Generic)
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
instance ToJSON Person
|
|
|
|
instance FromJSON Person
|
|
|
|
|
2017-09-12 18:38:52 +02:00
|
|
|
instance ToForm Person
|
|
|
|
instance FromForm Person
|
2015-02-24 23:56:06 +01:00
|
|
|
|
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
|
2016-01-07 14:30:08 +01:00
|
|
|
:<|> "deleteEmpty" :> DeleteNoContent '[JSON] NoContent
|
2015-02-24 23:56:06 +01:00
|
|
|
:<|> "capture" :> Capture "name" String :> Get '[JSON,FormUrlEncoded] Person
|
2016-06-07 10:50:46 +02:00
|
|
|
:<|> "captureAll" :> CaptureAll "names" String :> Get '[JSON] [Person]
|
2015-02-24 23:56:06 +01:00
|
|
|
:<|> "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
|
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)
|
2016-01-07 14:30:08 +01:00
|
|
|
:<|> "deleteContentType" :> DeleteNoContent '[JSON] NoContent
|
2017-05-16 12:21:37 +02:00
|
|
|
:<|> "empty" :> EmptyAPI
|
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
api :: Proxy Api
|
|
|
|
api = Proxy
|
|
|
|
|
2017-09-12 18:38:52 +02:00
|
|
|
getGet :: ClientM Person
|
|
|
|
getDeleteEmpty :: ClientM NoContent
|
|
|
|
getCapture :: String -> ClientM Person
|
|
|
|
getCaptureAll :: [String] -> ClientM [Person]
|
|
|
|
getBody :: Person -> ClientM Person
|
|
|
|
getQueryParam :: Maybe String -> ClientM Person
|
|
|
|
getQueryParams :: [String] -> ClientM [Person]
|
|
|
|
getQueryFlag :: Bool -> ClientM Bool
|
2017-09-12 20:43:16 +02:00
|
|
|
getRawSuccess :: HTTP.Method -> ClientM Response
|
|
|
|
getRawFailure :: HTTP.Method -> ClientM Response
|
2016-09-08 00:03:44 +02:00
|
|
|
getMultiple :: String -> Maybe Int -> Bool -> [(String, [Rational])]
|
2017-09-12 18:38:52 +02:00
|
|
|
-> ClientM (String, Maybe Int, Bool, [(String, [Rational])])
|
|
|
|
getRespHeaders :: ClientM (Headers TestHeaders Bool)
|
|
|
|
getDeleteContentType :: ClientM NoContent
|
2017-05-16 12:21:37 +02:00
|
|
|
|
2016-03-28 19:01:53 +02:00
|
|
|
getGet
|
2016-03-30 23:41:39 +02:00
|
|
|
:<|> getDeleteEmpty
|
|
|
|
:<|> getCapture
|
2016-05-26 18:10:33 +02:00
|
|
|
:<|> getCaptureAll
|
2016-03-30 23:41:39 +02:00
|
|
|
:<|> getBody
|
|
|
|
:<|> getQueryParam
|
|
|
|
:<|> getQueryParams
|
|
|
|
:<|> getQueryFlag
|
|
|
|
:<|> getRawSuccess
|
|
|
|
:<|> getRawFailure
|
|
|
|
:<|> getMultiple
|
|
|
|
:<|> getRespHeaders
|
2017-05-16 12:21:37 +02:00
|
|
|
:<|> getDeleteContentType
|
2017-09-12 18:38:52 +02:00
|
|
|
:<|> EmptyClient = client api
|
2016-03-28 19:01:53 +02:00
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
server :: Application
|
2016-02-18 16:36:24 +01:00
|
|
|
server = serve api (
|
2014-11-27 18:28:01 +01:00
|
|
|
return alice
|
2016-01-07 14:30:08 +01:00
|
|
|
:<|> return NoContent
|
2014-11-27 18:28:01 +01:00
|
|
|
:<|> (\ name -> return $ Person name 0)
|
2016-06-07 10:50:46 +02:00
|
|
|
:<|> (\ names -> return (zipWith Person names [0..]))
|
2014-11-27 18:28:01 +01:00
|
|
|
:<|> return
|
|
|
|
:<|> (\ name -> case name of
|
|
|
|
Just "alice" -> return alice
|
2017-01-16 10:44:25 +01:00
|
|
|
Just n -> throwError $ ServantErr 400 (n ++ " not found") "" []
|
|
|
|
Nothing -> throwError $ ServantErr 400 "missing parameter" "" [])
|
2014-11-27 18:28:01 +01:00
|
|
|
:<|> (\ names -> return (zipWith Person names [0..]))
|
|
|
|
:<|> return
|
2017-09-12 18:38:52 +02:00
|
|
|
:<|> (Tagged $ \ _request respond -> respond $ Wai.responseLBS HTTP.ok200 [] "rawSuccess")
|
|
|
|
:<|> (Tagged $ \ _request respond -> respond $ Wai.responseLBS HTTP.badRequest400 [] "rawFailure")
|
2015-05-02 03:21:03 +02:00
|
|
|
:<|> (\ a b c d -> return (a, b, c, d))
|
2017-09-12 20:43:16 +02:00
|
|
|
:<|> (return $ Servant.API.addHeader 1729 $ Servant.API.addHeader "eg2" True)
|
2016-01-07 14:30:08 +01:00
|
|
|
:<|> return NoContent
|
2017-05-16 17:59:41 +02:00
|
|
|
:<|> emptyServer)
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
|
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
|
2016-02-18 16:36:24 +01:00
|
|
|
failServer = serve failApi (
|
2017-09-12 18:38:52 +02:00
|
|
|
(Tagged $ \ _request respond -> respond $ Wai.responseLBS HTTP.ok200 [] "")
|
|
|
|
:<|> (\ _capture -> Tagged $ \_request respond -> respond $ Wai.responseLBS HTTP.ok200 [("content-type", "application/json")] "")
|
|
|
|
:<|> (Tagged $ \_request respond -> respond $ Wai.responseLBS HTTP.ok200 [("content-type", "fooooo")] "")
|
2015-03-09 04:38:32 +01:00
|
|
|
)
|
|
|
|
|
2016-02-17 21:45:08 +01:00
|
|
|
-- * basic auth stuff
|
2016-02-17 20:12:47 +01:00
|
|
|
|
|
|
|
type BasicAuthAPI =
|
|
|
|
BasicAuth "foo-realm" () :> "private" :> "basic" :> Get '[JSON] Person
|
|
|
|
|
|
|
|
basicAuthAPI :: Proxy BasicAuthAPI
|
|
|
|
basicAuthAPI = Proxy
|
|
|
|
|
|
|
|
basicAuthHandler :: BasicAuthCheck ()
|
|
|
|
basicAuthHandler =
|
|
|
|
let check (BasicAuthData username password) =
|
|
|
|
if username == "servant" && password == "server"
|
|
|
|
then return (Authorized ())
|
|
|
|
else return Unauthorized
|
|
|
|
in BasicAuthCheck check
|
|
|
|
|
2016-03-06 22:23:55 +01:00
|
|
|
basicServerContext :: Context '[ BasicAuthCheck () ]
|
|
|
|
basicServerContext = basicAuthHandler :. EmptyContext
|
2016-02-17 20:12:47 +01:00
|
|
|
|
|
|
|
basicAuthServer :: Application
|
2016-03-06 22:23:55 +01:00
|
|
|
basicAuthServer = serveWithContext basicAuthAPI basicServerContext (const (return alice))
|
2016-02-17 20:12:47 +01:00
|
|
|
|
2016-02-17 21:45:08 +01:00
|
|
|
-- * general auth stuff
|
|
|
|
|
|
|
|
type GenAuthAPI =
|
|
|
|
AuthProtect "auth-tag" :> "private" :> "auth" :> Get '[JSON] Person
|
|
|
|
|
|
|
|
genAuthAPI :: Proxy GenAuthAPI
|
|
|
|
genAuthAPI = Proxy
|
|
|
|
|
|
|
|
type instance AuthServerData (AuthProtect "auth-tag") = ()
|
|
|
|
type instance AuthClientData (AuthProtect "auth-tag") = ()
|
|
|
|
|
2017-09-13 01:49:55 +02:00
|
|
|
genAuthHandler :: AuthHandler Wai.Request ()
|
2016-02-17 21:45:08 +01:00
|
|
|
genAuthHandler =
|
2017-09-13 01:49:55 +02:00
|
|
|
let handler req = case lookup "AuthHeader" (Wai.requestHeaders req) of
|
2017-01-16 10:44:25 +01:00
|
|
|
Nothing -> throwError (err401 { errBody = "Missing auth header" })
|
2016-02-17 21:45:08 +01:00
|
|
|
Just _ -> return ()
|
|
|
|
in mkAuthHandler handler
|
|
|
|
|
2017-09-13 01:49:55 +02:00
|
|
|
genAuthServerContext :: Context '[ AuthHandler Wai.Request () ]
|
2016-03-06 22:23:55 +01:00
|
|
|
genAuthServerContext = genAuthHandler :. EmptyContext
|
2016-02-17 21:45:08 +01:00
|
|
|
|
|
|
|
genAuthServer :: Application
|
2016-03-06 22:23:55 +01:00
|
|
|
genAuthServer = serveWithContext genAuthAPI genAuthServerContext (const (return alice))
|
2016-02-17 21:45:08 +01:00
|
|
|
|
2016-11-21 12:27:11 +01:00
|
|
|
-- * generic client stuff
|
|
|
|
|
|
|
|
type GenericClientAPI
|
|
|
|
= QueryParam "sqr" Int :> Get '[JSON] Int
|
|
|
|
:<|> Capture "foo" String :> NestedAPI1
|
|
|
|
|
|
|
|
data GenericClient = GenericClient
|
2017-09-12 18:38:52 +02:00
|
|
|
{ getSqr :: Maybe Int -> ClientM Int
|
2016-11-21 12:27:11 +01:00
|
|
|
, mkNestedClient1 :: String -> NestedClient1
|
|
|
|
} deriving Generic
|
|
|
|
instance SOP.Generic GenericClient
|
2017-09-03 08:49:24 +02:00
|
|
|
instance (Client ClientM GenericClientAPI ~ client) => ClientLike client GenericClient
|
2016-11-21 12:27:11 +01:00
|
|
|
|
|
|
|
type NestedAPI1
|
|
|
|
= QueryParam "int" Int :> NestedAPI2
|
|
|
|
:<|> QueryParam "id" Char :> Get '[JSON] Char
|
|
|
|
|
|
|
|
data NestedClient1 = NestedClient1
|
|
|
|
{ mkNestedClient2 :: Maybe Int -> NestedClient2
|
2017-09-12 18:38:52 +02:00
|
|
|
, idChar :: Maybe Char -> ClientM Char
|
2016-11-21 12:27:11 +01:00
|
|
|
} deriving Generic
|
|
|
|
instance SOP.Generic NestedClient1
|
2017-09-03 08:49:24 +02:00
|
|
|
instance (Client ClientM NestedAPI1 ~ client) => ClientLike client NestedClient1
|
2016-11-21 12:27:11 +01:00
|
|
|
|
|
|
|
type NestedAPI2
|
|
|
|
= "sum" :> Capture "first" Int :> Capture "second" Int :> Get '[JSON] Int
|
|
|
|
:<|> "void" :> Post '[JSON] ()
|
|
|
|
|
|
|
|
data NestedClient2 = NestedClient2
|
2017-09-12 18:38:52 +02:00
|
|
|
{ getSum :: Int -> Int -> ClientM Int
|
|
|
|
, doNothing :: ClientM ()
|
2016-11-21 12:27:11 +01:00
|
|
|
} deriving Generic
|
|
|
|
instance SOP.Generic NestedClient2
|
2017-09-03 08:49:24 +02:00
|
|
|
instance (Client ClientM NestedAPI2 ~ client) => ClientLike client NestedClient2
|
2016-11-21 12:27:11 +01:00
|
|
|
|
|
|
|
genericClientServer :: Application
|
|
|
|
genericClientServer = serve (Proxy :: Proxy GenericClientAPI) (
|
|
|
|
(\ mx -> case mx of
|
|
|
|
Just x -> return (x*x)
|
2017-01-17 14:34:30 +01:00
|
|
|
Nothing -> throwError $ ServantErr 400 "missing parameter" "" []
|
2016-11-21 12:27:11 +01:00
|
|
|
)
|
|
|
|
:<|> nestedServer1
|
|
|
|
)
|
|
|
|
where
|
2017-01-17 14:34:30 +01:00
|
|
|
nestedServer1 _str = nestedServer2 :<|> (maybe (throwError $ ServantErr 400 "missing parameter" "" []) return)
|
2016-11-21 12:27:11 +01:00
|
|
|
nestedServer2 _int = (\ x y -> return (x + y)) :<|> return ()
|
|
|
|
|
2017-09-12 18:38:52 +02:00
|
|
|
{-# NOINLINE manager' #-}
|
|
|
|
manager' :: C.Manager
|
|
|
|
manager' = unsafePerformIO $ C.newManager C.defaultManagerSettings
|
|
|
|
|
2017-09-13 01:49:55 +02:00
|
|
|
runClient :: ClientM a -> BaseUrl -> IO (Either ServantError a)
|
2017-09-12 20:43:16 +02:00
|
|
|
runClient x baseUrl' = runClientM x (ClientEnv manager' baseUrl')
|
2015-10-07 17:50:29 +02:00
|
|
|
|
|
|
|
sucessSpec :: Spec
|
|
|
|
sucessSpec = beforeAll (startWaiApp server) $ afterAll endWaiApp $ do
|
|
|
|
|
|
|
|
it "Servant.API.Get" $ \(_, baseUrl) -> do
|
2017-09-12 20:43:16 +02:00
|
|
|
left show <$> runClient getGet baseUrl `shouldReturn` Right alice
|
2015-05-05 17:39:35 +02:00
|
|
|
|
2015-05-17 15:51:49 +02:00
|
|
|
describe "Servant.API.Delete" $ do
|
2015-10-07 17:50:29 +02:00
|
|
|
it "allows empty content type" $ \(_, baseUrl) -> do
|
2017-09-12 20:43:16 +02:00
|
|
|
left show <$> runClient getDeleteEmpty baseUrl `shouldReturn` Right NoContent
|
2015-05-17 15:51:49 +02:00
|
|
|
|
2015-10-08 12:09:23 +02:00
|
|
|
it "allows content type" $ \(_, baseUrl) -> do
|
2017-09-12 20:43:16 +02:00
|
|
|
left show <$> runClient getDeleteContentType baseUrl `shouldReturn` Right NoContent
|
2015-05-05 17:39:35 +02:00
|
|
|
|
2015-10-07 17:50:29 +02:00
|
|
|
it "Servant.API.Capture" $ \(_, baseUrl) -> do
|
2017-09-12 20:43:16 +02:00
|
|
|
left show <$> runClient (getCapture "Paula") baseUrl `shouldReturn` Right (Person "Paula" 0)
|
2015-05-05 17:39:35 +02:00
|
|
|
|
2016-05-26 18:10:33 +02:00
|
|
|
it "Servant.API.CaptureAll" $ \(_, baseUrl) -> do
|
2016-06-07 10:50:46 +02:00
|
|
|
let expected = [(Person "Paula" 0), (Person "Peta" 1)]
|
2017-09-12 20:43:16 +02:00
|
|
|
left show <$> runClient (getCaptureAll ["Paula", "Peta"]) baseUrl `shouldReturn` Right expected
|
2016-05-26 18:10:33 +02:00
|
|
|
|
2015-10-07 17:50:29 +02:00
|
|
|
it "Servant.API.ReqBody" $ \(_, baseUrl) -> do
|
2015-05-05 17:39:35 +02:00
|
|
|
let p = Person "Clara" 42
|
2017-09-12 20:43:16 +02:00
|
|
|
left show <$> runClient (getBody p) baseUrl `shouldReturn` Right p
|
2015-05-05 17:39:35 +02:00
|
|
|
|
2015-10-07 17:50:29 +02:00
|
|
|
it "Servant.API.QueryParam" $ \(_, baseUrl) -> do
|
2017-09-12 20:43:16 +02:00
|
|
|
left show <$> runClient (getQueryParam (Just "alice")) baseUrl `shouldReturn` Right alice
|
|
|
|
Left (FailureResponse r) <- runClient (getQueryParam (Just "bob")) baseUrl
|
2017-09-12 18:38:52 +02:00
|
|
|
responseStatusCode r `shouldBe` HTTP.Status 400 "bob not found"
|
2015-05-05 17:39:35 +02:00
|
|
|
|
2015-10-07 17:50:29 +02:00
|
|
|
it "Servant.API.QueryParam.QueryParams" $ \(_, baseUrl) -> do
|
2017-09-12 20:43:16 +02:00
|
|
|
left show <$> runClient (getQueryParams []) baseUrl `shouldReturn` Right []
|
|
|
|
left show <$> runClient (getQueryParams ["alice", "bob"]) baseUrl
|
2015-05-05 17:39:35 +02:00
|
|
|
`shouldReturn` Right [Person "alice" 0, Person "bob" 1]
|
|
|
|
|
|
|
|
context "Servant.API.QueryParam.QueryFlag" $
|
2015-10-07 17:50:29 +02:00
|
|
|
forM_ [False, True] $ \ flag -> it (show flag) $ \(_, baseUrl) -> do
|
2017-09-12 20:43:16 +02:00
|
|
|
left show <$> runClient (getQueryFlag flag) baseUrl `shouldReturn` Right flag
|
2015-05-05 17:39:35 +02:00
|
|
|
|
2015-10-13 21:45:22 +02:00
|
|
|
it "Servant.API.Raw on success" $ \(_, baseUrl) -> do
|
2017-09-12 20:43:16 +02:00
|
|
|
res <- runClient (getRawSuccess HTTP.methodGet) baseUrl
|
2015-03-08 23:59:40 +01:00
|
|
|
case res of
|
2015-05-05 17:39:35 +02:00
|
|
|
Left e -> assertFailure $ show e
|
2017-09-12 20:43:16 +02:00
|
|
|
Right r -> do
|
|
|
|
responseStatusCode r `shouldBe` HTTP.status200
|
|
|
|
responseBody r `shouldBe` "rawSuccess"
|
2015-05-05 17:39:35 +02:00
|
|
|
|
2015-10-11 21:47:31 +02:00
|
|
|
it "Servant.API.Raw should return a Left in case of failure" $ \(_, baseUrl) -> do
|
2017-09-12 20:43:16 +02:00
|
|
|
res <- runClient (getRawFailure HTTP.methodGet) baseUrl
|
2015-03-09 04:38:32 +01:00
|
|
|
case res of
|
2015-10-07 15:24:52 +02:00
|
|
|
Right _ -> assertFailure "expected Left, but got Right"
|
2017-09-12 18:38:52 +02:00
|
|
|
Left (FailureResponse r) -> do
|
|
|
|
responseStatusCode r `shouldBe` HTTP.status400
|
|
|
|
responseBody r `shouldBe` "rawFailure"
|
|
|
|
Left e -> assertFailure $ "expected FailureResponse, but got " ++ show e
|
2015-05-05 17:39:35 +02:00
|
|
|
|
2015-10-07 17:50:29 +02:00
|
|
|
it "Returns headers appropriately" $ \(_, baseUrl) -> do
|
2017-09-12 20:43:16 +02:00
|
|
|
res <- runClient getRespHeaders baseUrl
|
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
|
2015-10-07 17:50:29 +02:00
|
|
|
it "works for a combination of Capture, QueryParam, QueryFlag and ReqBody" $ \(_, baseUrl) ->
|
2016-03-28 19:01:53 +02:00
|
|
|
property $ forAllShrink pathGen shrink $ \(NonEmpty cap) num flag body ->
|
2015-05-05 17:39:35 +02:00
|
|
|
ioProperty $ do
|
2017-09-12 20:43:16 +02:00
|
|
|
result <- left show <$> runClient (getMultiple cap num flag body) baseUrl
|
2015-05-05 17:39:35 +02:00
|
|
|
return $
|
|
|
|
result === Right (cap, num, flag, body)
|
2015-03-09 04:38:32 +01:00
|
|
|
|
|
|
|
|
2015-10-07 17:50:29 +02:00
|
|
|
wrappedApiSpec :: Spec
|
|
|
|
wrappedApiSpec = describe "error status codes" $ do
|
2017-01-16 10:44:25 +01:00
|
|
|
let serveW api = serve api $ throwError $ ServantErr 500 "error message" "" []
|
2015-10-07 17:50:29 +02:00
|
|
|
context "are correctly handled by the client" $
|
|
|
|
let test :: (WrappedApi, String) -> Spec
|
|
|
|
test (WrappedApi api, desc) =
|
|
|
|
it desc $ bracket (startWaiApp $ serveW api) endWaiApp $ \(_, baseUrl) -> do
|
2017-09-12 18:38:52 +02:00
|
|
|
let getResponse :: ClientM ()
|
|
|
|
getResponse = client api
|
2017-09-12 20:43:16 +02:00
|
|
|
Left (FailureResponse r) <- runClient getResponse baseUrl
|
2017-09-12 18:38:52 +02:00
|
|
|
responseStatusCode r `shouldBe` (HTTP.Status 500 "error message")
|
2015-10-07 17:50:29 +02:00
|
|
|
in 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") :
|
|
|
|
[]
|
|
|
|
|
2015-10-07 17:50:29 +02:00
|
|
|
failSpec :: Spec
|
|
|
|
failSpec = beforeAll (startWaiApp failServer) $ afterAll endWaiApp $ do
|
|
|
|
|
2015-05-05 17:39:35 +02:00
|
|
|
context "client returns errors appropriately" $ do
|
2015-10-07 17:50:29 +02:00
|
|
|
it "reports FailureResponse" $ \(_, baseUrl) -> do
|
2017-09-12 18:38:52 +02:00
|
|
|
let (_ :<|> getDeleteEmpty :<|> _) = client api
|
2017-09-12 20:43:16 +02:00
|
|
|
Left res <- runClient getDeleteEmpty baseUrl
|
2015-05-05 17:39:35 +02:00
|
|
|
case res of
|
2017-09-12 20:43:16 +02:00
|
|
|
FailureResponse r | responseStatusCode r == HTTP.status404 -> return ()
|
2015-05-05 17:39:35 +02:00
|
|
|
_ -> fail $ "expected 404 response, but got " <> show res
|
|
|
|
|
2015-10-07 17:50:29 +02:00
|
|
|
it "reports DecodeFailure" $ \(_, baseUrl) -> do
|
2017-09-12 18:38:52 +02:00
|
|
|
let (_ :<|> _ :<|> getCapture :<|> _) = client api
|
2017-09-12 20:43:16 +02:00
|
|
|
Left res <- runClient (getCapture "foo") baseUrl
|
2015-05-05 17:39:35 +02:00
|
|
|
case res of
|
2017-09-12 18:38:52 +02:00
|
|
|
DecodeFailure _ _ -> return ()
|
2015-05-05 17:39:35 +02:00
|
|
|
_ -> fail $ "expected DecodeFailure, but got " <> show res
|
|
|
|
|
2015-10-07 17:50:29 +02:00
|
|
|
it "reports ConnectionError" $ \_ -> do
|
2017-09-12 18:38:52 +02:00
|
|
|
let (getGetWrongHost :<|> _) = client api
|
2017-09-12 20:43:16 +02:00
|
|
|
Left res <- runClient getGetWrongHost (BaseUrl Http "127.0.0.1" 19872 "")
|
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
|
|
|
|
|
2015-10-07 17:50:29 +02:00
|
|
|
it "reports UnsupportedContentType" $ \(_, baseUrl) -> do
|
2017-09-12 18:38:52 +02:00
|
|
|
let (getGet :<|> _ ) = client api
|
2017-09-12 20:43:16 +02:00
|
|
|
Left res <- runClient getGet baseUrl
|
2015-05-05 17:39:35 +02:00
|
|
|
case res of
|
|
|
|
UnsupportedContentType ("application/octet-stream") _ -> return ()
|
|
|
|
_ -> fail $ "expected UnsupportedContentType, but got " <> show res
|
|
|
|
|
2015-10-07 17:50:29 +02:00
|
|
|
it "reports InvalidContentTypeHeader" $ \(_, baseUrl) -> do
|
2017-09-12 18:38:52 +02:00
|
|
|
let (_ :<|> _ :<|> _ :<|> _ :<|> getBody :<|> _) = client api
|
2017-09-12 20:43:16 +02:00
|
|
|
Left res <- runClient (getBody alice) baseUrl
|
2015-05-05 17:39:35 +02:00
|
|
|
case res of
|
2017-09-12 18:38:52 +02:00
|
|
|
InvalidContentTypeHeader _ -> return ()
|
2015-05-05 17:39:35 +02:00
|
|
|
_ -> 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
|
2016-04-07 23:34:23 +02:00
|
|
|
WrappedApi :: (HasServer (api :: *) '[], Server api ~ Handler a,
|
2017-09-12 18:38:52 +02:00
|
|
|
HasClient ClientM api, Client ClientM api ~ ClientM ()) =>
|
2014-11-27 18:28:01 +01:00
|
|
|
Proxy api -> WrappedApi
|
|
|
|
|
2016-02-17 20:12:47 +01:00
|
|
|
basicAuthSpec :: Spec
|
|
|
|
basicAuthSpec = beforeAll (startWaiApp basicAuthServer) $ afterAll endWaiApp $ do
|
|
|
|
context "Authentication works when requests are properly authenticated" $ do
|
|
|
|
|
|
|
|
it "Authenticates a BasicAuth protected server appropriately" $ \(_,baseUrl) -> do
|
2017-09-12 18:38:52 +02:00
|
|
|
let getBasic = client basicAuthAPI
|
2016-02-17 20:12:47 +01:00
|
|
|
let basicAuthData = BasicAuthData "servant" "server"
|
2017-09-12 20:43:16 +02:00
|
|
|
left show <$> runClient (getBasic basicAuthData) baseUrl `shouldReturn` Right alice
|
2016-02-17 20:12:47 +01:00
|
|
|
|
|
|
|
context "Authentication is rejected when requests are not authenticated properly" $ do
|
|
|
|
|
|
|
|
it "Authenticates a BasicAuth protected server appropriately" $ \(_,baseUrl) -> do
|
2017-09-12 18:38:52 +02:00
|
|
|
let getBasic = client basicAuthAPI
|
2016-02-17 20:12:47 +01:00
|
|
|
let basicAuthData = BasicAuthData "not" "password"
|
2017-09-12 20:43:16 +02:00
|
|
|
Left (FailureResponse r) <- runClient (getBasic basicAuthData) baseUrl
|
2017-09-12 18:38:52 +02:00
|
|
|
responseStatusCode r `shouldBe` HTTP.Status 403 "Forbidden"
|
2014-11-27 18:28:01 +01:00
|
|
|
|
2016-02-17 21:45:08 +01:00
|
|
|
genAuthSpec :: Spec
|
|
|
|
genAuthSpec = beforeAll (startWaiApp genAuthServer) $ afterAll endWaiApp $ do
|
|
|
|
context "Authentication works when requests are properly authenticated" $ do
|
|
|
|
|
|
|
|
it "Authenticates a AuthProtect protected server appropriately" $ \(_, baseUrl) -> do
|
2017-09-12 18:38:52 +02:00
|
|
|
let getProtected = client genAuthAPI
|
2017-09-12 20:43:16 +02:00
|
|
|
let authRequest = mkAuthenticateReq () (\_ req -> Servant.Client.addHeader "AuthHeader" ("cool" :: String) req)
|
|
|
|
left show <$> runClient (getProtected authRequest) baseUrl `shouldReturn` Right alice
|
2016-02-17 21:45:08 +01:00
|
|
|
|
|
|
|
context "Authentication is rejected when requests are not authenticated properly" $ do
|
|
|
|
|
|
|
|
it "Authenticates a AuthProtect protected server appropriately" $ \(_, baseUrl) -> do
|
2017-09-12 18:38:52 +02:00
|
|
|
let getProtected = client genAuthAPI
|
2017-09-12 20:43:16 +02:00
|
|
|
let authRequest = mkAuthenticateReq () (\_ req -> Servant.Client.addHeader "Wrong" ("header" :: String) req)
|
|
|
|
Left (FailureResponse r) <- runClient (getProtected authRequest) baseUrl
|
2017-09-12 18:38:52 +02:00
|
|
|
responseStatusCode r `shouldBe` (HTTP.Status 401 "Unauthorized")
|
2016-02-17 21:45:08 +01:00
|
|
|
|
2016-11-21 12:27:11 +01:00
|
|
|
genericClientSpec :: Spec
|
|
|
|
genericClientSpec = beforeAll (startWaiApp genericClientServer) $ afterAll endWaiApp $ do
|
|
|
|
describe "Servant.Client.Generic" $ do
|
|
|
|
|
2017-09-12 18:38:52 +02:00
|
|
|
let GenericClient{..} = mkClient (client (Proxy :: Proxy GenericClientAPI))
|
2016-11-21 12:27:11 +01:00
|
|
|
NestedClient1{..} = mkNestedClient1 "example"
|
|
|
|
NestedClient2{..} = mkNestedClient2 (Just 42)
|
|
|
|
|
2017-09-04 09:19:08 +02:00
|
|
|
it "works for top-level client inClientM function" $ \(_, baseUrl) -> do
|
2017-09-12 20:43:16 +02:00
|
|
|
left show <$> runClient (getSqr (Just 5)) baseUrl `shouldReturn` Right 25
|
2016-11-21 12:27:11 +01:00
|
|
|
|
|
|
|
it "works for nested clients" $ \(_, baseUrl) -> do
|
2017-09-12 20:43:16 +02:00
|
|
|
left show <$> runClient (idChar (Just 'c')) baseUrl `shouldReturn` Right 'c'
|
|
|
|
left show <$> runClient (getSum 3 4) baseUrl `shouldReturn` Right 7
|
|
|
|
left show <$> runClient doNothing baseUrl `shouldReturn` Right ()
|
2016-11-21 12:27:11 +01:00
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
-- * utils
|
|
|
|
|
2015-10-07 17:50:29 +02:00
|
|
|
startWaiApp :: Application -> IO (ThreadId, BaseUrl)
|
|
|
|
startWaiApp app = do
|
|
|
|
(port, socket) <- openTestSocket
|
|
|
|
let settings = setPort port $ defaultSettings
|
|
|
|
thread <- forkIO $ runSettingsSocket settings socket app
|
|
|
|
return (thread, BaseUrl Http "localhost" port "")
|
|
|
|
|
|
|
|
|
|
|
|
endWaiApp :: (ThreadId, BaseUrl) -> IO ()
|
|
|
|
endWaiApp (thread, _) = killThread thread
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
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]
|