diff --git a/CHANGELOG.md b/CHANGELOG.md index e3d2cab07..e234c4002 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Added - Server response logging - Filter IN values, e.g. `?col=in.1,2,3` +- Return POSTed resource if header "Prefer: return=representation" ## [0.2.6.0] - 2015-02-18 ### Added diff --git a/src/App.hs b/src/App.hs index 2d350cd6a..32c5ab7e0 100644 --- a/src/App.hs +++ b/src/App.hs @@ -102,6 +102,7 @@ app reqBody req = handleJsonObj reqBody $ \obj -> do let qt = QualifiedTable schema (cs table) query = insertInto qt (map cs $ keys obj) (elems obj) + echoRequested = lookup "Prefer" hdrs == Just "return=representation" row <- H.maybeEx query let (Identity insertedJson) = fromMaybe (Identity "{}" :: Identity Text) row Just inserted = decode (cs insertedJson) :: Maybe Object @@ -116,7 +117,7 @@ app reqBody req = return $ responseLBS status201 [ jsonH , (hLocation, "/" <> cs table <> "?" <> cs params) - ] "" + ] $ if echoRequested then cs insertedJson else "" ([table], "PUT") -> handleJsonObj reqBody $ \obj -> do diff --git a/test/Feature/InsertSpec.hs b/test/Feature/InsertSpec.hs index 4196fcb00..d593421cb 100644 --- a/test/Feature/InsertSpec.hs +++ b/test/Feature/InsertSpec.hs @@ -49,7 +49,7 @@ spec = afterAll_ resetDb $ around withApp $ do post "/simple_pk" [json| { "extra":"foo"} |] `shouldRespondWith` 400 - context "into a table with no pk" . after_ (clearTable "no_pk") $ + context "into a table with no pk" . after_ (clearTable "no_pk") $ do it "succeeds with 201 and a link including all fields" $ do p <- post "/no_pk" [json| { "a":"foo", "b":"bar" } |] liftIO $ do @@ -57,6 +57,15 @@ spec = afterAll_ resetDb $ around withApp $ do simpleHeaders p `shouldSatisfy` matchHeader hLocation "/no_pk\\?a=eq.foo&b=eq.bar" simpleStatus p `shouldBe` created201 + it "returns full details of inserted record if asked" $ do + p <- request methodPost "/no_pk" + [("Prefer", "return=representation")] + [json| { "a":"bar", "b":"baz" } |] + liftIO $ do + simpleBody p `shouldBe` [json| { "a":"bar", "b":"baz" } |] + simpleHeaders p `shouldSatisfy` matchHeader hLocation "/no_pk\\?a=eq.bar&b=eq.baz" + simpleStatus p `shouldBe` created201 + context "with compound pk supplied" . after_ (clearTable "compound_pk") $ it "builds response location header appropriately" $ post "/compound_pk" [json| { "k1":12, "k2":42 } |]