test suite: Union (:<|>)

This commit is contained in:
Sönke Hahn 2014-10-27 15:52:18 +08:00
parent d0782db08b
commit c393afa08e

View file

@ -13,15 +13,18 @@ import Data.Proxy
import GHC.Generics import GHC.Generics
import Network.Wai.Test import Network.Wai.Test
import Test.Hspec import Test.Hspec
import Test.Hspec.Wai import Test.Hspec.Wai as Wai
import Servant.API.Get import Servant.API.Get
import Servant.API.Post import Servant.API.Post
import Servant.API.RQBody import Servant.API.RQBody
import Servant.API.Sub import Servant.API.Sub
import Servant.API.Union
import Servant.Server import Servant.Server
-- * test data types
data Person = Person { data Person = Person {
name :: String, name :: String,
age :: Integer age :: Integer
@ -34,11 +37,26 @@ instance FromJSON Person
alice :: Person alice :: Person
alice = Person "Alice" 42 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 :: Spec
spec = do spec = do
getSpec getSpec
postSpec postSpec
unionSpec
type GetApi = Get Person type GetApi = Get Person
@ -70,3 +88,29 @@ postSpec = do
post "/" (encode alice) `shouldRespondWith` "42"{ post "/" (encode alice) `shouldRespondWith` "42"{
matchStatus = 201 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