on master help sign in
test suite: Union (`:<|>`)
commitc393afa08e61266afd0408da1c33e2495355dc92 authorSönke Hahn <soenke.hahn@zalora.com> dateMon 27 Oct 2014, 15:52 (12 years ago) parentd0782db
1 file changed+45−1
test/Servant/ServerSpec.hs+45−1
test/Servant/ServerSpec.hs+45−1
@@ -13,15 +13,18 @@ import Data.Proxy
1313import GHC.Generics
1414import Network.Wai.Test
1515import Test.Hspec
16−import Test.Hspec.Wai
16+import Test.Hspec.Wai as Wai
1717
1818import Servant.API.Get
1919import Servant.API.Post
2020import Servant.API.RQBody
2121import Servant.API.Sub
22+import Servant.API.Union
2223import Servant.Server
2324
2425
26+-- * test data types
27+
2528data Person = Person {
2629 name :: String,
2730 age :: Integer
@@ -34,11 +37,26 @@ instance FromJSON Person
3437alice :: Person
3538alice = Person "Alice" 42
3639
40+data Animal = Animal {
41+ species :: String,
42+ numberOfLegs :: Integer
43+ }
44+ deriving (Eq, Show, Generic)
45+
46+instance ToJSON Animal
47+instance FromJSON Animal
48+
49+jerry :: Animal
50+jerry = Animal "Mouse" 4
51+
52+
53+-- * specs
3754
3855spec :: Spec
3956spec = do
4057 getSpec
4158 postSpec
59+ unionSpec
4260
4361
4462type GetApi = Get Person
@@ -70,3 +88,29 @@ postSpec = do
7088 post "/" (encode alice) `shouldRespondWith` "42"{
7189 matchStatus = 201
7290 }
91+
92+
93+type UnionApi =
94+ "foo" :> Get Person
95+ :<|> "bar" :> Get Animal
96+unionApi :: Proxy UnionApi
97+unionApi = Proxy
98+
99+unionServer :: Server UnionApi
100+unionServer =
101+ return alice
102+ :<|> return jerry
103+
104+unionSpec :: Spec
105+unionSpec = do
106+ describe "Servant.API.Union" $ do
107+ with (return $ serve unionApi unionServer) $ do
108+ it "unions endpoints" $ do
109+ response <- get "/foo"
110+ liftIO $ do
111+ decode' (simpleBody response) `shouldBe`
112+ Just alice
113+ response <- get "/bar"
114+ liftIO $ do
115+ decode' (simpleBody response) `shouldBe`
116+ Just jerry