Test JSON payload for post

This commit is contained in:
Joe Nelson
2014-08-23 21:49:16 -07:00
parent 83a70343af
commit 094c145228
2 changed files with 35 additions and 2 deletions
+1
View File
@@ -57,6 +57,7 @@ Test-Suite spec
, hspec-wai
, HDBC, HDBC-postgresql
, warp, wai >= 3.0.1 && < 3.0.2
, wai-extra
, http-types, scientific, time
, bytestring, aeson, network
, text, optparse-applicative
+34 -2
View File
@@ -8,6 +8,28 @@ import Test.Hspec.Wai.JSON
import SpecHelper
import Network.HTTP.Types
import Network.Wai.Test (SResponse(..))
import qualified Data.Aeson as JSON
import Data.Aeson ((.:))
import Data.Maybe (fromJust)
import Control.Applicative ((<$>), (<*>))
import Control.Monad (mzero)
data IncPK = IncPK {
incId :: Int
, incNullableStr :: Maybe String
, incStr :: String
, incInsert :: String
} deriving (Show)
instance JSON.FromJSON IncPK where
parseJSON (JSON.Object r) = IncPK <$>
r .: "id" <*>
r .: "nullable_string" <*>
r .: "non_nullable_string" <*>
r .: "inserted_at"
parseJSON _ = mzero
spec :: Spec
spec = around appWithFixture $ do
@@ -75,7 +97,17 @@ spec = around appWithFixture $ do
describe "Posting new record" $ do
context "into a table with auto-incrementing pk" $ do
it "does not require pk in the payload" $ do
it "does not require pk in the payload" $
post "/auto_incrementing_pk" [json|
{ "non_nullable_string":"not null"} |]
`shouldRespondWith` 200
`shouldRespondWith` 201
it "responds with the created row" $ do
r <- post "/auto_incrementing_pk" [json|
{ "non_nullable_string":"not null"} |]
let row = fromJust (JSON.decode $ simpleBody r :: Maybe IncPK)
liftIO $ do
incStr row `shouldBe` "not null"
incNullableStr row `shouldBe` Nothing
-- Add assertions to check timestamp and id
-- OR: change behavior to return no body at all