From d0782db08b74203ce5b070280bce286896493c8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Hahn?= Date: Mon, 27 Oct 2014 08:10:48 +0100 Subject: [PATCH] test suite: Post --- src/Servant/API/RQBody.hs | 1 - test/Servant/ServerSpec.hs | 29 ++++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/Servant/API/RQBody.hs b/src/Servant/API/RQBody.hs index b08ed8ff..624263a6 100644 --- a/src/Servant/API/RQBody.hs +++ b/src/Servant/API/RQBody.hs @@ -8,7 +8,6 @@ module Servant.API.RQBody where import Control.Applicative import Data.Aeson import Data.Proxy -import Data.Text import Network.Wai import Servant.API.Sub import Servant.Client diff --git a/test/Servant/ServerSpec.hs b/test/Servant/ServerSpec.hs index a44e1363..21136faf 100644 --- a/test/Servant/ServerSpec.hs +++ b/test/Servant/ServerSpec.hs @@ -1,7 +1,9 @@ +{-# LANGUAGE DataKinds #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TypeOperators #-} module Servant.ServerSpec where @@ -14,6 +16,9 @@ import Test.Hspec import Test.Hspec.Wai import Servant.API.Get +import Servant.API.Post +import Servant.API.RQBody +import Servant.API.Sub import Servant.Server @@ -27,23 +32,41 @@ instance ToJSON Person instance FromJSON Person alice :: Person -alice = Person "Alice" 103 +alice = Person "Alice" 42 spec :: Spec spec = do getSpec + postSpec type GetApi = Get Person getApi :: Proxy GetApi getApi = Proxy -getSpec = do +getSpec :: Spec = do describe "Servant.API.Get" $ do with (return (serve getApi (return alice))) $ do - it "serves a Person" $ do + it "allows to GET a Person" $ do response <- get "/" return response `shouldRespondWith` 200 liftIO $ do decode' (simpleBody response) `shouldBe` Just alice + + it "throws 404 on POSTs" $ do + post "/" "" `shouldRespondWith` 404 + + +type PostApi = RQBody Person :> (Post Integer) +postApi :: Proxy PostApi +postApi = Proxy + +postSpec :: Spec +postSpec = do + describe "Servant.API.Post and .RQBody" $ do + with (return (serve postApi (return . age))) $ do + it "allows to POST a Person" $ do + post "/" (encode alice) `shouldRespondWith` "42"{ + matchStatus = 201 + }