Ensure location header contains _a_ number, not a particular number
This commit is contained in:
@@ -15,6 +15,7 @@ library
|
|||||||
, warp, wai >= 3.0.1 && < 3.0.2
|
, warp, wai >= 3.0.1 && < 3.0.2
|
||||||
, http-types, scientific, time
|
, http-types, scientific, time
|
||||||
, HTTP, convertible
|
, HTTP, convertible
|
||||||
|
, case-insensitive
|
||||||
, bytestring, aeson, network
|
, bytestring, aeson, network
|
||||||
, text, optparse-applicative
|
, text, optparse-applicative
|
||||||
, unordered-containers
|
, unordered-containers
|
||||||
@@ -38,6 +39,7 @@ executable dbapi
|
|||||||
, HDBC, HDBC-postgresql
|
, HDBC, HDBC-postgresql
|
||||||
, warp, wai >= 3.0.1 && < 3.0.2
|
, warp, wai >= 3.0.1 && < 3.0.2
|
||||||
, HTTP, convertible
|
, HTTP, convertible
|
||||||
|
, case-insensitive
|
||||||
, http-types, scientific, time
|
, http-types, scientific, time
|
||||||
, bytestring, aeson, network
|
, bytestring, aeson, network
|
||||||
, text, optparse-applicative
|
, text, optparse-applicative
|
||||||
@@ -63,6 +65,7 @@ Test-Suite spec
|
|||||||
, HDBC, HDBC-postgresql
|
, HDBC, HDBC-postgresql
|
||||||
, warp, wai >= 3.0.1 && < 3.0.2
|
, warp, wai >= 3.0.1 && < 3.0.2
|
||||||
, HTTP, convertible
|
, HTTP, convertible
|
||||||
|
, case-insensitive
|
||||||
, wai-extra, containers
|
, wai-extra, containers
|
||||||
, http-types, scientific, time
|
, http-types, scientific, time
|
||||||
, bytestring, aeson, network
|
, bytestring, aeson, network
|
||||||
|
|||||||
@@ -2,10 +2,11 @@
|
|||||||
{-# LANGUAGE OverloadedStrings, QuasiQuotes #-}
|
{-# LANGUAGE OverloadedStrings, QuasiQuotes #-}
|
||||||
module Feature.InsertSpec where
|
module Feature.InsertSpec where
|
||||||
|
|
||||||
|
-- {{{ Imports
|
||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
import Test.Hspec.Wai
|
import Test.Hspec.Wai
|
||||||
import Test.Hspec.Wai.JSON
|
import Test.Hspec.Wai.JSON
|
||||||
import Network.Wai.Test (SResponse(simpleBody))
|
import Network.Wai.Test (SResponse(simpleBody,simpleHeaders,simpleStatus))
|
||||||
|
|
||||||
import SpecHelper
|
import SpecHelper
|
||||||
|
|
||||||
@@ -14,6 +15,15 @@ import Data.Aeson ((.:))
|
|||||||
import Data.Maybe (fromJust)
|
import Data.Maybe (fromJust)
|
||||||
import Control.Applicative ((<$>), (<*>))
|
import Control.Applicative ((<$>), (<*>))
|
||||||
import Control.Monad (mzero)
|
import Control.Monad (mzero)
|
||||||
|
import qualified Data.HashMap.Strict as Hash
|
||||||
|
import qualified Data.ByteString.Char8 as BS
|
||||||
|
import Data.CaseInsensitive
|
||||||
|
|
||||||
|
import Text.Regex.TDFA ((=~))
|
||||||
|
import Network.HTTP.Types.Header
|
||||||
|
import Network.HTTP.Types.Status
|
||||||
|
|
||||||
|
-- }}}
|
||||||
|
|
||||||
data IncPK = IncPK {
|
data IncPK = IncPK {
|
||||||
incId :: Int
|
incId :: Int
|
||||||
@@ -30,6 +40,14 @@ instance JSON.FromJSON IncPK where
|
|||||||
r .: "inserted_at"
|
r .: "inserted_at"
|
||||||
parseJSON _ = mzero
|
parseJSON _ = mzero
|
||||||
|
|
||||||
|
getHeader :: CI BS.ByteString -> [Header] -> Maybe BS.ByteString
|
||||||
|
getHeader name headers =
|
||||||
|
Hash.lookup name $ Hash.fromList headers
|
||||||
|
|
||||||
|
matchHeader :: CI BS.ByteString -> String -> [Header] -> Bool
|
||||||
|
matchHeader name valRegex headers =
|
||||||
|
maybe False (=~ valRegex) $ getHeader name headers
|
||||||
|
|
||||||
spec :: Spec
|
spec :: Spec
|
||||||
spec = around appWithFixture $
|
spec = around appWithFixture $
|
||||||
describe "Posting new record" $ do
|
describe "Posting new record" $ do
|
||||||
@@ -44,13 +62,13 @@ spec = around appWithFixture $
|
|||||||
context "with no pk supplied" $ do
|
context "with no pk supplied" $ do
|
||||||
context "into a table with auto-incrementing pk" $
|
context "into a table with auto-incrementing pk" $
|
||||||
it "succeeds with 201 and link" $ do
|
it "succeeds with 201 and link" $ do
|
||||||
post "/auto_incrementing_pk" [json| { "non_nullable_string":"not null"} |]
|
p <- post "/auto_incrementing_pk" [json| { "non_nullable_string":"not null"} |]
|
||||||
`shouldRespondWith` ResponseMatcher {
|
liftIO $ do
|
||||||
matchBody = Nothing,
|
simpleBody p `shouldBe` ""
|
||||||
matchStatus = 201,
|
simpleHeaders p `shouldSatisfy` matchHeader hLocation "/auto_incrementing_pk\\?id=eq\\.[0-9]+"
|
||||||
matchHeaders = [("Location", "/auto_incrementing_pk?id=eq.1")]
|
simpleStatus p `shouldBe` created201
|
||||||
}
|
let Just location = getHeader hLocation $ simpleHeaders p
|
||||||
r <- get "/auto_incrementing_pk?id=eq.1"
|
r <- get location
|
||||||
let [record] = fromJust (JSON.decode $ simpleBody r :: Maybe [IncPK])
|
let [record] = fromJust (JSON.decode $ simpleBody r :: Maybe [IncPK])
|
||||||
liftIO $ do
|
liftIO $ do
|
||||||
incStr record `shouldBe` "not null"
|
incStr record `shouldBe` "not null"
|
||||||
|
|||||||
Reference in New Issue
Block a user