test suite: Union (:<|>
)
This commit is contained in:
parent
d0782db08b
commit
c393afa08e
1 changed files with 45 additions and 1 deletions
|
@ -13,15 +13,18 @@ import Data.Proxy
|
|||
import GHC.Generics
|
||||
import Network.Wai.Test
|
||||
import Test.Hspec
|
||||
import Test.Hspec.Wai
|
||||
import Test.Hspec.Wai as Wai
|
||||
|
||||
import Servant.API.Get
|
||||
import Servant.API.Post
|
||||
import Servant.API.RQBody
|
||||
import Servant.API.Sub
|
||||
import Servant.API.Union
|
||||
import Servant.Server
|
||||
|
||||
|
||||
-- * test data types
|
||||
|
||||
data Person = Person {
|
||||
name :: String,
|
||||
age :: Integer
|
||||
|
@ -34,11 +37,26 @@ instance FromJSON Person
|
|||
alice :: Person
|
||||
alice = Person "Alice" 42
|
||||
|
||||
data Animal = Animal {
|
||||
species :: String,
|
||||
numberOfLegs :: Integer
|
||||
}
|
||||
deriving (Eq, Show, Generic)
|
||||
|
||||
instance ToJSON Animal
|
||||
instance FromJSON Animal
|
||||
|
||||
jerry :: Animal
|
||||
jerry = Animal "Mouse" 4
|
||||
|
||||
|
||||
-- * specs
|
||||
|
||||
spec :: Spec
|
||||
spec = do
|
||||
getSpec
|
||||
postSpec
|
||||
unionSpec
|
||||
|
||||
|
||||
type GetApi = Get Person
|
||||
|
@ -70,3 +88,29 @@ postSpec = do
|
|||
post "/" (encode alice) `shouldRespondWith` "42"{
|
||||
matchStatus = 201
|
||||
}
|
||||
|
||||
|
||||
type UnionApi =
|
||||
"foo" :> Get Person
|
||||
:<|> "bar" :> Get Animal
|
||||
unionApi :: Proxy UnionApi
|
||||
unionApi = Proxy
|
||||
|
||||
unionServer :: Server UnionApi
|
||||
unionServer =
|
||||
return alice
|
||||
:<|> return jerry
|
||||
|
||||
unionSpec :: Spec
|
||||
unionSpec = do
|
||||
describe "Servant.API.Union" $ do
|
||||
with (return $ serve unionApi unionServer) $ do
|
||||
it "unions endpoints" $ do
|
||||
response <- get "/foo"
|
||||
liftIO $ do
|
||||
decode' (simpleBody response) `shouldBe`
|
||||
Just alice
|
||||
response <- get "/bar"
|
||||
liftIO $ do
|
||||
decode' (simpleBody response) `shouldBe`
|
||||
Just jerry
|
||||
|
|
Loading…
Reference in a new issue