git.iko.soy/typeable/servant
on master help sign in
test suite: Post
commitd0782db08b74203ce5b070280bce286896493c8c authorSönke Hahn <soenke.hahn@zalora.com> dateMon 27 Oct 2014, 08:10 (12 years ago) parent13664a2
2 files changed+26−4
src/Servant/API/RQBody.hs−1 test/Servant/ServerSpec.hs+26−3
src/Servant/API/RQBody.hs−1
@@ -8,7 +8,6 @@ module Servant.API.RQBody where
88import Control.Applicative
99import Data.Aeson
1010import Data.Proxy
11−import Data.Text
1211import Network.Wai
1312import Servant.API.Sub
1413import Servant.Client
test/Servant/ServerSpec.hs+26−3
@@ -1,7 +1,9 @@
1+{-# LANGUAGE DataKinds #-}
12{-# LANGUAGE DeriveGeneric #-}
23{-# LANGUAGE OverloadedStrings #-}
34{-# LANGUAGE QuasiQuotes #-}
45{-# LANGUAGE ScopedTypeVariables #-}
6+{-# LANGUAGE TypeOperators #-}
57
68module Servant.ServerSpec where
79
@@ -14,6 +16,9 @@ import Test.Hspec
1416import Test.Hspec.Wai
1517
1618import Servant.API.Get
19+import Servant.API.Post
20+import Servant.API.RQBody
21+import Servant.API.Sub
1722import Servant.Server
1823
1924
@@ -27,23 +32,41 @@ instance ToJSON Person
2732instance FromJSON Person
2833
2934alice :: Person
30−alice = Person "Alice" 103
35+alice = Person "Alice" 42
3136
3237
3338spec :: Spec
3439spec = do
3540 getSpec
41+ postSpec
3642
3743
3844type GetApi = Get Person
3945getApi :: Proxy GetApi
4046getApi = Proxy
4147
42−getSpec = do
48+getSpec :: Spec = do
4349 describe "Servant.API.Get" $ do
4450 with (return (serve getApi (return alice))) $ do
45− it "serves a Person" $ do
51+ it "allows to GET a Person" $ do
4652 response <- get "/"
4753 return response `shouldRespondWith` 200
4854 liftIO $ do
4955 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+ }