Fix Location link in post to db lacking a primary key

Fixes #29
This commit is contained in:
Joe Nelson
2014-09-07 14:12:51 -07:00
parent d166cbc371
commit 4d81959a2a
2 changed files with 11 additions and 1 deletions
+3 -1
View File
@@ -75,7 +75,9 @@ app conn req respond = do
jsonBodyAction req (\row -> do
allvals <- insert ver table row conn
keys <- primaryKeyColumns ver (unpack table) conn
let keyvals = allvals `intersection` fromList (zip keys $ repeat SqlNull)
let keyvals = if null keys
then allvals
else allvals `intersection` fromList (zip keys $ repeat SqlNull)
let params = urlEncodeVars $ map (\t -> (fst t, "eq." <> convert (snd t) :: String)) $ toList keyvals
return $ responseLBS status201
[ jsonContentType
+8
View File
@@ -50,6 +50,14 @@ spec = around appWithFixture $
post "/simple_pk" [json| { "extra":"foo"} |]
`shouldRespondWith` 400
context "into a table with no pk" $
it "succeeds with 201 and a link including all fields" $ do
p <- post "/no_pk" [json| { "a":"foo", "b":"bar" } |]
liftIO $ do
simpleBody p `shouldBe` ""
simpleHeaders p `shouldSatisfy` matchHeader hLocation "/no_pk\\?a=eq.foo&b=eq.bar"
simpleStatus p `shouldBe` created201
context "with compound pk supplied" $
it "builds response location header appropriately" $
post "/compound_pk" [json| { "k1":12, "k2":42 } |]