on master help sign in
test suite: Union (`:<|>`)
c393afaSönke Hahn
test/Servant/ServerSpec.hs+45−1
@@ -13,15 +13,18 @@ import Data.Proxy
13import GHC.Generics
13import GHC.Generics
14import Network.Wai.Test
14import Network.Wai.Test
15import Test.Hspec
15import Test.Hspec
16−import Test.Hspec.Wai
16+import Test.Hspec.Wai as Wai
17
17
18import Servant.API.Get
18import Servant.API.Get
19import Servant.API.Post
19import Servant.API.Post
20import Servant.API.RQBody
20import Servant.API.RQBody
21import Servant.API.Sub
21import Servant.API.Sub
22+import Servant.API.Union
22import Servant.Server
23import Servant.Server
23
24
24
25
26+-- * test data types
27+
25data Person = Person {
28data Person = Person {
26 name :: String,
29 name :: String,
27 age :: Integer
30 age :: Integer
@@ -34,11 +37,26 @@ instance FromJSON Person
34alice :: Person
37alice :: Person
35alice = Person "Alice" 42
38alice = Person "Alice" 42
36
39
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
37
54
38spec :: Spec
55spec :: Spec
39spec = do
56spec = do
40 getSpec
57 getSpec
41 postSpec
58 postSpec
59+ unionSpec
42
60
43
61
44type GetApi = Get Person
62type GetApi = Get Person
@@ -70,3 +88,29 @@ postSpec = do
70 post "/" (encode alice) `shouldRespondWith` "42"{
88 post "/" (encode alice) `shouldRespondWith` "42"{
71 matchStatus = 201
89 matchStatus = 201
72 }
90 }
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