Do not include Content-Type header for empty body (#580)

* Do not include Content-Type header for empty body

Fixes #544

* Fix lint

* Changelog
This commit is contained in:
Joe Nelson
2016-05-03 21:18:23 -07:00
parent 88aad4b1b6
commit d9205bd838
3 changed files with 23 additions and 15 deletions
+1
View File
@@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Fixed ### Fixed
- Omit Content-Type header for empty body - @begriffs
- Prevent role from being changed twice - @begriffs - Prevent role from being changed twice - @begriffs
- Use read-only transaction for read requests - @ruslantalpa - Use read-only transaction for read requests - @ruslantalpa
+11 -8
View File
@@ -127,12 +127,14 @@ app dbStructure conf apiRequest =
let stm = createWriteStatement qi sq mq isSingle (iPreferRepresentation apiRequest) pKeys (contentType == TextCSV) payload let stm = createWriteStatement qi sq mq isSingle (iPreferRepresentation apiRequest) pKeys (contentType == TextCSV) payload
row <- H.query uniform stm row <- H.query uniform stm
let (_, _, location, body) = extractQueryResult row let (_, _, location, body) = extractQueryResult row
return $ responseLBS status201
[ return $ if iPreferRepresentation apiRequest == Full
contentTypeH, then responseLBS status201 [
(hLocation, "/" <> cs table <> "?" <> cs location) contentTypeH,
] (hLocation, "/" <> cs table <> "?" <> cs location)
$ if iPreferRepresentation apiRequest == Full then cs body else "" ] (cs body)
else responseLBS status201
[(hLocation, "/" <> cs table <> "?" <> cs location)] ""
(ActionUpdate, TargetIdent qi, Just payload@(PayloadJSON uniform)) -> (ActionUpdate, TargetIdent qi, Just payload@(PayloadJSON uniform)) ->
case mutateSqlParts of case mutateSqlParts of
@@ -145,8 +147,9 @@ app dbStructure conf apiRequest =
s = case () of _ | queryTotal == 0 -> status404 s = case () of _ | queryTotal == 0 -> status404
| iPreferRepresentation apiRequest == Full -> status200 | iPreferRepresentation apiRequest == Full -> status200
| otherwise -> status204 | otherwise -> status204
return $ responseLBS s [contentTypeH, r] return $ if iPreferRepresentation apiRequest == Full
$ if iPreferRepresentation apiRequest == Full then cs body else "" then responseLBS s [contentTypeH, r] (cs body)
else responseLBS s [r] ""
(ActionDelete, TargetIdent qi, Nothing) -> (ActionDelete, TargetIdent qi, Nothing) ->
case mutateSqlParts of case mutateSqlParts of
+11 -7
View File
@@ -32,6 +32,8 @@ spec = do
liftIO $ do liftIO $ do
simpleBody p `shouldBe` "" simpleBody p `shouldBe` ""
simpleStatus p `shouldBe` created201 simpleStatus p `shouldBe` created201
-- should not have content type set when body is empty
lookup hContentType (simpleHeaders p) `shouldBe` Nothing
it "filters columns in result using &select" $ it "filters columns in result using &select" $
request methodPost "/menagerie?select=integer,varchar" [("Prefer", "return=representation")] request methodPost "/menagerie?select=integer,varchar" [("Prefer", "return=representation")]
@@ -312,13 +314,15 @@ spec = do
g <- get "/items?id=eq.42" g <- get "/items?id=eq.42"
liftIO $ simpleHeaders g liftIO $ simpleHeaders g
`shouldSatisfy` matchHeader "Content-Range" "\\*/0" `shouldSatisfy` matchHeader "Content-Range" "\\*/0"
request methodPatch "/items?id=eq.2" [] p <- request methodPatch "/items?id=eq.2" [] [json| { "id":42 } |]
[json| { "id":42 } |] pure p `shouldRespondWith` ResponseMatcher {
`shouldRespondWith` ResponseMatcher { matchBody = Nothing,
matchBody = Nothing, matchStatus = 204,
matchStatus = 204, matchHeaders = ["Content-Range" <:> "0-0/1"]
matchHeaders = ["Content-Range" <:> "0-0/1"] }
} liftIO $
lookup hContentType (simpleHeaders p) `shouldBe` Nothing
g' <- get "/items?id=eq.42" g' <- get "/items?id=eq.42"
liftIO $ simpleHeaders g' liftIO $ simpleHeaders g'
`shouldSatisfy` matchHeader "Content-Range" "0-0/1" `shouldSatisfy` matchHeader "Content-Range" "0-0/1"