fix: Return 406 instead of 415 for non-acceptable media type

415 is for Content-Type and 406 for Accept headers.
This commit is contained in:
Wolfgang Walther
2024-02-15 19:01:12 +01:00
parent 3432f75ed4
commit 2466f4e738
8 changed files with 19 additions and 18 deletions
+4 -4
View File
@@ -30,15 +30,15 @@ spec actualPgVersion = describe "OpenAPI" $ do
, matchHeaders = ["Content-Type" <:> "application/openapi+json; charset=utf-8"]
}
it "should respond to openapi request on none root path with 415" $
it "should respond to openapi request on none root path with 406" $
request methodGet "/items"
(acceptHdrs "application/openapi+json") ""
`shouldRespondWith` 415
`shouldRespondWith` 406
it "should respond to openapi request with unsupported media type with 415" $
it "should respond to openapi request with unsupported media type with 406" $
request methodGet "/"
(acceptHdrs "text/csv") ""
`shouldRespondWith` 415
`shouldRespondWith` 406
it "includes postgrest.org current version api docs" $ do
r <- simpleBody <$> get "/"
+5 -5
View File
@@ -31,7 +31,7 @@ spec = describe "custom media types" $ do
request methodGet "/lines" (acceptHdrs "text/plain") ""
`shouldRespondWith`
[json| {"code":"PGRST107","details":null,"hint":null,"message":"None of these media types are available: text/plain"} |]
{ matchStatus = 415
{ matchStatus = 406
, matchHeaders = [matchContentTypeJson]
}
@@ -115,7 +115,7 @@ spec = describe "custom media types" $ do
[json|
{"code":"PGRST107","details":null,"hint":null,"message":"None of these media types are available: text/xml"}
|]
{ matchStatus = 415
{ matchStatus = 406
, matchHeaders = ["Content-Type" <:> "application/json; charset=utf-8"]
}
@@ -140,7 +140,7 @@ spec = describe "custom media types" $ do
""
`shouldRespondWith`
[json|{"code":"PGRST107","details":null,"hint":null,"message":"None of these media types are available: text/plain"}|]
{ matchStatus = 415
{ matchStatus = 406
, matchHeaders = ["Content-Type" <:> "application/json; charset=utf-8"]
}
@@ -156,7 +156,7 @@ spec = describe "custom media types" $ do
(acceptHdrs "application/octet-stream") ""
`shouldRespondWith`
[json| {"code":"PGRST107","details":null,"hint":null,"message":"None of these media types are available: application/octet-stream"} |]
{ matchStatus = 415 }
{ matchStatus = 406 }
-- TODO SOH (start of heading) is being added to results
it "works if there's an anyelement aggregate defined" $ do
@@ -279,7 +279,7 @@ spec = describe "custom media types" $ do
}
request methodGet "/rpc/ret_some_mt" (acceptHdrs "text/csv") ""
`shouldRespondWith` 415
`shouldRespondWith` 406
context "on tables" $ do
it "returns application/json for */* if not explicitly set" $ do
+3 -3
View File
@@ -463,12 +463,12 @@ disabledSpec =
it "doesn't work if db-plan-enabled=false(the default)" $ do
request methodGet "/projects?id=in.(1,2,3)"
(acceptHdrs "application/vnd.pgrst.plan") ""
`shouldRespondWith` 415
`shouldRespondWith` 406
request methodGet "/rpc/getallprojects?id=in.(1,2,3)"
(acceptHdrs "application/vnd.pgrst.plan") ""
`shouldRespondWith` 415
`shouldRespondWith` 406
request methodDelete "/projects?id=in.(1,2,3)"
(acceptHdrs "application/vnd.pgrst.plan") ""
`shouldRespondWith` 415
`shouldRespondWith` 406
+2 -2
View File
@@ -939,12 +939,12 @@ spec actualPgVersion = do
}
describe "Accept headers" $ do
it "should respond an unknown accept type with 415" $
it "should respond an unknown accept type with 406" $
request methodGet "/simple_pk"
(acceptHdrs "text/unknowntype") ""
`shouldRespondWith`
[json|{"message":"None of these media types are available: text/unknowntype","code":"PGRST107","details":null,"hint":null}|]
{ matchStatus = 415
{ matchStatus = 406
, matchHeaders = [matchContentTypeJson]
}
+2 -2
View File
@@ -656,10 +656,10 @@ spec actualPgVersion =
it "rejects unknown content type even if payload is good" $ do
request methodPost "/rpc/sayhello"
(acceptHdrs "audio/mpeg3") [json| { "name": "world" } |]
`shouldRespondWith` 415
`shouldRespondWith` 406
request methodGet "/rpc/sayhello?name=world"
(acceptHdrs "audio/mpeg3") ""
`shouldRespondWith` 415
`shouldRespondWith` 406
it "rejects malformed json payload" $ do
p <- request methodPost "/rpc/sayhello"
(acceptHdrs "application/json") "sdfsdf"
+1 -1
View File
@@ -3714,7 +3714,7 @@ begin
perform set_config('response.headers', json_build_array(json_build_object('Content-Type', 'app/groucho'))::text, true);
resp := 'groucho';
else
raise sqlstate 'PT415' using message = 'Unsupported Media Type';
raise sqlstate 'PT406' using message = 'Not Acceptable';
end case;
return resp;
end; $$ language plpgsql;