git.iko.soy/typeable/servant
on master help sign in
test suite: Post
d0782dbSönke Hahn
src/Servant/API/RQBody.hs−1
@@ -8,7 +8,6 @@ module Servant.API.RQBody where
8import Control.Applicative
8import Control.Applicative
9import Data.Aeson
9import Data.Aeson
10import Data.Proxy
10import Data.Proxy
11−import Data.Text
12import Network.Wai
11import Network.Wai
13import Servant.API.Sub
12import Servant.API.Sub
14import Servant.Client
13import Servant.Client
test/Servant/ServerSpec.hs+26−3
@@ -1,7 +1,9 @@
1+{-# LANGUAGE DataKinds #-}
1{-# LANGUAGE DeriveGeneric #-}
2{-# LANGUAGE DeriveGeneric #-}
2{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE QuasiQuotes #-}
4{-# LANGUAGE QuasiQuotes #-}
4{-# LANGUAGE ScopedTypeVariables #-}
5{-# LANGUAGE ScopedTypeVariables #-}
6+{-# LANGUAGE TypeOperators #-}
5
7
6module Servant.ServerSpec where
8module Servant.ServerSpec where
7
9
@@ -14,6 +16,9 @@ import Test.Hspec
14import Test.Hspec.Wai
16import Test.Hspec.Wai
15
17
16import Servant.API.Get
18import Servant.API.Get
19+import Servant.API.Post
20+import Servant.API.RQBody
21+import Servant.API.Sub
17import Servant.Server
22import Servant.Server
18
23
19
24
@@ -27,23 +32,41 @@ instance ToJSON Person
27instance FromJSON Person
32instance FromJSON Person
28
33
29alice :: Person
34alice :: Person
30−alice = Person "Alice" 103
35+alice = Person "Alice" 42
31
36
32
37
33spec :: Spec
38spec :: Spec
34spec = do
39spec = do
35 getSpec
40 getSpec
41+ postSpec
36
42
37
43
38type GetApi = Get Person
44type GetApi = Get Person
39getApi :: Proxy GetApi
45getApi :: Proxy GetApi
40getApi = Proxy
46getApi = Proxy
41
47
42−getSpec = do
48+getSpec :: Spec = do
43 describe "Servant.API.Get" $ do
49 describe "Servant.API.Get" $ do
44 with (return (serve getApi (return alice))) $ do
50 with (return (serve getApi (return alice))) $ do
45− it "serves a Person" $ do
51+ it "allows to GET a Person" $ do
46 response <- get "/"
52 response <- get "/"
47 return response `shouldRespondWith` 200
53 return response `shouldRespondWith` 200
48 liftIO $ do
54 liftIO $ do
49 decode' (simpleBody response) `shouldBe` Just alice
55 decode' (simpleBody response) `shouldBe` Just alice
56+
57+ it "throws 404 on POSTs" $ do
58+ post "/" "" `shouldRespondWith` 404
59+
60+
61+type PostApi = RQBody Person :> (Post Integer)
62+postApi :: Proxy PostApi
63+postApi = Proxy
64+
65+postSpec :: Spec
66+postSpec = do
67+ describe "Servant.API.Post and .RQBody" $ do
68+ with (return (serve postApi (return . age))) $ do
69+ it "allows to POST a Person" $ do
70+ post "/" (encode alice) `shouldRespondWith` "42"{
71+ matchStatus = 201
72+ }